对于HttpServlet,未定义getPart(String)

时间:2013-12-04 23:09:02

标签: javascript jsp servlets file-upload request

html代码:

                <td>Select File : </td>
            <td><input  name="file" type="file"/> </td>
        </tr>
        <tr>
            <td>Enter Filename : </td>
            <td><input type="text" name="photoname" size="20"/> </td>
        </tr>

servlet代码:

Part p1 =  request.getPart("file"); //1
InputStream is = p1.getInputStream();

Part p2  = request.getPart("photoname");//2
Scanner s = new Scanner(p2.getInputStream());
String filename = s.nextLine();   

但是在以下几点我得到错误: 1.Part p1 = request.getPart(“file”); 2.Part p2 = request.getPart(“photoname”);

1 个答案:

答案 0 :(得分:0)

在HttpServletRequest中,getPart未在Java EE 5中实现。您至少需要Java EE 6.您可能需要升级servlet容器。

比较http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletRequest.html http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

(例如,如果您安装了Tomcat 5,请移至Tomcat 7或8。)

或者只使用Apache Commons File Upload。要了解如何操作,请参阅How to upload files to server using JSP/Servlet?