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);
    }

猜你喜欢

转载自blog.csdn.net/weixin_44812604/article/details/128252709