我有一个表格,其中我有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
答案 0 :(得分:0)
你必须添加表格声明enctype =“multipart / form-data”
例:
的 <form method="POST" action="../propicuploader" enctype="multipart/form-data" >
强>