File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Problem with uploading images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem with uploading images" Watch "Problem with uploading images" New topic

Problem with uploading images

paresh pisipati

Joined: May 17, 2011
Posts: 1
Hi.I have a problem with my jsp code.Whenever i try to upload a file, other than image file,all other files are getting uploaded.For images,it is displayed as invalid image

my form is
<%@ page language="java" %>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<% // for uploading the file we used Encrypt type of multipart/form-data and input of file type to browse and submit the file %>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="fileupload.jsp" METHOD=POST>
<center><table border="2" >
<tr><center><td colspan="2"><p align="center"><B>PROGRAM FOR UPLOADING THE FILE</B><center></td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="F1" TYPE="file"></td></tr>
<tr><td colspan="2"><p align="right"><INPUT TYPE="submit" VALUE="Upload File" ></p></td></tr>

my jsp file is:

<%@ page import="*" %>
//to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead =, totalBytesRead, formDataLength);
totalBytesRead += byteRead;

String file = new String(dataBytes);
//for saving the file name
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
//extracting the index of file
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;

// creating a new file with the same name and writing the content in new file
FileOutputStream fileOut = new FileOutputStream("/home/inkntcom/public_html/files/"+saveFile);
catch(Exception ex){
out.println("The error is "+ex);
%><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b>
<% out.println(saveFile); %></td></tr></table> <%

Please reply to this post
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Step 1 is to move the upload Java code out of the JSP and into a servlet. This is misuse of JSPs.

Once you've done that please repost the code and be sure to UseCodeTags so that the code is readable.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Problem with uploading images
jQuery in Action, 3rd edition