package com.bowintek.practice.controller.system; import com.bowintek.practice.AppConfig; import com.bowintek.practice.filter.exception.BaseResponse; import com.bowintek.practice.filter.exception.RespGenerstor; import com.bowintek.practice.model.CfFile; import com.bowintek.practice.services.service.AccountService; import com.bowintek.practice.services.service.system.FileService; import com.bowintek.practice.util.BASE64DecodedMultipartFile; import com.bowintek.practice.vo.system.FileModel; import io.netty.handler.codec.base64.Base64Encoder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Base64; import java.util.List; @RestController @RequestMapping("/api/system/file") public class FileController { @Autowired private FileService fileService; @Autowired private AccountService accountService; @Autowired private AppConfig appConfig; @GetMapping("getList") public BaseResponse> getList(@RequestParam("fileRefID") String fileRefID) { return RespGenerstor.success(fileService.getList(fileRefID)); } @PostMapping("upload") public BaseResponse upload(MultipartFile[] files, String fileRefId, Integer fileType, Integer isSingle) throws Exception { return RespGenerstor.success(fileService.uploadToLocal(files[0], fileRefId, fileType, accountService.getLoginUserID(), isSingle)); } @PostMapping("uploadBase64") public BaseResponse uploadBase64(@RequestParam(required = false) String base64Str, @RequestParam(required = false) String fileRefId, @RequestParam(required = false) Integer fileType, @RequestParam(required = false) Integer isSingle) throws Exception { MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(base64Str); MultipartFile[] files = new MultipartFile[]{multipartFile}; return RespGenerstor.success(fileService.saveFile(files, fileRefId, fileType, accountService.getLoginUserID(), isSingle)); } @GetMapping("downFile") public void downFile(HttpServletResponse response, @RequestParam("fileId") String fileId) throws IOException { OutputStream os = response.getOutputStream(); fileService.downFtpFile(fileId, os); if (null != os) { os.flush(); os.close(); } } @GetMapping("downloadTempFile") public void downloadTempFile(HttpServletResponse response, @RequestParam("fileName") String fileName) throws IOException { OutputStream os = response.getOutputStream(); fileService.downloadTempFile(fileName, os); if (null != os) { os.flush(); os.close(); } } @GetMapping("delete") public BaseResponse delete(@RequestParam("fileId") String fileId) throws IOException { return RespGenerstor.success(fileService.delete(fileId)); } @GetMapping("getFileBase64") public BaseResponse getFileBase64(HttpServletResponse response, @RequestParam("fileId") String fileId) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); fileService.downFtpFile(fileId, bos); byte[] bytes = bos.toByteArray(); Base64.Encoder encoder = Base64.getEncoder(); String base64String = encoder.encodeToString(bytes); bos.close(); return RespGenerstor.success(base64String); } @GetMapping("getFileByte") public BaseResponse getFileByte(HttpServletResponse response, @RequestParam("fileId") String fileId) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); fileService.downFtpFile(fileId, bos); bos.close(); return RespGenerstor.success(bos.toByteArray()); } @GetMapping("downFileToLocal") public BaseResponse downFileToLocal(@RequestParam("fileId") String fileId) throws IOException { return RespGenerstor.success(fileService.downFtpFileToLocal(fileId)); } @GetMapping("downFileToUrl/{fileId}") public void downFileToUrl(HttpServletResponse response, HttpServletRequest request, @PathVariable("fileId") String fileId) throws IOException { OutputStream os = response.getOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { fileService.downFtpFile(fileId, bos); CfFile file = fileService.getFileByID(fileId); response.setContentType("application/force-download");// 设置强制下载不打开 response.setContentType("multipart/form-data;charset=UTF-8"); response.setHeader("Content-Length", String.valueOf(bos.toByteArray().length)); response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(file.getFileName(), "UTF-8"));// 设置文件名 os.write(bos.toByteArray()); } catch (Exception ex) { } bos.close(); if (null != os) { os.flush(); os.close(); } } }