• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Image upload problem

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.*, java.io.*" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form action="disable.jsp" method="post" enctype="multipart/form-data" name="disable">
<br/>
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"/>
<hr/>
<input type="submit"/>
<input type="reset"/>
<br/>
</form>
</body>
</html>

<%
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)it.next();
if (fileItemTemp.isFormField())
{
if (fileItemTemp.getFieldName().equals("filename"))
optionalFileName = fileItemTemp.getString();
}//end of if(fileItemTemp.isFormField()) condition
else
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();
else
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( ?, ? , ?)");
pstmt.setInt(1,100);
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.printStackTrace();
%>
<%=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

Regards.
SK
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
to:
fipImage = fileItem.getInputStream();

That should get you past your issue.
 
Shrikant Kulkarni
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carl,

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

Shrikant
 
What's that smell? Hey, sniff this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic