改善之前的文档下载,有错误地址的情况

改善了文档下载,当传过来错误地址的时候,如果为单个文件,会给用户下载一个ErrorFile文件,内容为XXX文件不存在,当多个文件的时候,压缩包里会有正确路径的文件,和一个错误文件,内容为哪写文件不存在。


 /**
     *
     * @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;

        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)){
            ServletOutputStream 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);
            }
            if (bis != null) {
                bis.close();
            }
            if (in != null) {
                in.close();
            }
            if (os != null) {
                os.close();
            }
        }else {
            //拼接文件夹名,MyFileUtils是自己写的一个工具类,getHeaderFileName是写的一个方法通过subString来切割字符串,得到文件名
            String errorFilePathName=MyFileUtils.getHeaderFileName(filename)+" Not Exist";
            //拼接路径
            String errorFilePath = MyFileUtils.getErrorFilePath() + MyFileUtils.getHeaderFileName(errorFilePathName) ;
            //创建路径
            File errorFile = new File(errorFilePath);
            if (!errorFile.exists()) {
                errorFile.mkdirs();
            }
            //定义文件名
            String errorName="ErrorFile.txt";
            //定义响应头,设置下载的文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(errorName, "UTF-8"));

            //创建一个文件
            RandomAccessFile raf = new RandomAccessFile(errorFilePath+File.separator+errorName,"rw");
            //文件里的信息
            String errorMsg="ErrorMessage: "+filename+" 文件不存在!";
            //设置字符编码
            String str=new String(errorMsg.getBytes("gbk"), "iso8859-1");
            //开始写入
            raf.writeBytes(str);
            //将对应文件放入in流
            in = new FileInputStream(errorFile+File.separator+errorName);
            //获取输出流
            ServletOutputStream os = response.getOutputStream();
            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);
            }
            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();

        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+" 文件不存在!";

                try {
                    // 如果文件存在,则追加内容;如果文件不存在,则创建文件
                    errorFiles = new File(errorFilesPath+File.separator+"ErrorFile.txt");
                    //开启追加功能
                    fw = new FileWriter(errorFiles, true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //往文件里追加内容
                PrintWriter pw = new PrintWriter(fw);
                pw.println(errorMsg);
                pw.flush();
                try {
                    fw.flush();
                    pw.close();
                    fw.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        //获得错误文件
        if (FileUtil.isFile(errorFiles)){
            in = new FileInputStream(errorFiles);
            //将错误文件放入zip中
            ZipEntry zipEntry = new ZipEntry("ErrorFile.txt");
            zos.putNextEntry(zipEntry);
            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);
            }
            //关闭该文件追加功能,防止下个用户传错误文件的时候,会获得上一次的错误信息
            fw = new FileWriter(errorFiles, false);
            fw.close();
        }

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

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值