关于文件上传下载问题

如果只是单纯的文件上传下载还是比较简单的。其代码如下:

文件上传:

    @PostMapping("/uploadFile")
    @ApiOperation("上传报告文件")
    public ResponseResult uploadAudio(@RequestParam(value = "file") MultipartFile file) {

        if (file.isEmpty()) {
            return ResponseResult.failed("文件不能为空");
        }

        //文件地址不存在,则创建
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        //文件后缀
        String prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        //if (".mp3".equals(prefix) || ".m4a".equals(prefix) || ".wav".equals(prefix)) {

       // String fileName = UUID.randomUUID().toString().replaceAll("-", "") + prefix;

        String fileName = file.getOriginalFilename();

        File newFile = new File(path + fileName);
        log.info("上传路径:" + path);
        log.info("文件名:" + fileName);
        try {
            file.transferTo(newFile);
//               request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+
            String url = dbPath + fileName;
            Map<String,Object> map=new HashMap<>();
            map.put("url",url);
            map.put("name",fileName);
            return ResponseResult.ok(map, "上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.failed("上传失败");
        }
        //  }
        // return ResponseResult.failed("文件格式不符合要求");

    }

文件下载:

@GetMapping("/exportFile")
    @ApiOperation("下载文件")
    public void exportQualityReport(@RequestParam("id") Integer id,HttpServletResponse response)throws IOException{
        InputStream inputStream = null;
        ServletOutputStream outputStream = null;
        if (id!=null){
            QualityReportVo vo=reportService.getById(id);
            if (vo!=null){
                try {
                    String fileName = vo.getFileName();
                    String path1 =locationPre+vo.getReportLocation();
                    Resource resource = new DefaultResourceLoader().getResource("file:" + path1);
                    FileConvertUtil.responseInit(response, fileName);
                    inputStream = resource.getInputStream();
                    outputStream = response.getOutputStream();
                    IOUtils.copy(inputStream, outputStream);
                    response.flushBuffer();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new IOException("下载错误");
                } finally {
                    try {
                        if (outputStream != null) {
                            outputStream.close();
                        }
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (Exception e) {
                        throw new CustomException("下载错误");
                    }
                }
            }else {
                throw new IOException("文件id不存在");
            }
        }else {
            throw new CustomException("id不能为空");
        }

    }

忘记补上重要的一点了,就是设置下载的规则,也就是编码呐,以哪种形象下载呐

/**
 * 设置文件下载规则
 *
 * @param response
 * @param fileName
 */
public static void responseInit(HttpServletResponse response, String fileName) {
    response.reset();

    response.setHeader("Content-Disposition", "attachment;filename=" + new
            String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));

    response.setContentType("application/octet-stream; charset=utf-8");

    response.setCharacterEncoding("UTF-8");

}

路径就用自己上传文件的路径就好,代码里面的路径是因为在存储的时候,只显示相对路径,所以在拿个路径时,需要加上前缀做处理。然后路径我也是在配置文件上面配置好了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值