我有一個表格,其中我有type="file"
和type="submit"
,現在我需要將所選文件發送到服務器,但問題是,每當我提出我的形式,我得到了巨大的Apache的錯誤說:該請求不包含multipart/form-data或多部分/混合流apache錯誤
HTTP狀態500 - $ org.apache.tomcat.util.http.fileupload.FileUploadBase InvalidContentTypeException:請求不包含多/表格數據或多部分/混合流,內容類型頭是應用程序/ 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