1

我制作了一个 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,但只得到了shock.jpg而不是完整的filepath. 也许完整filepath的没有到来,这就是找不到文件错误的原因。那么我怎样才能发送完整的filepath呢?

4

2 回答 2

2

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

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

实现该doPost方法以获取相同的文件。您可能想看看Apache Commons FileUpload to upload files 和 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();
}

现在您必须根据需要解析文件内容。请参阅博文上传和存储文件。但是,我强烈建议使用Apache Commons FileUpload

于 2012-07-30T08:05:13.300 回答
0

它看起来像一个本地错误,因为它真的找不到url. 我建议通过打印出urlString 并创建一个虚拟类来为该doGet方法提供手工请求/响应来进行调试,以确保问题出在程序本身还是在请求的某些意外传递/格式中(您可能想要评论为此方法的某些部分,例如连接和语句部分)。

编辑:虚拟类(或方法,在这种情况下)的示例:

private void testDoGet() {
    // I would suggest commenting out all the Connection and PreparedStatement
    // parts of the doGet method so you don't have to establish the connection.
    // - this is just to test if you can get to the image on your machine.
    HttpServletRequest request;
    //insert into request the image parameter with the string to the requested image
    HttpServletResponse response //TODO initialize with some class implementing it
    doGet(request, response);
    // if you want to, set a breakpoint somewhere here to check 
    // what's in the classes now        
}
于 2012-07-30T08:16:57.903 回答