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


  HTTP状态500-org.apache.tomcat.util.http.fileupload.FileUploadBase $ InvalidContentTypeException:请求不包含多部分/表单数据或多部分/混合流,内容类型标头为application / x-www-form- 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

最佳答案

您必须在表单声明中添加enctype =“ multipart / form-data”
例:
    <form method="POST" action="../propicuploader" enctype="multipart/form-data" >

如果您需要更多信息:What does enctype='multipart/form-data' mean?

关于java - 该请求不包含多部分/表单数据或多部分/混合流apache错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39357960/

10-10 03:58