File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes image upload to a paticular folder at web server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "image upload to a paticular folder at web server" Watch "image upload to a paticular folder at web server" New topic

image upload to a paticular folder at web server

donghai fu

Joined: Dec 12, 2012
Posts: 1
Please please please help me, I am stuck on this for quiet a while and checked the previous posts here, but still could not fix this. My code is as below, when run it, it always shows failure, and nothing uploaded on the server.

1) I don't know which line to set up the folder on the server for me to save the images, it is: "/upload/"

2) I searched the online posts, says I need to install the "The Commons FileUpload package" through, I don't know how to install the page, in which folder of my server?

Really appreciate your help.


<form method="post" ACTION="upload.jsp" name="uploadForm" ENCTYPE='multipart/form-data'>
<input type="file" name="uploadFile" >
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">

My "upload.jsp" is as:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page import="*" %>
String contentType = request.getContentType();
System.out.println("Content type is :: " + contentType);
String imageSave=null;
byte dataBytes[]=null;
String saveFile=null;
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
dataBytes = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
byteRead =, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
// out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1, contentType.length());
// out.println(boundary);
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
FileOutputStream fileOut = new FileOutputStream("c://"+saveFile);
// fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
}catch (Exception e)
System.err.println ("Error writing to file");
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>File Upload Success/Failure</TITLE>
<table align="center" width="300" height="100">
<th width="85">Image Name:</th>
<td width="60"><%=saveFile%></td>
<th width="85">No Of Bytes:</th>
<td width="60"><%=dataBytes.length%></td>
<th width="85">Image Save:</th>
<td width="60"><%=imageSave%></td>

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63868

Step 1: If you've looked at other topics on this issue, you'll have noted that the Java code should not be in a JSP but in a servlet. So first step is to ditch the JSP and put the file upload handling code in a servlet.

Step 1: Place the jar file for the commons library in the web app's WEB-INF/lib folder.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: image upload to a paticular folder at web server
It's not a secret anymore!