File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes upload file question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "upload file question" Watch "upload file question" New topic

upload file question

hesham katon
Ranch Hand

Joined: Dec 18, 2002
Posts: 82
Hello everybody,
I am trying to make simple upload file through html form
I used Jakarta Commons FileUpload
I have made the code typically like jakarta description :
<%@ page import="org.apache.commons.fileupload.*,java.util.*,*" %>
// Check that we have a file upload request
boolean isMultipart = FileUpload.isMultipartContent(request);

// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);

// Process the uploaded items
Iterator iter = items.iterator();

while (iter.hasNext())
FileItem item = (FileItem);

if (item.isFormField()) {
out.println("hi , this is form based item "+item.getFieldName()+" "+item.getString()+"<BR>");
} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();

InputStream uploadedStream = item.getInputStream();
out.println("hi , this is uploaded item "+item.getFieldName()+" "+item.getName()+" "+item.getName()+" "+item.getSize()+"<BR>");



also this is the form i am using :
<form method="post" ACTION="upload.jsp" name="upform" ENCTYPE='multipart/form-data'>
<input type="file" name="uploadfile">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
<input type="hidden" name="action" value="upload">


the upload.jsp works fine without errors , and generate some infromation about the file i want to upload , its name , its size ... but i don't know where this file is uploaded ???

i searched on the server just beside the upload.jsp page , but i didn't find any uploaded file !

can any one help me and tell me what i am missing in this code ?
or, if the file is really uploaded , where is it , and how i can define a specific place to upload it ?

thank you very much in advance

Hesham Katon<br />SCJP 1.2<br />SCWCD 1.4<br />Better to light a candle than to curse the darkness
Jeremy Wilson
Ranch Hand

Joined: Feb 18, 2003
Posts: 166
InputStream uploadedStream = item.getInputStream();

With these lines you are not reading the inputstream. Create a file object with this input stream and write it out to a place on disk the server has access to. The way it is now the file attributes are only pulled from the request and never written to disk.

Jeremy Wilson
hesham katon
Ranch Hand

Joined: Dec 18, 2002
Posts: 82
thanks Jeremy , but can you help me with some code example ..
this is what i have tried to do , but still not workin

// creating File Object
InputStream uploadedFile = item.getInputStream();
File serverFile = new File("3.gif");

// then i must convert the file into bytes to write it
byte[] byteArray=new byte[uploadedFile.available()];;
FileOutputStream os = new FileOutputStream(serverFile);
the code is work without exception
but still no result on the disk
wish you can help
I agree. Here's the link:
subject: upload file question
It's not a secret anymore!