日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 打包边下载_JAVA实现边下载边压缩

發布時間:2025/3/12 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 打包边下载_JAVA实现边下载边压缩 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

@ResponseBodypublic voiddownloadUrl(HttpServletResponse response, HttpServletRequest request){

String sourceFilePath=properties.getString("sourceFilePath");//要下載的文件路徑

eavlProName = xxx;//項目名

String downloadName = "xxx.zip"; //下載文件名

String agent = request.getHeader("USER-AGENT"); //瀏覽器內核信息

try{if (agent.contains("MSIE")||agent.contains("Trident")) {//IE

downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");

}else{

downloadName= new String(downloadName.getBytes("UTF-8"),"ISO-8859-1");

}

}catch(Exception e) {

e.printStackTrace();

}

zip(sourceFilePath, eavlProName, response, downloadName);

response.getOutputStream().close();//這個應該沒什么用

}private voidzip(String souceFileName, String eavlProName, HttpServletResponse response, String downloadName) {

File file= newFile(souceFileName);try{

zip(file, eavlProName, response, downloadName);

}catch(IOException e) {

e.printStackTrace();

}

}private void zip(File souceFile, String eavlProName, HttpServletResponse response, String downloadName) throwsIOException {

ZipOutputStream out= null;//設置壓縮流。

response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");try{

out= new ZipOutputStream(newBufferedOutputStream(response.getOutputStream()));

out.setMethod(ZipOutputStream.DEFLATED);//設置壓縮方法

} catch(Exception e) {

e.printStackTrace();

}

out.setEncoding("gbk");

zip(souceFile, out, eavlProName, eavlProName,null);

out.closeEntry();

out.close();

}private voidzip(File souceFile, ZipOutputStream out, String base, String eavlProName, Integer i)throwsIOException {if(souceFile.isDirectory()) {

File[] files=souceFile.listFiles();if (files.length != 0) {if (i == null || i != 1) { //對于地方目錄先不創建,當地方目錄下存在項目的截圖時,才創建

out.putNextEntry(new ZipEntry(base + "/"));

base= base.length() == 0 ? "" : base + "/";

}if (i == null) {

i= 0;

}

i++;for(File file : files) {if (i == null || i != 2) { //2地方下面的項目名.這種不創建目錄

zip(file, out, base +file.getName(), eavlProName, i);

}else{

String picPathname=file.getName();if (picPathname.equals(eavlProName)) { //取對應項目下的文件

zip(file, out, base, eavlProName, i);

}

}

}

}

}else{if (base.length() > 0) {

out.putNextEntry(newZipEntry(base));

}else{

out.putNextEntry(newZipEntry(souceFile.getName()));

}

FileInputStream in= newFileInputStream(souceFile);intb;byte[] by = new byte[1024];while ((b = in.read(by)) != -1) {

out.write(by,0, b);

}

in.close();

}

}

總結

以上是生活随笔為你收集整理的java 打包边下载_JAVA实现边下载边压缩的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。