欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)服務(wù)器文件打包zip并下載的示例(邊打包邊下載)

 更新時(shí)間:2014年04月13日 09:18:40   作者:  
這篇文章主要介紹了java實(shí)現(xiàn)服務(wù)器文件打包zip并下載的示例,使用該方法,可以即時(shí)打包文件,一邊打包一邊傳輸,不使用任何的緩存,讓用戶零等待,需要的朋友可以參考下

使用該方法,可以即時(shí)打包文件,一邊打包一邊傳輸,不使用任何的緩存,讓用戶零等待!

復(fù)制代碼 代碼如下:

/**
 *
 * mySocket 客戶端 Socket
 * @param file 待打包的文件夾或文件
 * @param fileName 打包下載的文件名
 * @throws IOException
 */

private void down(File file, String fileName) throws IOException {
 OutputStream outputStream = mySocket.getOutputStream();
 StringBuffer sb = new StringBuffer("HTTP/1.1 200 OK\r\n");
 sb.append("Server: java/1.1\r\n");
 sb.append("Content-Type:application/octet-stream;charset=UTF-8\r\n");
 //sb.append("User-Agent: Mozilla/4.0 (compatible;MSIE6.0;Windows NT 5.0)\r\n");
 sb.append("Content-Disposition: attachment; filename=" + fileName
   + "\r\n");
 sb.append("Transfer-Encoding: chunked\r\n");
 sb.append("Connection: Keep-Alive\r\n\r\n");
 outputStream.write(sb.toString().getBytes());
 outputStream.flush();
 ZipCompressor zipCompressor = new ZipCompressor(new MyOutputStream(
   outputStream));
 zipCompressor.compress(file);
 System.out.println("zip end");  
 System.out.println("write '0\\r\\n\\r\\n'");
 outputStream.write("0\r\n\r\n".getBytes());//Transfer-Encoding: chunked傳輸結(jié)束標(biāo)記
 outputStream.flush();
 outputStream.close();
 System.out.println("download stop");
 try {
  mySocket.close();
 } catch (Throwable t) {
 }
}

復(fù)制代碼 代碼如下:

