一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java實現服務器文件打包zip并下載的示例(邊打包邊下載)

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

2019-11-18 14:43Java教程網 JAVA教程

這篇文章主要介紹了java實現服務器文件打包zip并下載的示例,使用該方法,可以即時打包文件,一邊打包一邊傳輸,不使用任何的緩存,讓用戶零等待,需要的朋友可以參考下

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

 

復制代碼代碼如下:


/**
 * 
 * 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 {
 }
}

 

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产首页精品 | 国产网站免费看 | 免费观看韩剧网站在线观看 | 天天爱综合 | 国产日韩欧美成人 | 日本丰满www色 | 日本视频免费在线 | 99视频久久精品久久 | 精品日韩欧美一区二区三区 | nhdta系列媚药系列 | 久久99国产综合精品AV蜜桃 | 国产真实一区二区三区 | 蜜桃视频在线观看官网 | 色偷偷91久久综合噜噜噜 | 热穴高校 | 亚洲欧美日韩国产综合专区 | asspics大尿chinese| 日本xxxxxxxxx高清hd | 操到翻白眼 | 午夜精品久久久内射近拍高清 | 亚洲精品国产精品国自产观看 | 久久九九久精品国产尤物 | 国产伦码精品一区二区三区 | 千金在线观看 | 好男人免费高清在线观看2019 | 国产伦精品一区二区 | 精品区卡一卡2卡三免费 | 久久精品黄AA片一区二区三区 | 9999网站 | 免费视频亚洲 | 丝袜老师好湿好紧我要进去了 | 美女国内精品自产拍在线播放 | 调教小荡娃h | 欧美贵妇vs高跟办公室 | x8x8在线永久免费观看 | 激情婷婷综合久久久久 | ipx358cn出差被男上司在线 | 久青草国产在线观看视频 | 亚洲AV午夜福利精品香蕉麻豆 | 亚洲国产综合久久久无码色伦 | 亚洲人成绝费网站色ww |