Search in sources :

Example 1 with DownloadException

use of com.qiwenshare.ufop.exception.operation.DownloadException in project qiwen-file by qiwenshare.

the class FiletransferService method downloadFile.

@Override
public void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) {
    UserFile userFile = userFileMapper.selectById(downloadFileDTO.getUserFileId());
    if (userFile.getIsDir() == 0) {
        FileBean fileBean = fileMapper.selectById(userFile.getFileId());
        Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
        if (downloader == null) {
            log.error("下载失败,文件存储类型不支持下载,storageType:{}", fileBean.getStorageType());
            throw new DownloadException("下载失败");
        }
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.setFileUrl(fileBean.getFileUrl());
        downloadFile.setFileSize(fileBean.getFileSize());
        httpServletResponse.setContentLengthLong(fileBean.getFileSize());
        downloader.download(httpServletResponse, downloadFile);
    } else {
        LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.likeRight(UserFile::getFilePath, userFile.getFilePath() + userFile.getFileName() + "/").eq(UserFile::getUserId, userFile.getUserId()).eq(UserFile::getIsDir, 0).eq(UserFile::getDeleteFlag, 0);
        List<UserFile> userFileList = userFileMapper.selectList(lambdaQueryWrapper);
        String staticPath = UFOPUtils.getStaticPath();
        String tempPath = staticPath + "temp" + File.separator;
        File tempDirFile = new File(tempPath);
        if (!tempDirFile.exists()) {
            tempDirFile.mkdirs();
        }
        FileOutputStream f = null;
        try {
            f = new FileOutputStream(tempPath + userFile.getFileName() + ".zip");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
        ZipOutputStream zos = new ZipOutputStream(csum);
        BufferedOutputStream out = new BufferedOutputStream(zos);
        try {
            for (UserFile userFile1 : userFileList) {
                FileBean fileBean = fileMapper.selectById(userFile1.getFileId());
                Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
                if (downloader == null) {
                    log.error("下载失败,文件存储类型不支持下载,storageType:{}", fileBean.getStorageType());
                    throw new UploadException("下载失败");
                }
                DownloadFile downloadFile = new DownloadFile();
                downloadFile.setFileUrl(fileBean.getFileUrl());
                downloadFile.setFileSize(fileBean.getFileSize());
                InputStream inputStream = downloader.getInputStream(downloadFile);
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                try {
                    zos.putNextEntry(new ZipEntry(userFile1.getFilePath().replace(userFile.getFilePath(), "/") + userFile1.getFileName() + "." + userFile1.getExtendName()));
                    byte[] buffer = new byte[1024];
                    int i = bis.read(buffer);
                    while (i != -1) {
                        out.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (IOException e) {
                    log.error("" + e);
                    e.printStackTrace();
                } finally {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        out.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            log.error("压缩过程中出现异常:" + e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String zipPath = "";
        try {
            Downloader downloader = ufopFactory.getDownloader(StorageTypeEnum.LOCAL.getCode());
            DownloadFile downloadFile = new DownloadFile();
            downloadFile.setFileUrl("temp" + File.separator + userFile.getFileName() + ".zip");
            File tempFile = new File(UFOPUtils.getStaticPath() + downloadFile.getFileUrl());
            httpServletResponse.setContentLengthLong(tempFile.length());
            downloader.download(httpServletResponse, downloadFile);
            zipPath = UFOPUtils.getStaticPath() + "temp" + File.separator + userFile.getFileName() + ".zip";
        } catch (Exception e) {
            // org.apache.catalina.connector.ClientAbortException: java.io.IOException: Connection reset by peer
            if (e.getMessage().contains("ClientAbortException")) {
            // 该异常忽略不做处理
            } else {
                log.error("下传zip文件出现异常:{}", e.getMessage());
            }
        } finally {
            File file = new File(zipPath);
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
Also used : DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) ImageInputStream(javax.imageio.stream.ImageInputStream) UploadException(com.qiwenshare.ufop.exception.operation.UploadException) ZipEntry(java.util.zip.ZipEntry) Downloader(com.qiwenshare.ufop.operation.download.Downloader) Adler32(java.util.zip.Adler32) UploadException(com.qiwenshare.ufop.exception.operation.UploadException) DownloadException(com.qiwenshare.ufop.exception.operation.DownloadException) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) ZipOutputStream(java.util.zip.ZipOutputStream) DownloadException(com.qiwenshare.ufop.exception.operation.DownloadException) CheckedOutputStream(java.util.zip.CheckedOutputStream) DeleteFile(com.qiwenshare.ufop.operation.delete.domain.DeleteFile) UploadFile(com.qiwenshare.ufop.operation.upload.domain.UploadFile) DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) PreviewFile(com.qiwenshare.ufop.operation.preview.domain.PreviewFile)

Aggregations

LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 DownloadException (com.qiwenshare.ufop.exception.operation.DownloadException)1 UploadException (com.qiwenshare.ufop.exception.operation.UploadException)1 DeleteFile (com.qiwenshare.ufop.operation.delete.domain.DeleteFile)1 Downloader (com.qiwenshare.ufop.operation.download.Downloader)1 DownloadFile (com.qiwenshare.ufop.operation.download.domain.DownloadFile)1 PreviewFile (com.qiwenshare.ufop.operation.preview.domain.PreviewFile)1 UploadFile (com.qiwenshare.ufop.operation.upload.domain.UploadFile)1 Adler32 (java.util.zip.Adler32)1 CheckedOutputStream (java.util.zip.CheckedOutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1