我正在尝试通过从用户获取文件来将图像文件插入 MySql DB。在插入文件时,代码工作正常,但在将文件转换为 Blob 格式时,我得到一个java.io.FileNotFoundException
这是我的 JSP 代码:
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);
异常报告如下:
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);
紧随其后的是:
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)