FileNotFoundException将图像上载到Oracle数据库

时间:2012-07-30 07:50:44

标签: exception servlets filenotfoundexception servletexception ojdbc

我制作了一个servlet程序,将图像插入到Oracle数据库中。该计划如下。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InsertImage extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String url=request.getParameter("image");
        File image=new File(url);
        FileInputStream fis;
        PrintWriter pw=response.getWriter();
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String str = "jdbc:oracle:thin:@localhost:1521:XE";
            Connection con = DriverManager.getConnection(str,"system","root");
            PreparedStatement pstmt=con.prepareStatement("insert into insertimage(image) values(?)");
            fis = new FileInputStream(image);
            pstmt.setBinaryStream(1, (InputStream)fis, (int)(image.length()));
            int size=pstmt.executeUpdate();
            if(size>0)
            {
                pw.println("<html>Image Uploaded Successfully.</html>");
            }
            else
            {
                pw.println("<html>Image could not be uploaded.</html>");
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
}

输入来自的HTML页面是:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>
        <form action="InsertImage" name="form1">
            INSERT IMAGE
            <input type="file" name="image"></input>
            <input type="submit" name="upload"></input>
        </form>
    </body>
</html>

当我尝试从HTML页面运行此代码时,无论我提供什么图片输入,它总是抛出FileNotFoundException。我无法理解为什么我会这样做。 stacktrace是:

java.io.FileNotFoundException: Counter-Strike-Servers.jpg (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at InsertImage.doGet(InsertImage.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

我尝试在servlet中打印URL并且只获得 shocked.jpg 而不是完整的filepath。也许完整的filepath未来,这是找不到文件错误的原因。那么如何发送完整的filepath

2 个答案:

答案 0 :(得分:2)

从JSP / HTML上传文件时,您必须将表单方法设置为POST,并将encType设置为multipart/form-data。 (HTTP specification

<form action="InsertImage" method="post" encType="multipart/form-data" name="form1">

实施doPost方法以获取相同的文件。您可能需要查看Apache Commons FileUpload上传文件和Stack Overflow post How to upload files to server using JSP/Servlet? 以获取更多详细信息。

我尝试了一种方法来上传文件,而不使用Apache Common FileUpload,它正在运行。

HTML:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    </head>
    <body>
        <form action="InsertImage" name="form1" method="post" enctype="multipart/form-data">
            INSERT IMAGE
            <input type="file" name="image"></input>
            <input type="submit" name="upload"></input>
        </form>

        </body>
        </html>

    </body>
</html>

Servlet doPost:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String url=request.getParameter("image");
    InputStream is = request.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
    String line = null;
    PrintWriter pw=response.getWriter();
    pw.println("Reading file");
    while ((line = reader.readLine()) != null) {
         pw.println(line);
    }
    pw.flush();
}

现在您必须根据需要解析文件内容。请参阅博客文章 Upload and store files 。但是,我强烈建议使用 Apache Commons FileUpload

答案 1 :(得分:0)

它看起来像是一个本地错误,它确实无法找到url指定的文件。我建议通过打印url字符串并创建一个虚拟类来为调试提供一个手工制作的请求/响应,以确保问题是在程序本身还是在一些意外的传递中格式化请求(您可能希望为此注释掉方法的某些部分,例如连接和语句部分)。

编辑:虚拟类(或方法,在本例中)的示例:

doGet