【问题标题】:how to fix the request doesn't contain a multipart/form-data or multipart/mixed stream apache error如何修复请求不包含多部分/表单数据或多部分/混合流 apache 错误
【发布时间】:2017-01-14 08:45:32
【问题描述】:

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

HTTP 状态 500 - org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException:请求不包含 multipart/form-data 或 multipart/mixed 流,内容类型标头为 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

提前致谢:)

【问题讨论】:

    标签: java forms apache file-upload


    【解决方案1】:

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

    【讨论】:

    • 为什么?你能详细说明一下吗?
    猜你喜欢
    • 2015-09-28
    • 2016-02-29
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2016-12-17
    • 2012-03-16
    相关资源
    最近更新 更多