Java 请求没有';不包含多部分/表单数据或多部分/混合流apache错误

Java 请求没有';不包含多部分/表单数据或多部分/混合流apache错误,java,html,apache,file-upload,Java,Html,Apache,File Upload,我有一个表单,其中有一个type=“file”和一个type=“submit”,现在我需要将所选文件发送到服务器,但问题是,每当我提交表单时,我都会收到一个巨大的apache错误,上面说: HTTP状态500-org.apache.tomcat.util.HTTP.fileupload.FileUploadBase$InvalidContentTypeException:请求不包含多部分/表单数据或多部分/混合流,内容类型标头为application/x-www-form-urlencoded

我有一个表单,其中有一个
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);
}

浏览
我的服务器端脚本:

<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);
}
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
PrintWriter out=response.getWriter();
String description=request.getParameter(“description”);//检索
Part filePart=request.getPart(“文件”);//检索
字符串文件名=路径.get(filePart.getSubmittedFileName()).getFileName().toString();//MSIE修复。
InputStream fileContent=filePart.getInputStream();
File File=新文件(“D:\\image123.jpg”);
createNewFile();
OutputStream=新数据输出流(新文件输出流(文件));
复制(文件内容、流);
}

我使用的是ApacheV9.0和JavaEE8,您必须在表单声明中添加enctype=“multipart/formdata” 例子:


如果需要更多信息:

您必须向表单声明中添加enctype=“多部分/表单数据” 例子:

如果您想了解更多信息: