文件上传
代码语言:javascript复制application.properties 文件设置 (默认设置 1MB)
#设置上传文件的大小
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.maxRequestSize=100MB
public String uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
if (file == null || file.isEmpty()) {
throw new Exception("未选择需上传的文件");
}
String filePath = new File("./apiFile").getAbsolutePath();
File fileUpload = new File(filePath);
if (!fileUpload.exists()) {
fileUpload.mkdirs();
}
String fileName = serviceUuid file.getOriginalFilename();
fileUpload = new File(filePath, fileName);
//判断文件是否存在,如果存在
if (fileUpload.exists()) {
//首先删除本地文件
fileUpload.delete();
}
try {
//将文件上传至本地
file.transferTo(fileUpload);
return "上传成功"; //ResponseData.success("上传成功");
} catch (IOException e) {
throw new Exception("上传文件到服务器失败:" e.toString());
}
}
文件下载
代码语言:javascript复制application.properties 文件设置 (默认设置 1MB)
#设置上传文件的大小
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.maxRequestSize=100MB
public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("serviceUuid") String serviceUuid) throws Exception {
if (file == null || file.isEmpty()) {
throw new Exception("未选择需上传的文件");
}
String filePath = new File("./apiFile").getAbsolutePath();
File fileUpload = new File(filePath);
if (!fileUpload.exists()) {
fileUpload.mkdirs();
}
String fileName = serviceUuid file.getOriginalFilename();
fileUpload = new File(filePath, fileName);
//判断文件是否存在,如果存在
if (fileUpload.exists()) {
//首先删除本地文件
fileUpload.delete();
}
try {
//将文件上传至本地
file.transferTo(fileUpload);
return "上传成功"; //ResponseData.success("上传成功");
} catch (IOException e) {
throw new Exception("上传文件到服务器失败:" e.toString());
}
}