一:页面
<form action="/users/upload" method="post"
enctype="multipart/form-data">
File:<input type="file" name="f1"><br> File:<input
type="file" name="f1"><br> File:<input type="file"
name="f1"><br> <input type="submit">
</form>
二:代码
public void upload() throws UnsupportedEncodingException {
HttpServletRequest req = getRequest();
req.setCharacterEncoding("UTF-8");
int userId = 888999;
DiskFileItemFactory disk = new DiskFileItemFactory();
File ff = new File("d:");
if (ff.exists()) {
disk.setRepository(ff);// 设置临时目录,用于保存临时文件
}
ServletFileUpload upload = new ServletFileUpload(disk);
try {
List<FileItem> list = upload.parseRequest(req);
List<Map<String, Object>> files = new ArrayList<>();
String path = req.getServletContext().getRealPath("/users");
for (int i = 0; i < list.size(); i++) {
FileItem item = list.get(i);
String name = "";
if (i == 0) {
name = userId + "cardfront.jpg";
} else if (i == 1) {
name = userId + "cardbehi.jpg";
} else if (i == 2) {
name = userId + "cardspeci.jpg";
} else {
name = userId + "2.jpg";
}
item.write(new File(path, name));
item.delete();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
renderJson(123);
}
三:结果
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。