我正在嘗試上傳文件以及文本字段。儘管據說我無法通過request.getParameter("remarks");
檢索文本字段的值,但我導入了commons-fileupload
。雖然我似乎無法弄清楚什麼是錯我的代碼,其中,它在這一行List<FileItem> items = upload.parseRequest(request);
一個錯誤說SEVERE: Servlet.service() for servlet [SubmitResult] in context with path [/HIS] threw exception [Servlet execution threw an exception] with root cause java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
使用多部分表單數據和文本字段上傳文件
HTML:
<div class="modal-body">
<input type="hidden" name="orderid2" id="orderid2">
<table>
<tr>
<td>
Remarks:
</td>
<td>
<input type="text" name="remarks" placeholder="Remarks" autocomplete="off" class="form-control placeholder-no-fix"> <br>
</td>
</tr>
<tr>
<td>
Upload File:
</td>
<td>
<input type="file" name="file" id="file" accept="image/png, .txt, application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/msword"/> <br>
</td>
</tr>
</table>
</div>
的Servlet:
String rtempfile = File.createTempFile("temp", "1").getParent();
MultipartRequest multi = new MultipartRequest(request, rtempfile, 15*1024*1024);
Enumeration files = multi.getFileNames();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while(iter.hasNext()) {
FileItem item = iter.next();
if(item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
if(name.equals("orderid2"))
order.setOrderID(Integer.parseInt(value));
else if(name.equals("remarks"))
order.setRemarks(value);
}
}
orderDAO.submitResult(order, multi, files);
似乎您沒有將Commons IO jar包含到您的項目中,只需下載幷包含它們即可。 http://commons.apache.org/proper/commons-io/download_io.cgi – Parth
@Parth謝謝!這解決了錯誤 – nubteens