aspose file tools*
The moose likes JSP and the fly likes how to upload file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to upload file" Watch "how to upload file" New topic

how to upload file

Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30

i am using the following code for uploading file

<%@ page import="java.util.*,*,org.apache.commons.fileupload.*" %>

try {

out.print("inside try</b><br>");
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();

// Set upload parameters
//set memory size allowed to the uploading (optional)
// set a directory to temporarily upload the file if
// the last memory size in insufficient (optional) :
// set the max uploaded file size (-1 for no max size)

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

// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext())
FileItem item = (FileItem);
if (item.isFormField())
{ // the item is not a file item
String name = item.getFieldName();
String value = item.getString();
out.print("item :<b>"+name+"</b> value:<b>"+value+"</b><hr>");
{ // the item is a file item
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
out.print("item :<b>"+fieldName+"</b><br>");
out.print("  file :<b>"+fileName+"</b><br>");
out.print("  content type :<b>"+contentType+"</b><br>");
out.print("  in memory ? :<b>"+isInMemory+"</b><br>");
out.print("  size:<b>"+sizeInBytes+"</b> bits<br>");

String destFile="c:\\"+fileName;
// copy the file to disk
File uploadedFile = new File(destFile);
out.print("  file uploaded to : <b>"+destFile+"<hr>");
}//while end
}//try end
catch (FileUploadBase.SizeLimitExceededException e)
out.print("COMMONS error : maximum file size exceeded");
catch (FileUploadException e)
out.print("COMMONS error :"+e.toString());
catch (Exception e)
out.print("error :"+e.toString());

html file is

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<FORM ACTION="file_upload.jsp" ENCTYPE="multipart/form-data" METHOD=POST>
Which file do you want to upload? <INPUT TYPE=FILE NAME=file> <BR>

when i execute it i am getting the output till the filesize correctly but when the file has to be moved to a particular location its giving error as

how to handle this
please help me out

waiting for your reply

Bineet Singh Uppal

Joined: Sep 26, 2005
Posts: 5
Dear Santosh,
I copied ur code, made two files and to my surprise the file was uploaded sucessfully. I uploaded mp3 of 8mb and some other files..even binary exe was uploaded. May be some configuration mismatch is there...

Hope for the Best, Ready for the Worst
I agree. Here's the link:
subject: how to upload file