Hello…
I have a problem.. and i need some help with it..
this is my form in html:
[CODE]
<form method=”POST” name=”form2″ id=”form2″ enctype=’multipart/form-data’ action=”/servlet/uploadImage”>
<input type=”file”/>
</form>
and this is the class im using:
[CODE]
public class uploadImage extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileName=processFile(request);
HttpSession sess = request.getSession();
if(fileName!=null){
sess.setAttribute(“nombreLogo”,fileName);
}else{
sess.setAttribute(“nombreLogo”,”null”);
}
}
public String processFile(HttpServletRequest req) {
String fileNameReturn=””;
try {
// object capable of parsing request
DiskFileUpload fu = new DiskFileUpload();
// max number of bytes
fu.setSizeMax(1024*100); // 100 K
fu.setSizeThreshold(4096);
//fu.setRepositoryPath(“/tmp”);
// file processing
List fileItems = fu.parseRequest(req);
if(fileItems == null)
{
System.out.println(“The list is null”);
return null;
}
System.out.println(“<br>The number of files uploaded is: ” + fileItems.size());
// We iterate on the files
Iterator i = fileItems.iterator();
FileItem actual = null;
System.out.println(“estamos en la iteración”);
while (i.hasNext())
{
actual = (FileItem)i.next();
String fileName = actual.getName();
System.out.println(“<br>File uploaded: ” + fileName);
// we create a file object
File fichero = new File(fileName);
System.out.println(“The file name is: ” + fichero.getName());
fileNameReturn=fichero.getName();
fichero = new File(“d:\path\” + fichero.getName());
actual.write(fichero);
}
}
catch(Exception e) {
System.out.println(e.getMessage());
return null;
}
return fileNameReturn;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
HttpSession sess = request.getSession();
if(String.valueOf(sess.getAttribute(“nombreLogo”)).equals(“null”)){
sess.setAttribute(“msg”,”El tamaño del archivo excede el máximo permitido.”);
}
response.sendRedirect(“/showroom/personalizaInteractivo.jsp”);
}
}
What i want to do is to be able to put other types of “inputs” such as “text” on the html form, the problem im having is that when the class tries to process the request, it throws an exception because the first input is no “file”
i dont know if im being clear.. ask me for more info if you think it is necessary
I need to send something like this
[code=html]
<form method=”POST” name=”form2″ id=”form2″ enctype=’multipart/form-data’ action=”/servlet/uploadImage>
Name: <input type=”text” name=”name”/><br/>
Upload your picture: <input type=”file”/><br/>
<input type=”submit”/>
</form>
and i need to insert those values in a database.
the class i got from a tutorial from here:
[url]http://www.adictosaltrabajo.com/tutoriales/tutoriales.php?pagina=fileupload
its in spanish though..
thanks in advance