zoukankan      html  css  js  c++  java
  • springboot文件上传下载

    springboot比较方便的下载方式,在此记录一下

    public ResponseEntity<FileSystemResource> export(File file) {
        if (file == null) {
            return null;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=" + file.getName());
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new FileSystemResource(file));
    }

    使用方式如下:

    @RequestMapping("/downLoadImg")
        public ResponseEntity<FileSystemResource> downLoadImg(HttpServletRequest request) {
            String imgPath = request.getParameter("img");
            //文件路径
            String filePath = SDKUtils.getImgPath() + imgPath;
            return export(new File(filePath));
        }

    文件上传方式

        @PostMapping(value = "/upload")
        public CommonResult upload(@RequestParam("file") MultipartFile file) {
            String fileName = file.getOriginalFilename();
            //FileUtil 是使用hutool 工具类的   touch  : 创建文件,如果父目录不存在也自动创建
            //resourceConfig.getImageUrl()  存储路径
            File mkdir = FileUtil.touch(resourceConfig.getImageUrl() + fileName);
            try {
                //据说性能不错,使用方便,还没验证
                file.transferTo(mkdir);
                return CommonResult.success("上传成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return CommonResult.error("上传失败");
        }
    
  • 相关阅读:
    APK自我保护方法
    Andorid APK反逆向解决方案---梆梆加固原理探寻
    判断android文件是否加壳
    java调用dll-JNA
    Java调用本地接口
    pat00-自测2. 素数对猜想 (20)
    pat00-自测4. Have Fun with Numbers (20)
    pat00-自测3. 数组元素循环右移问题 (20)
    pat00-自测1. 打印沙漏(20)
    pat1013. Battle Over Cities (25)
  • 原文地址:https://www.cnblogs.com/quliang/p/11548342.html
Copyright © 2011-2022 走看看