java实现文件的上传和下载,将文件流转base64返回给前端

上传代码

public ResultInfo<?> uploadFile(@RequestParam MultipartFile file, @RequestParam String id) throws BusinessException{
        try {
            if (file.isEmpty()) {
                return JsonResult.error(StatusCode.ERROR_ADD);
            }
            // 获取文件名
            String fileName = file.getOriginalFilename();
            System.out.println("上传的文件名为:" + fileName);
            String preName = fileName.substring(0,fileName.lastIndexOf("."));
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
            System.out.println("文件的后缀名为:" + suffixName);
            // 设置文件存储路径         *************************************************
            String filePath = "自己的文件路径/";
            String path = filePath + fileName;
            File dest = new File(new File(path).getAbsolutePath());// dist为文件,有多级目录的文件
            // 检测是否存在目录
            if (dest.getParentFile().exists()) {//因此这里使用.getParentFile(),目的就是取文件前面目录的路径
                //如果存在文件夹
                FileSystemUtils.deleteRecursively(dest.getParentFile());// 删除文件夹
            }
            dest.getParentFile().mkdirs();// 新建文件夹
            file.transferTo(dest);// 文件写入
            return this.update(actionData);
        } catch (IllegalStateException e) {
            e.printStackTrace();
            throw new BusinessException("999999", "业务数据操作异常", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("999999", "业务数据操作异常", e);
        }
    }

文件下载代码

public ResultInfo<?> downloadFile(String fileName , HttpServletResponse response) throws BusinessException{
        if (fileName != null) {
            //设置文件路径
            String filePath = "自己的文件路径"+"/"+fileName;
            File file = new File(filePath);
            if (file.exists()) {
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return JsonResult.success(bis);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally { // 做关闭操作
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return JsonResult.error(StatusCode.ERROR_ADD);
    }

后端通过json拿到文件流的返回,我采用将文件流转成base64返回给前台,实现代码如下:

public ResultInfo<?> downloadFile(String fileName ) throws BusinessException{
        if (fileName != null) {
            //设置文件路径
            String filePath = "自己的文件路径/"+fileName;
            String base64 = null;
            InputStream in = null;
            try {
                File file = new File(filePath);
                in = new FileInputStream(file);
                byte[] bytes=new byte[(int)file.length()];
                in.read(bytes);
                base64 = Base64.getEncoder().encodeToString(bytes);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("999999", "业务数据操作异常", e);
            }finally {
                if(in!=null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        throw new BusinessException("999999", "业务数据操作异常", e);
                    }
                }
            }
            return JsonResult.success(base64);
        }
        return JsonResult.error(StatusCode.ERROR_ADD);
    }
  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值