|
| 1 | +/** |
| 2 | + * @Project: |
| 3 | + * @Author: leegoo |
| 4 | + * @Date: 2021年11月16日 |
| 5 | + */ |
| 6 | +package cn.withmes.file.system; |
| 7 | + |
| 8 | +import lombok.Builder; |
| 9 | +import lombok.Getter; |
| 10 | +import lombok.Setter; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.core.io.Resource; |
| 15 | +import org.springframework.http.HttpHeaders; |
| 16 | +import org.springframework.http.MediaType; |
| 17 | +import org.springframework.http.ResponseEntity; |
| 18 | +import org.springframework.stereotype.Controller; |
| 19 | +import org.springframework.web.bind.annotation.CrossOrigin; |
| 20 | +import org.springframework.web.bind.annotation.GetMapping; |
| 21 | +import org.springframework.web.bind.annotation.PathVariable; |
| 22 | +import org.springframework.web.bind.annotation.PostMapping; |
| 23 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 24 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 25 | +import org.springframework.web.bind.annotation.RequestParam; |
| 26 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 27 | +import org.springframework.web.multipart.MultipartFile; |
| 28 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
| 29 | + |
| 30 | +import javax.servlet.http.HttpServletRequest; |
| 31 | +import java.io.File; |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.Serializable; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.nio.file.Paths; |
| 36 | +import java.text.SimpleDateFormat; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.List; |
| 41 | +import java.util.stream.Collectors; |
| 42 | + |
| 43 | +/** |
| 44 | + * ClassName: FileController |
| 45 | + * |
| 46 | + * @author leegoo |
| 47 | + * @Description: |
| 48 | + * @date 2021年11月16日 |
| 49 | + */ |
| 50 | +@Controller |
| 51 | +@CrossOrigin(allowCredentials = "true", originPatterns = "*", maxAge = 3600) |
| 52 | +public class FileController { |
| 53 | + |
| 54 | + private static final Logger logger = LoggerFactory.getLogger(FileController.class); |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private FileService fileService; |
| 58 | + |
| 59 | + |
| 60 | + @RequestMapping(value = "/", method = RequestMethod.GET) |
| 61 | + public String index() { |
| 62 | + return "template/index"; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + @PostMapping("/uploadFile") |
| 67 | + @ResponseBody |
| 68 | + public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) { |
| 69 | + String fileName = fileService.storeFile(file); |
| 70 | + String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath() |
| 71 | + .path("/downloadFile/") |
| 72 | + .path(fileName) |
| 73 | + .toUriString(); |
| 74 | + return new UploadFileResponse(fileName, fileDownloadUri, |
| 75 | + file.getContentType(), file.getSize()); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + @PostMapping("/uploadMultipleFiles") |
| 80 | + public List<UploadFileResponse> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) { |
| 81 | + return Arrays.stream(files) |
| 82 | + .map(this::uploadFile) |
| 83 | + .collect(Collectors.toList()); |
| 84 | + } |
| 85 | + |
| 86 | + @GetMapping("/downloadFile/{fileName:.+}") |
| 87 | + public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) { |
| 88 | + // Load file as Resource |
| 89 | + Resource resource = fileService.loadFileAsResource(fileName); |
| 90 | + |
| 91 | + // Try to determine file's content type |
| 92 | + String contentType = null; |
| 93 | + try { |
| 94 | + contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath()); |
| 95 | + } catch (IOException ex) { |
| 96 | + logger.info("Could not determine file type."); |
| 97 | + } |
| 98 | + |
| 99 | + // Fallback to the default content type if type could not be determined |
| 100 | + if (contentType == null) { |
| 101 | + contentType = "application/octet-stream"; |
| 102 | + } |
| 103 | + |
| 104 | + return ResponseEntity.ok() |
| 105 | + .contentType(MediaType.parseMediaType(contentType)) |
| 106 | + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") |
| 107 | + .body(resource); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + @javax.annotation.Resource |
| 112 | + private FileProperties fileProperties; // 文件在本地存储的地址 |
| 113 | + |
| 114 | + @GetMapping("/listFileNames") |
| 115 | + @ResponseBody |
| 116 | + public List<FileItem> listFileNames() { |
| 117 | + Path path = Paths.get(fileProperties.getUploadDir()).toAbsolutePath().normalize(); |
| 118 | + File file = path.toFile(); |
| 119 | + File[] files = file.listFiles(); |
| 120 | + if (null == files) { |
| 121 | + return Collections.emptyList(); |
| 122 | + } |
| 123 | + List<File> fileList = Arrays.stream(files).sorted((a, b) -> (int) (b.lastModified() - a.lastModified())).collect(Collectors.toList()); |
| 124 | + List<FileItem> result = new ArrayList<>(files.length); |
| 125 | + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置格式 |
| 126 | + for (File itemFile : fileList) { |
| 127 | + long lastModified = itemFile.lastModified(); |
| 128 | + double d = itemFile.length() / 1048576D; |
| 129 | + result.add(FileItem.builder().filename(itemFile.getName()).freeSpace(String.format("%.2f", d)).lastModifiedStr(format.format(lastModified)).build()); |
| 130 | + } |
| 131 | + return result; |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + @GetMapping("/deleteFile/{fileName}") |
| 136 | + @ResponseBody |
| 137 | + public boolean deleteFile(@PathVariable("fileName") String fileName) { |
| 138 | + return fileService.deleteFile(fileName); |
| 139 | + } |
| 140 | + |
| 141 | + @Setter |
| 142 | + @Getter |
| 143 | + @Builder |
| 144 | + static |
| 145 | + class FileItem implements Serializable { |
| 146 | + private String filename; |
| 147 | + private long lastModifiedL; |
| 148 | + private String lastModifiedStr; |
| 149 | + private String freeSpace; |
| 150 | + } |
| 151 | +} |
0 commit comments