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{
                 //记得删除文件
            }
        }    
    

      

  • 相关阅读:
    SGU 176.Flow construction (有上下界的最大流)
    POJ 2391.Ombrophobic Bovines (最大流)
    poj 1087.A Plug for UNIX (最大流)
    poj 1273.PIG (最大流)
    POJ 2112.Optimal Milking (最大流)
    SGU 196.Matrix Multiplication
    SGU 195. New Year Bonus Grant
    关于multicycle path
    ppt做gif动图
    codeforces 598A Tricky Sum
  • 原文地址:https://www.cnblogs.com/jiehanshi/p/11533465.html
Copyright © 2011-2022 走看看