• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Joe Ess
  • salvin francis
  • fred rosenberger

how to upload file

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.*,java.io.*,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) iter.next();
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...
Then YOU must do the pig's work! Read this tiny ad. READ IT!
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!