【问题标题】:what JAR contains getPart() method?什么 JAR 包含 getPart() 方法?
【发布时间】:2014-03-21 04:51:41
【问题描述】:

我正在尝试将文件从 apache 7.0 服务器上传到 mySQL 数据库。我已经编写了html代码来上传文件。

<form action="UploadValidate.jsp" method="post" enctype="multipart/form-data">
    <fieldset>
    <legend><font size="4" color="white">File Upload</font></legend><br/><br/>
    <font size="4" color="white"><b>
    <h3> Select File to Upload</h3> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    <input type="file"name="file" /><br/><br/>

    <center>
    <input type="submit" value="Upload File" /><br/><br/></center>

    </font>
    </fieldset>
</form>

这是 UploadValidate.jsp

      <%@ page import="java.io.*,java.sql.*,java.lang.String.*,com.oreilly.servlet.*"%>
      <%
       InputStream inputstream=null;
       String str=request.getParameter("file");
       Part filePart=request.getPart(str);
       out.println(filePart);
       if(filePart!=null){
           out.println(filePart.getName());
           out.println(filePart.getSize());
           out.println(filePart.getContentType());
           //output the inputstream of uploaded file
           inputstream=filePart.getInputStream();
        }
       else{
       out.println("cannot execute if condition");
        }
      %>
     <%
    try{
    String message=null;
    int id=123;
    String url="jdbc:mysql://localhost:3306/Project";
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection(url,"root","admin");
    String sql="INSERT INTO uploadfile(id,file) VALUES(?,?)";
    PreparedStatement stmt=con.prepareStatement(sql);
    stmt.setInt(1,id);
    if(inputstream!=null){
        stmt.setBlob(2,inputstream);
    }
    int row=stmt.executeUpdate();
    if(row>0){
        out.print("<h3><font color=red> Success Welcome!!!!!<br><br> </font></h3>");
    }
}catch(Exception e){
    e.printStackTrace();
}
//session.setAttribute("Message",message);
//response.sendRedirect("Message.jsp");
   %>

即使我选择了要上传的文件,filePart 对象仍返回 null。如果 bolck 则终止并返回 “null 无法执行 if 条件”作为输出。

【问题讨论】:

  • 这不是一个好主意。 JSP 中的 Scriptlet 代码在 1999 年过时了。不要这样做。调试器会快速告诉你哪里出错了。
  • 为什么要重新发明轮子?网上有很多例子。例如,参见corejavaexample.blogspot.fr/2013/04/…,您会看到如何处理“multipart/form-data

标签: java mysql jsp


【解决方案1】:

要回答实际问题,您得到空返回值的原因有两个。一、代码:

   String str=request.getParameter("file");
   Part filePart=request.getPart(str);

应该是

   Part filePart = request.getPart("file");

但是,至关重要的是,request.getPart 方法是一个 Servlet 3.0 特性,它需要使用带有 @MultipartConfig 注释的 servlet(不是 JSP 文件)。

但是,代码中充斥着不好的做法。

正如 cmets 中所述,十多年来一直不鼓励使用 scriptlet;您应该首先使用 servlet 作为某种控制器,然后使用以下方式(在内部)转发到您的 JSP 视图:

request.getRequestDispatcher("/WEB-INF/jsp/view.jsp").forward(request, response);

&lt;font&gt;&lt;center&gt; HTML 元素也已被弃用多年。

最后,有很多库可以帮助上传文件。例如,Apache Commons FileUpload

Here is a good example 了解如何将以上所有内容放在一起。

【讨论】:

  • 我曾尝试使用 Part filePart = request.getPart("file");我无法得到结果。它返回 null。
  • 我已经编辑了我的答案来解释为什么request.getPart("file") 也不起作用。您确实需要将该代码移出 JSP 并移入 Servlet。如果您真的想将它保存在 JSP 中,那么您需要使用 Apache Commons FileUpload 来处理上传。
猜你喜欢
  • 2015-02-04
  • 2012-08-18
  • 1970-01-01
  • 2021-11-27
  • 2016-08-20
  • 2015-11-16
  • 2016-01-25
  • 2015-11-04
  • 1970-01-01
相关资源
最近更新 更多