https://gitee.com/dafran/file-up-down.git
配置
server:port: 8899spring:servlet:multipart:# 最大支持文件大小max-file-size: 1024MB# 最大支持请求大小max-request-size: 1024MB#文件存储路径filepath: F:/open/
实体类
@Data@AllArgsConstructor@NoArgsConstructorpublic class Resp<E> {/*** 状态码*/private String code;/*** 消息*/private String message;/*** 特殊类型*/private E body;/*** 成功* @param body* @param <E>* @return*/public static <E> Resp<E> success(E body) {return new Resp("200","",body);}/*** 失败* @param code* @param message* @param <E>* @return*/public static <E> Resp<E> fail(String code,String message) {return new Resp(code,message,(Object) null);}}
Service
public interface FileService {Resp<String> upload(MultipartFile file);Resp<String> download(HttpServletResponse response) throws UnsupportedEncodingException;}
ServiceImpl
@Servicepublic class FileServerImpl implements FileService {@Overridepublic Resp<String> upload(MultipartFile file) {// 判断文件是否为空if (file.isEmpty()) {return Resp.fail("400","文件为空!");}// 原始文件名,新文件名,文件路径(反斜杠两个第一个为转义),目标文件String originalFilename = file.getOriginalFilename();String fileName = System.currentTimeMillis()+"."+originalFilename.substring(originalFilename.lastIndexOf(".") + 1);String filePath = "F:\\open\\";File dest = new File(filePath+fileName);// 没有文件夹,则创建if (!dest.getParentFile().exists()) {dest.getParentFile().mkdirs();}// 防止硬盘爆满或者其他情况导致失败try {file.transferTo(dest);} catch (Exception e) {e.printStackTrace();return Resp.fail("500",originalFilename + "上传失败!");}return Resp.success(fileName);}@Overridepublic Resp<String> download(HttpServletResponse response) throws UnsupportedEncodingException {String fileName="1619692787751.pdf";String filePath = "F:\\open\\";File dest = new File(filePath+fileName);// 如果没有对应文件if (dest.exists()) {response.setContentType("application/vnd.ms-excel;charset=UTF-8");response.setCharacterEncoding("UTF-8");// response.setContentType("application/force-download");response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(fileName,"UTF-8"));byte[] buffer = new byte[1024];FileInputStream fis = null; //文件输入流BufferedInputStream bis = null;OutputStream os = null; //输出流try {os = response.getOutputStream();fis = new FileInputStream(dest);bis = new BufferedInputStream(fis);int i = bis.read(buffer);while(i != -1){os.write(buffer);i = bis.read(buffer);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("----------file download---" + fileName);try {bis.close();fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return Resp.fail("501","没有对应文件");}}
Controller
@RestController@RequestMapping(value = "/file")public class FileController {@Autowiredprivate FileService fileService;@RequestMapping(value = "/upload",method = RequestMethod.POST)private Resp<String> upload(@RequestParam("file")MultipartFile file) {return fileService.upload(file);}@RequestMapping(value = "/download",method = RequestMethod.GET)private Resp<String> download(HttpServletResponse response) throws UnsupportedEncodingException {return fileService.download(response);}}
