我正在尝试上传文件以及文本字段。尽管据说我无法通过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