jfinal利用form表单上传多个文件

一:页面

<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);
}

三:结果



猜你喜欢

转载自blog.csdn.net/m0_37934074/article/details/78872981