国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現服務器文件打包zip并下載的示例(邊打包邊下載)

2019-11-26 15:33:26
字體:
來源:轉載
供稿:網友

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

復制代碼 代碼如下:

/**
 *
 * 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傳輸結束標記
 outputStream.flush();
 outputStream.close();
 System.out.println("download stop");
 try {
  mySocket.close();
 } catch (Throwable t) {
 }
}

復制代碼 代碼如下:

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();//必須關閉,這樣才會寫入zip的結束信息,否則zip文件不完整.若想繼續寫入,可重寫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);
  }
 }

 // 壓縮一個目錄
 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() + "/");
  }
 }

 //壓縮一個文件
 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);
  }
 }
}

復制代碼 代碼如下:

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);//字節數1+CRLF
  out.write(b);//數據實體
  out.write(rnBytes);//CRLF
 }

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

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 华坪县| 唐山市| 榆林市| 太白县| 哈巴河县| 富蕴县| 郴州市| 湖北省| 乐安县| 赫章县| 抚宁县| 木兰县| 普洱| 永平县| 延川县| 衡山县| 买车| 雷州市| 三亚市| 兴隆县| 新昌县| 大姚县| 开阳县| 南靖县| 宜良县| 赤水市| 绥江县| 雷山县| 通许县| 万安县| 宁国市| 博爱县| 铁力市| 黄大仙区| 舞阳县| 曲阳县| 大同市| 乐陵市| 大关县| 长宁县| 自治县|