再次改善文档下载

通过将错误信息写成字符,然后直接写入字符流。

 /**
     *
     * @return
     * @Author lrw
     * @Discription 下载文件:如果前端传过来的filePath为一个文件,就下载单个文件
     * 如果为多个文件,就下载一个压缩包文件
     */
    @Override
    public void downloadTranslationFiles(List<String> filePath, HttpServletResponse response) throws IOException {

        if (filePath.size() <= 1) {
            for (String fp : filePath) {
                downloadFilesForOne( fp, response);
            }
        } else {
            downloadFilesForZIP(filePath, response);
        }


    }

    /**
     * @param response
     * @throws IOException
     * @Author lrw
     * @Discription 下载单个文件
     */
    public void downloadFilesForOne(String filePath, HttpServletResponse response) throws IOException {

        FileSystemResource fileSystemResource = null;
        FileInputStream in = null;

        String filename = null;
        BufferedInputStream bis = null;
        ServletOutputStream os=null;

        filename = filePath.substring(filePath.lastIndexOf("\\") + 1);


        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        fileSystemResource = new FileSystemResource(filePath);
        File file = fileSystemResource.getFile();
        if (FileUtil.isFile(file)){
            os = response.getOutputStream();
            in = new FileInputStream(file);
            bis = new BufferedInputStream(in, 1024 * 10);
            byte[] buf = new byte[1024 * 10];
            int len = 0;
            while ((len = bis.read(buf, 0, 1024 * 10)) > 0) {
                os.write(buf, 0, len);
            }
            
        }else {

            //定义响应头,设置下载的文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("ErrorFile_"+System.currentTimeMillis()+".txt", "UTF-8"));

            //文件里的信息
            String errorMsg="ErrorMessage: "+filename+" 文件不存在!";
            //将String转换为字符,设置字符编码
            byte[] buf = errorMsg.getBytes(StandardCharsets.UTF_8);

            //获取输出流
            os = response.getOutputStream();

            os.write(buf);

       
        }
        if (bis != null) {
            bis.close();
        }
        if (in != null) {
            in.close();
        }
        if (os != null) {
            os.close();
        }


    }

    /**
     * lrw
     * 下载ZIP
     * @param response
     * @throws IOException
     */
    public void downloadFilesForZIP(List<String> filePath, HttpServletResponse response) throws IOException {

        OutputStream os = null;
        FileSystemResource fileSystemResource = null;
        FileInputStream in = null;
        ZipOutputStream zos = null;
        String FilePath = null;
        String filename = null;
        BufferedInputStream bis = null;
        File errorFiles = null;
        String errorFilesPath = null;
        FileWriter fw = null;
        os = response.getOutputStream();
        List<String> list = new ArrayList<>();
        zos = new ZipOutputStream(os);
        String zipFileName = System.currentTimeMillis() + "_dataFiles.zip";
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName, "UTF-8"));
        for (String fp : filePath) {

            FilePath = fp;
            filename = FilePath.substring(FilePath.lastIndexOf("\\") + 1);
            fileSystemResource = new FileSystemResource(FilePath);
            File file = fileSystemResource.getFile();
            if (FileUtil.isFile(file)) {
                ZipEntry zipEntry = new ZipEntry(filename);
                zos.putNextEntry(zipEntry);
                in = new FileInputStream(file);
                bis = new BufferedInputStream(in, 1024 * 10);
                byte[] buf = new byte[1024 * 10];
                int len = 0;
                while ((len = bis.read(buf, 0, 1024 * 10)) > 0) {
                    zos.write(buf, 0, len);
                }

            } else {
                //拼接一个文件夹名
                errorFilesPath = MyFileUtils.getErrorFilePath() + "ErrorFile";
                //创建文件夹
                File errorFilesFolder = new File(errorFilesPath);
                if (!errorFilesFolder.exists()) {
                    errorFilesFolder.mkdirs();
                }
                //设置错误信息
                String errorMsg = "ErrorMessage: " + filename + " 文件不存在!"+"\n";
                //放入列表中
                list.add(errorMsg);


            }
        }

		//设置压缩包里错误文件名
        ZipEntry zipEntry = new ZipEntry("ErrorFile_"+System.currentTimeMillis()+".txt");
        zos.putNextEntry(zipEntry);
        for (String str:list){

            byte[] buf = str.getBytes(StandardCharsets.UTF_8);

                zos.write(buf);


        }


        if (bis != null) {
            bis.close();
        }
        if (in != null) {
            in.close();
        }
        if (zos != null) {
            zos.close();
        }

    }
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值