Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to upload file  RSS feed

Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!