1、前端的form表单:
<form id="form" action="controller层的多文件上传方法访问路径" method="post" enctype="multipart/form-data">
<input type="file" name="file" multiple="multiple" >
</form>
<a href="controller层的文件下载方法访问路径">下载</a>
2、在你的SRC-> main-> wabapp新建文件夹:
例:uploadfile
3、多文件上传Controller层方法
①、public void uploadfile(HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file = null;for (MultipartFile multipartFile : files) {
file = multipartFile;
if (!file.isEmpty()) {
String fileName = file.getOriginalFilename();
String filePath = request.getSession().getServletContext().getRealPath("uploadfile/");
try {
uploadFile(file.getBytes(), filePath, fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
②、public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath + fileName);
out.write(file);
out.flush();
out.close();
}
4、文件下载Controller层方法
①、public void down(HttpServletRequest request, HttpServletResponse response) throws Exception {
String fileName =“文件名”;
String filePhth = request.getSession().getServletContext().getRealPath("\\uploadfile") + "//"
+ fileName;
fileName = URLEncoder.encode(fileName, "UTF-8");// 设置文件下载头
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// 设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
downloadFile(fileName, filePhth, out);
}
}
②、public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath + fileName);
out.write(file);
out.flush();
out.close();
}
}