0

I am trying to insert a image file to MySql DB by getting the file from user.On inserting the file the code works fine but on converting File to Blob format i get an java.io.FileNotFoundException Here is my JSP code:

File img= new File(request.getParameter("rfile"));
String accno=request.getParameter("raccno");
String reportname=request.getParameter("reportname");
String date=request.getParameter("rdate");
PreparedStatement pt2=null;
pt2= connection.prepareStatement("insert into reportuser(Account_No,date,report_name,report_image)"+ "values(?,?,?,?)");
pt2.setString(1, accno);
pt2.setString(2, date);
pt2.setString(3, reportname);
pt2.setBinaryStream(4, (InputStream) fiss, (int) (img.length()));
pt2.executeUpdate();InputStream fiss=new FileInputStream(img);

The exception report is as follows:

org.apache.jasper.JasperException: An exception occurred processing JSP page /add_report.jsp at line 12
9: String reportname=request.getParameter("reportname");
10: String date=request.getParameter("rdate");
11: File img= new File(request.getParameter("rfile"));
12: InputStream fiss=new FileInputStream(img);

it is followed by:

root cause
java.io.FileNotFoundException: logo.png (The system cannot find the file specified)
    java.io.FileInputStream.open(Native Method)
    java.io.FileInputStream.<init>(FileInputStream.java:138)
    org.apache.jsp.add_005freport_jsp._jspService(add_005freport_jsp.java:78)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Shubhanshu
  • 57
  • 6
  • umm, storing images in MySQL is a bad idea why not make a folder for the images and store links to them in the db instead? – 1337holiday Apr 13 '13 at 17:20
  • possible duplicate of [How to upload files to server using JSP/Servlet?](http://stackoverflow.com/a/2424824/) – BalusC Apr 13 '13 at 17:24
  • possible duplicate of [How to upload files to server using JSP/Servlet?](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet) – dunni Apr 13 '13 at 18:36
  • Storing images or any other file type to database is actually not that bad idea. One professional electronics design application is keeping entire design project information (schematics, circuit boards, simulations, mechanical designs etc.) in a Microsoft Access database file which even supports team work. – Cebence Apr 13 '13 at 18:48

1 Answers1

0

You can't do this in a web application:

File img = new File(request.getParameter("rfile"));

From the exception message you can see that your application is trying to locate the file in its current folder. Which is not the same as the location of the image you are adding, especially if the web application is running on a different computer than your web browser.

CLARIFICATION: If your webapp is running on the same computer as your web browser submitting full file path (c:\my_images\photo_of_me.jpg) will work because the JVM can access it. But only via a FULL file name. If your webapp is on another computer the only way to add a file is to accept a file upload by using Commons FileUpload.

What you probably meant to do is to "upload" the image and save it to your database. For that you need to use Apache Commons FileUpload.

Cebence
  • 2,406
  • 2
  • 19
  • 20
  • thanks 4 the answer but the above code works fine when i tried to add the file. I try using Apache Commons FileUpload. – Shubhanshu Apr 13 '13 at 18:30