SpringMVC 圖片壓縮下載

package com.gosun.cecs.system.manager.action;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class PictureDownload {
	
    public static void getPictureStream(HttpServletRequest request,  
            HttpServletResponse response,
    		List<String> urlList,String carnum)  //圖片url 列表 後面carnum只是用於命名的
            throws Exception {
        ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
      SimpleDateFormat sdf=new SimpleDateFormat("_yyyyMMdd_hhmmss");
        response.setContentType("text/html;charset=UTF-8");  
        request.setCharacterEncoding("UTF-8");   
        long fileLength = 0 ;
        String filename=carnum+sdf.format(new Date())+".rar"; //文件名  爲rar格式
        response.setContentType("application/octet-stream");  
        response.setHeader("Content-disposition", "attachment; filename="  
                + new String(filename.getBytes("gb2312"), "ISO8859-1"));         
        /** 將數據轉爲流返回 */
        for (int i = 0; i < urlList.size(); i++) {
        	fileLength=fileLength+ new File(urlList.get(i)).length();
        	
            String urlString = urlList.get(i);
            urlString = new String(urlString.getBytes("UTF-8"), "GBK");
            URL url = new URL(urlString);
            if (url != null) {
                // File f = new File(urlString);
                //圖片名稱
                ZipEntry zipEntry = new ZipEntry(carnum+"_"+(i+1)+".jpg");
                zipOut.putNextEntry(zipEntry);
                InputStream in = url.openStream();
                byte b[] = new byte[2048];
                int length = 0;
                while ((length = in.read(b)) != -1) {
                    zipOut.write(b, 0, length);
                }
                zipOut.closeEntry();
                in.close();
            }
        }
        
        response.setHeader("Content-Length", String.valueOf(fileLength)); 
        
        zipOut.close();

    }

}
SpringMVC   圖片壓縮的靜態類

 @RequestMapping(value = "picDownload")  
	 public ModelAndView picDownload(HttpServletRequest request,  
	            HttpServletResponse response,
	            String id   
			 ) throws Exception {  
	  VehTrace v=vehTraceManager.getvehTraceById(id);
			List<String> urlList=new ArrayList<String>();
			
			if(!StringUtils.isNullBlank(v.getCar_img_url1()))urlList.add(v.getCar_img_url1());
			if(!StringUtils.isNullBlank(v.getCar_img_url2()))urlList.add(v.getCar_img_url2());
			if(!StringUtils.isNullBlank(v.getCar_img_url3()))urlList.add(v.getCar_img_url3());
			if(!StringUtils.isNullBlank(v.getCar_img_url4()))urlList.add(v.getCar_img_url4());
			if(!StringUtils.isNullBlank(v.getCar_img_url5()))urlList.add(v.getCar_img_url5());
			String carnum=v.getPlate(); 
	  
	        PictureDownload.getPictureStream(request, response, urlList, carnum);
	  
	        return null;  
	    }  
Controller或者Action中 調用的方法 返回null就可以了  

 <form id="downForm" action="vehTrace.do?method=picDownload&id=${id}" method="post">
	</form>

    function downPicture(){
    
        $("#downForm").submit();
        
    }
這裏使用 form表單來接收 二進制流的文件   不能使用ajax接收  因爲ajax接收的類型爲

  • "xml": 返回 XML 文檔,可用 jQuery 處理。
  • "html": 返回純文本 HTML 信息;包含的 script 標籤會在插入 dom 時執行。
  • "script": 返回純文本 JavaScript 代碼。不會自動緩存結果。除非設置了 "cache" 參數。注意:在遠程請求時(不在同一個域下),所有 POST 請求都將轉爲 GET 請求。(因爲將使用 DOM 的 script標籤來加載)
  • "json": 返回 JSON 數據 。
  • "jsonp": JSONP 格式。使用 JSONP 形式調用函數時,如 "myurl?callback=?" jQuery 將自動替換 ? 爲正確的函數名,以執行回調函數。
  • "text": 返回純文本字符串

沒有二進制流的格式   所以這裏就使用form 表單提交後返回的數據來下載壓縮好的圖片.





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章