java web批量下载
生活随笔
收集整理的這篇文章主要介紹了
java web批量下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java批量下載,將多文件打包成zip格式下載
現在的需求的:
根據產品族、產品類型,下載該產品族、產品類型下面的pic包;
pic包是zip壓縮文件;
t_product表:
這些包以blob形式存在另一張表中:
t_imagefile表:
現在要做的是:將接入網、OLT下面的兩個包downloadPIC:MA5800系列-pic.zip 和?MA5900-pic.rar一起打包成zip壓縮文件下載下來;
代碼:
ProductController.java:
/*** 根據產品族、產品類型下載照片包*/@RequestMapping("/downloadwBatch")public void downloadwBatch(HttpServletRequest request, HttpServletResponse response, String productFamily, String productType){//http://localhost:8080/MySSM/downloadwBatch?productFamily=接入網&productType=OLTtry {productFamily = new String(productFamily.getBytes("iso-8859-1"), "utf-8");productType = new String(productType.getBytes("iso-8859-1"), "utf-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}//獲取要下載的照片包名Map<String, String> params = new HashMap<String, String>();params.put("productFamily", productFamily);params.put("productType", productType);List<String> packageNames = productService.getPackageNamesByFamilyAndType(params);//根據包名獲取待下載的文件 文件名-字節數組的形式Map<String, byte[]> files = new HashMap<String, byte[]>();for(String packageName : packageNames){byte[] f = productService.getPackage(packageName);if(f!=null){files.put(packageName, f);}}//設置下載的壓縮包名String zipName = productFamily + "_"+ productType + ".zip";//根據文件,進行壓縮,批量下載if(files.size() > 0){productService.downloadBatchByFile(response, files, zipName);}}ProductService.java:
/*** 根據包名獲取文件*/public byte[] getPackage(String packageName){byte[] bag = null;try{ImageFile m = productMapper.getPackage(packageName);if(m!=null){bag = m.getPicture();}}catch(Exception e){e.printStackTrace();}return bag;}/*** 根據產品族、產品類型 獲取待下載的包名* @param params* @return*/public List<String> getPackageNamesByFamilyAndType(Map<String, String> params) {List<String> packageNames = productMapper.getPackageNamesByFamilyAndType(params);return packageNames;}/*** 根據文件,進行壓縮,批量下載* @param response* @param files* @throws Exception */public void downloadBatchByFile(HttpServletResponse response, Map<String, byte[]> files, String zipName){try{response.setContentType("application/x-msdownload");response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(zipName, "utf-8"));ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());BufferedOutputStream bos = new BufferedOutputStream(zos);for(Entry<String, byte[]> entry : files.entrySet()){String fileName = entry.getKey(); //每個zip文件名byte[] file = entry.getValue(); //這個zip文件的字節BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(file));zos.putNextEntry(new ZipEntry(fileName));int len = 0;byte[] buf = new byte[10 * 1024];while( (len=bis.read(buf, 0, buf.length)) != -1){bos.write(buf, 0, len);}bis.close();bos.flush();}bos.close();}catch(Exception e){e.printStackTrace();}}ProductMapper.java:
/*** 根據包名獲取文件*/public ImageFile getPackage(String packageName) throws Exception;/*** 據產品族、產品類型 獲取待下載的包名*/public List<String> getPackageNamesByFamilyAndType(Map<String, String> params);ProductMapper.xml:
<!-- 根據包名獲取文件 --><select id="getPackage" parameterType="java.lang.String" resultType="com.cy.model.ImageFile">select * from t_imagefile where packageName = #{packageName}</select><!-- 跟據產品族、產品類型 獲取待下載的包名 --><select id="getPackageNamesByFamilyAndType" parameterType="java.util.Map" resultType="java.lang.String">select packageName from t_imagefile m join t_product p on m.packageName = p.downloadPicwhere p.productFamily = #{productFamily} and p.productType = #{productType}</select>測試:
在瀏覽器中輸入:http://localhost:8080/MySSM/downloadwBatch?productFamily=接入網&productType=OLT
下載結果如下:
簡單的demo
package com.msznyl;import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Download {public static void main(String[] args) {//需要壓縮的文件--包括文件地址和文件名String [] path ={"E:\\360DocProtect\\01.txt","E:\\360DocProtect\\02.docx"};// 要生成的壓縮文件地址和文件名稱String desPath = "D:\\DOWNLOADS\\new.zip";File zipFile = new File(desPath);ZipOutputStream zipStream = null;FileInputStream zipSource = null;BufferedInputStream bufferStream = null;try {//構造最終壓縮包的輸出流zipStream = new ZipOutputStream(new FileOutputStream(zipFile));for(int i =0;i<path.length;i++){File file = new File(path[i]);//將需要壓縮的文件格式化為輸入流zipSource = new FileInputStream(file);//壓縮條目不是具體獨立的文件,而是壓縮包文件列表中的列表項,稱為條目,就像索引一樣ZipEntry zipEntry = new ZipEntry(file.getName());//定位該壓縮條目位置,開始寫入文件到壓縮包中zipStream.putNextEntry(zipEntry);//輸入緩沖流bufferStream = new BufferedInputStream(zipSource, 1024 * 10);int read = 0;//創建讀寫緩沖區byte[] buf = new byte[1024 * 10];while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1){zipStream.write(buf, 0, read);}}} catch (Exception e) {e.printStackTrace();} finally {//關閉流try {if(null != bufferStream) bufferStream.close();if(null != zipStream) zipStream.close();if(null != zipSource) zipSource.close();} catch (IOException e) {e.printStackTrace();}}} }總結
以上是生活随笔為你收集整理的java web批量下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端第一天,第六十五天
- 下一篇: SOLIDWORKS+CAD+UG软件培