将图像上传到数据库但获得异常

时间:2015-07-30 05:21:46

标签: java jsp servlets

我想将图片上传到DB。我为此目的使用java servlet。但我有一个例外

HTTP Status 500 - Unable to process parts as no multi-part configuration has been provided

我的jsp页面:

<form action="uploadservlet" method="post"
              enctype="multipart/form-data">
           <input type="file" name="photo" size="50"/>
            <br />
            <input type="submit" value="Upload File" />
        </form>

我的servlet post方法是

public void doPost(HttpServletRequest request, 
                   HttpServletResponse response)
                  throws ServletException, java.io.IOException {
        InputStream inputStream = null; // input stream of the upload file

            // obtains the upload file part in this multipart request
            Part filePart = request.getPart("photo");
            if (filePart != null) {
                // prints out some information for debugging
                System.out.println(filePart.getName());
                System.out.println(filePart.getSize());
                System.out.println(filePart.getContentType());

                // obtains input stream of the upload file
                inputStream = filePart.getInputStream();
            }
       }

1 个答案:

答案 0 :(得分:0)

需要使用@MultipartConfig(即javax.servlet.annotation.MultipartConfig)注释servlet。 @MultipartConfig注释允许servlet处理使用multipart/form-data MIME类型发出的请求。另一种选择是使用&#34; multipart-config&#34;标记DispatcherServlet。 web.xml中的部分

您还可以查看提供更大灵活性的commons-fileupload