zoukankan      html  css  js  c++  java
  • java 文件流的处理 文件打包成zip

    1、下载文件到本地

    public void download(HttpServletResponse response){
        String filePath ="";//文件路径
        String fileName ="";//文件名称
        // 读到流中
        InputStream inStream = new FileInputStream(filePath);
        // 设置输出的格式
        response.reset();
         response.setContentType("bin");
         response.addHeader("Content-Disposition", "attachment; filename="" + fileName + """);
         IOUtils.copy(inStream, response.getOutputStream());
    }
    

    2、java后端下载

    方式一:
    new URL(fileUrl + item.getcBhFileserver()).openStream()
    
    方法二:
        public Boolean addFile(String url, String id, String fileName) {
    
            RequestCallback requestCallBack = new RequestCallback() {
    
                @Override
                public void doWithRequest(ClientHttpRequest request) throws IOException {
                    request.getHeaders().add("accept", MediaType.APPLICATION_OCTET_STREAM_VALUE);
                }
            };
    
            ResponseExtractor<Boolean> responseExtractor = new ResponseExtractor<Boolean>() {
    
                @Override
                public Boolean extractData(ClientHttpResponse response) throws IOException {
                    if (response.getStatusCode() == HttpStatus.OK) {
                        //得到文件流
                        InputStream input = response.getBody();
                        return true;
                    }
                    return false;
                }
            };
            return restTemplate.execute(url, HttpMethod.GET, requestCallBack, responseExtractor, id);
        }
    

    3、文件打包成zip

    public void zipFilesAll() throws Exception {
            String zipPath = "";//zip包路径
            String zipFileName = "";//zip包名称
            File zipFile = new File(zipFileName .toString());
    
            // 创建 FileOutputStream 对象
            FileOutputStream fileOutputStream = null;
            // 创建 ZipOutputStream
            ZipOutputStream zipOutputStream = null;
            try {
                //创建文件夹
                zipFile = new File(zipPath );
                FileUtils.forceMkdir(zipFile);
    
                //创建文件
                zipFile = new File(zipFileName .toString());
                if (!zipFile.exists()) {
                    zipFile.createNewFile();
                }
    
                // 实例化 FileOutputStream 对象
                fileOutputStream = new FileOutputStream(zipFileName.toString());
                // 实例化 ZipOutputStream 对象
                zipOutputStream = new ZipOutputStream(fileOutputStream);
                // 创建 ZipEntry 对象
                ZipEntry zipEntry = null;
                for (CL cl: ClList) {
                    // 实例化 ZipEntry 对象,源文件数组中的当前文件
                    zipEntry = new ZipEntry(tCltjjl.getcClmc() + ".zip");
                    zipOutputStream.putNextEntry(zipEntry);
                    IOUtils.copy(new FileInputStream(cl.getcPath(), zipOutputStream);
                }
            } catch (Exception e) {
                
            }finally{
                 //记得删除文件
            }
        }    
    

      

  • 相关阅读:
    单点登陆
    【springmvc学习】常用注解总结
    Excel里函数中的万金油,你确定不要点进来看看?
    Maven3在Eclipse上安装插件
    VSCode 云同步扩展设置 Settings Sync 插件
    Ubuntu 18.04 root 使用ssh密钥远程登陆
    [Asp.net] C# 操作Excel的几种方式 优缺点比较
    开源框架Autofac使用入门
    C# .net Ueditor实现图片上传到阿里云OSS 对象存储
    [python]TypeError: string indices must be integers解决方法
  • 原文地址:https://www.cnblogs.com/jiehanshi/p/11533465.html
Copyright © 2011-2022 走看看