123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- 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<List<FileModel>> getList(@RequestParam("fileRefID") String fileRefID) {
- return RespGenerstor.success(fileService.getList(fileRefID));
- }
- @PostMapping("upload")
- public BaseResponse<Integer> 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<Integer> 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<Integer> delete(@RequestParam("fileId") String fileId) throws IOException {
- return RespGenerstor.success(fileService.delete(fileId));
- }
- @GetMapping("getFileBase64")
- public BaseResponse<String> 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<String> 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<String> 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();
- }
- }
- }
|