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
JavaRanch » Java Forums » Java » JSP
Bookmark "how to upload file" Watch "how to upload file" New topic
Author

how to upload file

Santosh Kumar
Ranch Hand

Joined: May 25, 2005
Posts: 30
hi

i am using the following code for uploading file

fileUpload.jsp
-----------------
<%@ page import="java.util.*,java.io.*,org.apache.commons.fileupload.*" %>
<HTML>
<BODY>
<%

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)
//upload.setSizeThreshold(2000);
// set a directory to temporarily upload the file if
// the last memory size in insufficient (optional) :
//upload.setRepositoryPath("c:/temp");
// set the max uploaded file size (-1 for no max size)
upload.setSizeMax(-1);

// 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>");
}
else
{ // 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);
item.write(uploadedFile);
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());
}
%>
</BODY>
</HTML>

html file is

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

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



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
:java.lang.NullPointerException

how to handle this
please help me out

waiting for your reply


regards
santosh
Bineet Singh Uppal
Greenhorn

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to upload file