0

我在下面创建了程序,当我按下提交按钮时程序抛出 FileNotFound 异常。由于 JSP 页面无法找到图像的完整路径,问题就来了。我调试了 JSP 程序,发现 HTML 表单只传递图像名称而没有路径,这就是问题来的原因。任何人都可以解决这个问题。

##################  SQL Query ######################################

    CREATE TABLE IMAGEMAIN(ID INTEGER,IMAGE BLOB) ;

##################  HTML  Form ######################

     <form name="frm" method="post" action="index.jsp">
     <input type="text" name="hint">
     <input type="file" name="user_file">
     <input type="submit">

################### JSP PAGE ########################

try
{ 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("Connection loaded");
    Connection con = DriverManager.getConnection("jdbc:odbc:project","image","image");
    System.out.println("Connection created");
    String ll=request.getParameter("user_file");
    String lo=request.getParameter("hint");
    File imgfile = new File(ll);

    FileInputStream fin = new FileInputStream(imgfile);

    PreparedStatement pre = con.prepareStatement("insert into IMAGEMAIN (id,image) values(?,?)");
    pre.setString(1,lo);
    pre.setBinaryStream(2,fin,(int)imgfile.length());
    pre.executeUpdate();
    pre.close();
}

catch(Exception E)
{
    out.println("the eror is  "+ E);
}
4

1 回答 1

0

您正在使用的构造函数FileInputStream(String)需要一个文件名,这将不起作用,因为在 HTTP 上传的文件中没有这样的文件名 - 相反,您可以直接操作流。

根据这个 SO QA(jsp 文件上传问题),JSP 不提供处理多部分 HTTP 请求的内置支持,因此如果不使用额外的 Java 包(例如Apache Commons FileUpload ),您将无法处理上传的文件。

所以我建议你安装 FileUpload,然后用它来访问上传的文件。我不熟悉它,但这里有文档:http: //commons.apache.org/proper/commons-fileupload/using.html

于 2013-08-19T14:04:33.293 回答