• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

upload file question

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :
====================upload.jsp=========================
<%@ page import="org.apache.commons.fileupload.*,java.util.*,java.io.*" %>
<%
// 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) iter.next();

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();
uploadedStream.close();
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.html=============
<form method="post" ACTION="upload.jsp" name="upform" ENCTYPE='multipart/form-data'>
<input type="file" name="uploadfile">
<p>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
<input type="hidden" name="action" value="upload">

</form>
====================================

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
 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
InputStream uploadedStream = item.getInputStream();
uploadedStream.close();

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.
 
hesham katon
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
serverFile.createNewFile();

// then i must convert the file into bytes to write it
byte[] byteArray=new byte[uploadedFile.available()];

uploadedFile.read(byteArray);
FileOutputStream os = new FileOutputStream(serverFile);
os.write(byteArray);
os.close();
=============================================================
the code is work without exception
but still no result on the disk
wish you can help
 
Look ma! I'm selling my stuff!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic