【发布时间】: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