文件下载:
@RestController
@Api(description = “下载文件接口”)
public class FileDownloadController {
@ApiOperation("下载文件")
@GetMapping("/api/ResourceFile/DownloadDateSetFile")
public CommonRlt download(HttpServletResponse response, @Param("fileName") String fileName) {
String parentPath = "D:"+File.separator+"DownloadFiles";
//通过response输出流将文件传递到浏览器
//构建一个文件通过Paths工具类获取一个Path对象
Path fileUrl = Paths.get(parentPath, fileName);
System.out.println(fileUrl);
//判断文件是否存在
if (Files.exists(fileUrl)) {
//存在则下载
//通过response设定他的响应类型
//获取文件的后缀名
String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
//设置contentType ,只有指定contentType才能下载
response.setContentType("application/" + fileSuffix);
//添加http头信息
//因为fileName的编码格式是UTF-8 但是http头信息只识别 ISO8859-1 的编码格式
//因此要对fileName重新编码
try {
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//使用Path 和response输出流将文件输出到浏览器
try {
Files.copy(fileUrl, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
return new CommonRlt(true,200,"下载成功",fileUrl);
}
}
测试:localhost:8080//api/ResourceFile/DownloadDateSetFile?fileName=aaa.txt
2.单文件上传
@RestController
@CrossOrigin
@Api(description = “上传单个文件接口”)
public class FileUploadController {
@PostMapping("/api/Upload/UploadFile")
@ApiOperation(value = "判断条件后上传单个文件")
public CommonRlt upload(@RequestParam("uploadFile") MultipartFile uploadFile, HttpServletRequest req) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM/");
int maxSize = 1024*1024*4; //上传最大为4MB
if (uploadFile.getSize()>maxSize) {
throw new IllegalArgumentException("文件过大,无法上传");
}
// 上传的文件将保存在项目运行目录下的 UploadFiles 文件夹,
String realPath = req.getSession().getServletContext().getRealPath("/UploadFiles/");
System.out.println(realPath);
// 并且在 uploadFile 文件夹中通过日期对上传的文件归类保存
String format = sdf.format(new Date());
File folder = new File(realPath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
}
// 对上传的文件重命名,避免文件重名
String fileName = uploadFile.getOriginalFilename();
String newName = new SimpleDateFormat("yyyyMMddhhmmssSSSS").format(new Date()).toString()+
fileName.substring(fileName.lastIndexOf("."), fileName.length());
String suffix=fileName.substring(fileName.lastIndexOf("."));//文件后缀
if (!(suffix.equals(".jpg"))) {
throw new IllegalArgumentException("只能上传jpg格式");
}
try {
// 文件保存
uploadFile.transferTo(new File(folder, newName));
// 返回上传文件的访问路径
String fileUrl = req.getScheme() + "://" + req.getServerName() + ":"
+ req.getServerPort() + "/UploadFiles/" + format + newName;
System.out.println(fileUrl);
return new CommonRlt(true,200,"上传成功",fileUrl);
} catch (IOException e) {
e.printStackTrace();
}
return new CommonRlt(false,0,"上传失败",null);
}
}
前端:
3.多文件上传
@RestController
@CrossOrigin
@Api(value = “上传多个文件接口”)
public class MultiFileUploadController {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM/");
@PostMapping("/api/Upload/UploadMultipleFile")
public CommonRlt upload(@RequestParam("uploadFiles") MultipartFile[] uploadFiles, HttpServletRequest req,
@Param("fileUrl")String fileUrl, @Param("fileName") String fileName ) {
String result = "";
int v_i=1;
// 遍历所有上传的文件
for (MultipartFile uploadFile : uploadFiles) {
int maxSize = 1024*1024*4; //上传最大为4MB
if (uploadFile.getSize()>maxSize) {
throw new IllegalArgumentException("文件过大,无法上传");
}
// 上传的文件将保存在项目运行目录下的 uploadFile 文件夹
String realPath = req.getSession().getServletContext().getRealPath("/UploadFiles/");
if (v_i>1){
realPath=realPath+"/";
}
v_i+=v_i+1;
// 并且在 uploadFile 文件夹中通过日期对上传的文件归类保存
String format = sdf.format(new Date());
File folder = new File(realPath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
}
// 对上传的文件重命名,避免文件重名
fileName = uploadFile.getOriginalFilename();
String newName = new SimpleDateFormat("yyyyMMddhhmmssSSSS").format(new Date()).toString()+
fileName.substring(fileName.lastIndexOf("."), fileName.length());
String suffix=fileName.substring(fileName.lastIndexOf("."));//文件后缀
if (!(suffix.equals(".jpg"))) {
throw new IllegalArgumentException("只能上传jpg格式");
}
try {
// 文件保存
uploadFile.transferTo(new File(folder, newName));
// 获取上传文件的访问路径
fileUrl = req.getScheme() + "://" + req.getServerName() + ":"
+ req.getServerPort() + "/UploadFiles/" + format + newName;
result += fileUrl + " ";
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回所有上传文件的访问路径
return new CommonRlt(true,200,"上传成功",result);
}
}
多文件上传前端: