如何修复请求不包含multipart / form-data或multipart / mixed stream apache错误

时间:2016-09-06 19:45:07

标签: java forms apache file-upload

我有一个表格,其中我有type="file"type="submit",现在我需要将所选文件发送到服务器,但问题是每当我提交表单时我都会得到一个巨大的apache错误说:

  

HTTP状态500 - org.apache.tomcat.util.http.fileupload.FileUploadBase $ InvalidContentTypeException:请求不包含multipart / form-data或multipart / mixed流,内容类型标题是application / x-www - 形式进行了urlencoded

这是我的客户端脚本:

<form method="POST" action="../propicuploader">
            <div class="browsephoto_div">
                <label class="takeapicture_btn">
                    <input type="file" accept=".png, .gif, .jpeg, .jpg" id="imagetoupload" enctype="multipart/form-data" onchange="document.getElementById('myImg').src = window.URL.createObjectURL(this.files[0])" required/>
                    <span>Browse</span>
                </label>
            </div>
            <div class="ProfilePicsubmit_div">
                <input type="submit" class="Profilpicsubmit_btn" value="Next"/>
            </div>
        </form>

我的服务器端脚本:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    String description = request.getParameter("description"); // Retrieves <input type="text" name="description">
    Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
    String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
    InputStream fileContent = filePart.getInputStream();
    File file = new File("D:\\image123.jpg");
    file.createNewFile();
    OutputStream stream = new DataOutputStream(new FileOutputStream(file));
    IOUtils.copy(fileContent,stream);
}

我正在使用apache v9.0 and java EE 8

提前致谢:)

1 个答案:

答案 0 :(得分:0)

在表单中使用enctype =“multipart / form-data”