Apache commons文件上传损坏的文件

时间:2013-02-05 15:16:28

标签: file-upload apache-commons-fileupload

我有一个应该处理上传文件的servlet。 Apache公共文件上传库用于此目的。

问题是文件已损坏。看起来像符号"〜"被替换为"?"在文件中。

这里我的html表单是:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
</head>
<body>

<FORM action="http://localhost:8081/wihome-connector-bpm/bpmFileUpload"
      enctype="multipart/form-data"
      method="post">

        What files are you sending?
        <INPUT type="file" name="uploadedFiles"><BR>

        <INPUT type="submit" value="Send">

        <INPUT type="reset">

</FORM>


</body>
</html>

那就是servlet:

public class FileUploadServlet extends HttpServlet {

    private static final Log LOG = LogFactory.getLog(FileUploadServlet.class);

    /**
     * {@inheritDoc}
     */
    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {


        try {
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            if (isMultipart) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);

                List items = upload.parseRequest(request);

                Iterator iter = items.iterator();
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    if (!item.isFormField()) {
                        LOG.info("Uploading file: " + item.getName());
                        byte[] fileContents = IOUtils.toByteArray(item.getInputStream());
                        //...
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

我正在尝试上传图片enter image description here

但我得到了:

enter image description here

你能帮帮我吗?可能是什么问题?

1 个答案:

答案 0 :(得分:0)

好的,过滤器出现了问题。 Project有一个过滤器,它在丰富的servlet之前损坏了请求。