package cn.liangjintang.webserver.zipFile;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCompressor {
 static final int BUFFER = 8192;
 private OutputStream outputStream;
 public ZipCompressor(MyOutputStream outputStream) {
  this.outputStream=outputStream;
 }
 public void compress(File file) {
  if (!file.exists())
   throw new RuntimeException(file.getAbsolutePath() + "不存在!");
  try {
   CheckedOutputStream cos = new CheckedOutputStream(outputStream,
     new CRC32());
   ZipOutputStream out = new ZipOutputStream(cos);
   String basedir = "";
   compress(file, out, basedir);
   out.close();//必須關(guān)閉,這樣才會(huì)寫入zip的結(jié)束信息,否則zip文件不完整.若想繼續(xù)寫入,可重寫outputStream.close()方法
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }

 private void compress(File file, ZipOutputStream out, String basedir) {
  //判斷是目錄還是文件
  if (file.isDirectory()) {
   System.out.println("壓縮:" + basedir + file.getName());
   this.compressDirectory(file, out, basedir);
  } else {
   System.out.println("壓縮:" + basedir + file.getName());
   this.compressFile(file, out, basedir);
  }
 }

 // 壓縮一個(gè)目錄
 private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
  if (!dir.exists())
   return;

  File[] files = dir.listFiles();
  for (int i = 0; i < files.length; i++) {
   /** 遞歸 */
   compress(files[i], out, basedir + dir.getName() + "/");
  }
 }

 //壓縮一個(gè)文件
 private void compressFile(File file, ZipOutputStream out, String basedir) {
  if (!file.exists()) {
   return;
  }
  try {
   BufferedInputStream bis = new BufferedInputStream(
     new FileInputStream(file));
   ZipEntry entry = new ZipEntry(basedir + file.getName());
   out.putNextEntry(entry);
   int count;
   byte data[] = new byte[BUFFER];
   while ((count = bis.read(data, 0, BUFFER)) != -1) {
    out.write(data, 0, count);
   }
   bis.close();
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }
}

復(fù)制代碼 代碼如下:

package cn.liangjintang.webserver.zipFile;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MyOutputStream extends FilterOutputStream {
 public MyOutputStream(OutputStream out) {
  super(out);
 }

 final byte[] oneBytes = "1\r\n".getBytes();
 final byte[] rnBytes = "\r\n".getBytes();

 public void write(int b) throws IOException {
  out.write(oneBytes);//字節(jié)數(shù)1+CRLF
  out.write(b);//數(shù)據(jù)實(shí)體
  out.write(rnBytes);//CRLF
 }

 public void write(byte[] b) throws IOException {
  out.write(Integer.toHexString(b.length).getBytes());//字節(jié)數(shù),十六進(jìn)制
  out.write(rnBytes);//CRLF
  out.write(b);//數(shù)據(jù)實(shí)體
  out.write(rnBytes);//CRLF
 }

 public void write(byte[] b, int off, int len) throws IOException {
  out.write(Integer.toHexString(len - off).getBytes());//字節(jié)數(shù),十六進(jìn)制
  out.write(rnBytes);//CRLF
  out.write(b, off, len);//數(shù)據(jù)實(shí)體
  out.write(rnBytes);//CRLF
 }

 /**
  * 重寫該方法,否則OutputStream會(huì)被關(guān)閉,其他的數(shù)據(jù)<br/>
  * (如Transfer-Encoding: chunked傳輸結(jié)束標(biāo)記)就不能再繼續(xù)寫入了
  */
 public void close() throws IOException {
 }
}

相關(guān)文章

  • 詳解Spring Data Jpa當(dāng)屬性為Null也更新的完美解決方案

    詳解Spring Data Jpa當(dāng)屬性為Null也更新的完美解決方案

    這篇文章主要介紹了詳解Spring Data Jpa當(dāng)屬性為Null也更新的完美解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • Java SMM框架關(guān)聯(lián)關(guān)系映射示例講解

    Java SMM框架關(guān)聯(lián)關(guān)系映射示例講解

    SSM框架是spring MVC ,spring和mybatis框架的整合,是標(biāo)準(zhǔn)的MVC模式,將整個(gè)系統(tǒng)劃分為表現(xiàn)層,controller層,service層,DAO層四層,使用spring MVC負(fù)責(zé)請(qǐng)求的轉(zhuǎn)發(fā)和視圖管理,spring實(shí)現(xiàn)業(yè)務(wù)對(duì)象管理,mybatis作為數(shù)據(jù)對(duì)象的持久化引擎
    2022-08-08
  • Mybatis-plus批量去重插入ON DUPLICATE key update使用方式

    Mybatis-plus批量去重插入ON DUPLICATE key update使用方式

    這篇文章主要介紹了Mybatis-plus批量去重插入ON DUPLICATE key update使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • SpringBoot整合swagger的操作指南

    SpringBoot整合swagger的操作指南

    Swagger 是一個(gè)開(kāi)源的框架,用于設(shè)計(jì)、構(gòu)建、文檔化和使用 RESTful 風(fēng)格的 Web 服務(wù),Spring Boot 是一個(gè)用于構(gòu)建獨(dú)立的、基于生產(chǎn)級(jí)別的 Spring 應(yīng)用程序的框架,本文講給大家介紹一下SpringBoot整合swagger的操作指南,需要的朋友可以參考下
    2023-09-09
  • Java線程中的線程本地變量ThreadLocal詳解

    Java線程中的線程本地變量ThreadLocal詳解

    這篇文章主要介紹了Java線程中的線程本地變量ThreadLocal詳解,ThreadLocal存放的值是線程內(nèi)共享的,線程間互斥的,主要用于線程內(nèi)共享一些數(shù)據(jù),避免通過(guò)參數(shù)來(lái)傳遞,這樣處理后,能夠優(yōu)雅的解決一些實(shí)際問(wèn)題,需要的朋友可以參考下
    2023-11-11
  • java如何獲得redis所有的key-value

    java如何獲得redis所有的key-value

    這篇文章主要介紹了java如何獲得redis所有的key-value,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 淺談一下數(shù)據(jù)庫(kù)連接池Druid德魯伊

    淺談一下數(shù)據(jù)庫(kù)連接池Druid德魯伊

    數(shù)據(jù)庫(kù)連接池就是一個(gè)容器持有多個(gè)數(shù)據(jù)庫(kù)連接,當(dāng)程序需要操作數(shù)據(jù)庫(kù)的時(shí)候直接從池中取出連接,使用完之后再還回去,和線程池一個(gè)道理,需要的朋友可以參考下
    2023-05-05
  • 解決nacos項(xiàng)目啟動(dòng)報(bào)錯(cuò):Connection refused: no further informa問(wèn)題

    解決nacos項(xiàng)目啟動(dòng)報(bào)錯(cuò):Connection refused: no further&

    這篇文章主要介紹了解決nacos項(xiàng)目啟動(dòng)報(bào)錯(cuò):Connection refused: no further informa問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • java執(zhí)行bat命令碰到的阻塞問(wèn)題的解決方法

    java執(zhí)行bat命令碰到的阻塞問(wèn)題的解決方法

    這篇文章主要介紹了java執(zhí)行bat命令碰到的阻塞問(wèn)題的解決方法,有需要的朋友可以參考一下
    2014-01-01
  • springboot中EasyPoi實(shí)現(xiàn)自動(dòng)新增序號(hào)的方法

    springboot中EasyPoi實(shí)現(xiàn)自動(dòng)新增序號(hào)的方法

    本文主要介紹了EasyPoi實(shí)現(xiàn)自動(dòng)新增序號(hào),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評(píng)論