• 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 ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • 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
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
 
Greenhorn
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)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!