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

Image upload problem

Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
Hi All,
I want to upload some .jpeg images from a PC into a DB for that i am writing a java application. I am using apache's fileupload API's for this.
The code is given below:

<%@ page import="org.apache.commons.fileupload.*, java.util.*, java.lang.Exception, oracle.jdbc.driver.OracleDriver, java.sql.*,*" %>
<html xmlns="">
<form action="disable.jsp" method="post" enctype="multipart/form-data" name="disable">
Choose a photo to be uploaded to the WAP server:
<input name="myFile" type="file"/><br/>
On the WAP server, save the file as: (Enter something here if you want the file to be saved in a different file name.)
<input name="filename" type="text"/>
<input type="submit"/>
<input type="reset"/>

if (FileUpload.isMultipartContent(request))
DiskFileUpload diskFileUpload = new DiskFileUpload();
List fileItemsList = diskFileUpload.parseRequest(request);
String optionalFileName = "";
FileItem fileItem = null;

Iterator it = fileItemsList.iterator();
while (it.hasNext())
FileItem fileItemTemp = (FileItem);
if (fileItemTemp.isFormField())
if (fileItemTemp.getFieldName().equals("filename"))
optionalFileName = fileItemTemp.getString();
}//end of if(fileItemTemp.isFormField()) condition
fileItem = fileItemTemp;
}// end of while loop

if (fileItem!=null)
String fileName = fileItem.getName();
/* Save the uploaded file if its size is greater than 0. */
String sFileName = fileName;
if (fileItem.getSize() > 0)
if (optionalFileName.trim().equals(""))
fileName = (new File(fileName)).getName();
fileName = optionalFileName;
File saveTo = new File(sFileName);
FileInputStream fipImage = null;
int result =0;
Connection conn = null;
ResultSet rs = null;
try {
conn = DisableText.getConnection();
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO image_upload VALUES( ?, ? , ?)");
fipImage = new FileInputStream(saveTo); // #### Error occurs
pstmt.setBinaryStream( 3, fipImage, (int)( saveTo.length() ) );
pstmt.setString(2, "pic");
result = pstmt.executeUpdate();
if(result != 0){
The uploaded file has been saved successfully.
}// end of if(result != 0) condition
}// end of try block
catch (Exception e){
<%=e%> <br/>
An error occurred when we tried to save the uploaded file. <br/>
} // end of catch block
}// end of if(fileItem.getSize() > 0) condition
} // end of if(fileItem!=null) condition
}// end of if(FileUpload.isMultipartContent(request)) condition

The EAR file of this application is created and deployed in the application server. When application is accessed through IE browser and tried to upload a .jpeg image, it gives FileNotFound error.
(The "####" sign indicates the line where the FileNotFound error occurs in the code written above.)
But the same code works in the Debug mode. The same application when deployed in the server and accessed through IE browser it gives FileNotFound error.

Can any one please help on in solving this problem.
Thanks well in advance

Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
You need to look at how FileUpload works.

The biggest thing, is during this part, the file is either in memory or in a temporary directory with a temporary file name. You can drop the code:

File saveTo = new File(sFileName);

And change the code:
fipImage = new FileInputStream(saveTo);
fipImage = fileItem.getInputStream();

That should get you past your issue.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
Hi Carl,

Really a greate analysis. It worked out. It is fixed my problem.
Thanks a lot.

I agree. Here's the link:
subject: Image upload problem
It's not a secret anymore!