• FileUploadController


    @Slf4j
    @Controller
    @RequestMapping(value = "/upload")
    public class JkFileUploadController {
    
        private static final String UPLOAD_FILE_PATH = "upload/jk/files";
    
        @Value("${data.attachment}")
        public String root;
    
        @Value("${domain}")
        public String domain;
    
        @Resource
        private ResourceLoader resourceLoader;
        
        /**
         * 允许上传文件的格式
         */
        private static final String[] FILE_TYPE = new String[]{".doc", ".ppt", ".pdf", ".xls", ".xlsx", ".gif", ".jpeg", ".bmp", ".jpg", ".png"};
    
        @PostMapping(value = "/jk/files")
        public ResponseEntity<String> uploadAllFile(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
            if (file.isEmpty() || !FileValidateUtil.validateType(file, FILE_TYPE)) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
            }
            String path = getFilePath(file.getOriginalFilename());
            String docUrl = StringUtils.replace(StrUtil.subSuf(path, root.length()), "\", "/");
            try {
                File newFile = new File(path);
                file.transferTo(newFile);
                return ResponseEntity.status(HttpStatus.OK).body(domain + docUrl);
            } catch (IOException e) {
                log.error("上传文件错误!" + e);
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
            }
        }
    
        @ResponseBody
        @RequestMapping(method = RequestMethod.GET, value = "/jk/files/{filename:.+}")
        public ResponseEntity<?> getFile(@PathVariable String filename) {
            try {
                String filePath = StringUtils.replace(Paths.get(root, UPLOAD_FILE_PATH, filename).toString(), "\", "/");
                return ResponseEntity.ok(resourceLoader.getResource("file:" + filePath));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return ResponseEntity.notFound().build();
            }
        }
    
        private String getFilePath(String sourceFileName) {
            String baseFolder = root + File.separator + UPLOAD_FILE_PATH;
            File file = new File(baseFolder);
            if (!file.isDirectory()) {
                if (!file.mkdirs()) {
                    log.info("目录创建失败");
                }
            }
            // 生成新的文件名
            String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + RandomUtils.nextInt(100, 9999) + "." + StringUtils.substringAfterLast(sourceFileName, ".");
            return baseFolder + File.separator + fileName;
        }
    }
  • 相关阅读:
    ListView具有多种item布局——实现微信对话列
    CSDN 2013年度博客之星评选——分享几张厦门杭州的美图
    不做旁观者,给博主最有力的支持——博客之星评选,期待您的支持,谢谢路过的朋友投上您宝贵的一票
    Android应用中使用及实现系统“分享”接口
    android手势创建及识别
    JSON数据源的多值参数实现
    葡萄城报表本地设计器
    如何设计带查询条件的报表
    巧用SQL语句补足不完全数据报表
    表格数据分组报表
  • 原文地址:https://www.cnblogs.com/xiaomaoyvtou/p/13305399.html
Copyright © 2020-2023  润新知