about 3 years ago

這是在網頁上下載壓縮檔的一個功能

前端網頁
jquery.fileDownload.js Library

<script type="text/javascript" src="js/plug-in/jquery.fileDownload.js"></script>
<script>
function downloadzip(){
    var jsonObj = { filelist : [] };
    
    var picturelist = $("input:checkbox[name=picture]:checked");
    
    if(picturelist.size() == 0){
        return;
    }
    $.each( picturelist, function( index, item ) {
        var pic = new Object;
        pic.picturesid = $(item).attr('picturesid');
        pic.filename = $(item).attr('filename');
        jsonObj.filelist.push(pic);
    });
    $.fileDownload("picCustom.do", {
        httpMethod: "POST",
        data: {act: "downloadZipImages", jsonstr: JSON.stringify(jsonObj)}
    });
}
</script>
public ModelAndView downloadZipImages(HttpServletRequest request, HttpServletResponse response) throws PGException {
        String regEx="[/\\\\:*?\"<>|]";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = null;
        try {
            request.setCharacterEncoding("UTF-8");
            String jsonstr = org.apache.commons.lang.StringUtils.trimToNull(request.getParameter("jsonstr"));
            String jsonstrrep = jsonstr.replaceAll(",", ",");
            JSONObject jsonobj = JSON.parseObject(jsonstrrep);
            JSONArray jsonarray = jsonobj.getJSONArray("filelist");
            List<String> picturesidList = new ArrayList<String>();
            Hashtable<String,String> filenametable = new Hashtable<String,String>();
            for(int i=0;i<jsonarray.size();i++){
                JSONObject pic = jsonarray.getJSONObject(i);
                String picturesid = pic.getString("picturesid");
                String filename = pic.getString("filename")+".jpg";
                picturesidList.add(picturesid);
                //替換掉不合法的檔案名稱

                matcher = pattern.matcher(filename);
                if(matcher.find()){
                    filename = matcher.replaceAll("");
                }
                filenametable.put(picturesid, filename);
            }
            List<Mtkpicturemain> mtkpicturemainList = new ArrayList<Mtkpicturemain>();
            if (picturesidList.size() > 0) {
                mtkpicturemainList = facade.getMtkpicturemainByPicturesid(picturesidList);
            }
            SimpleDateFormat SDformat = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateStr = SDformat.format(new Date());
            String fileName = "PIC_" + dateStr + ".zip";
            // 設定 response 封包中的標頭

            response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
            // 設定欲下載檔案的 ContentType

            response.setContentType("application/zip");
            OutputStream responseOS = response.getOutputStream();
            ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(responseOS));
            zipOut.setEncoding("cp950");
            if (mtkpicturemainList.size() > 0) {
                for (int i = 0; i < mtkpicturemainList.size(); i++) {
                    Mtkpicturemain m = mtkpicturemainList.get(i);
                    // 檔案命名規則:dsrNoGiveName_dsrno_brandcode_picmissionsid_customerno_picmissionsid_照片主檔新增日期_照片主檔新增時間.jpg

                    // 當為自訂任務時因沒有picmissionsid

                    // 此時檔案名稱便為dsrNoGiveName_dsrno_brandcode_picmissionsid_customerno__照片主檔新增日期_照片主檔新增時間.jpg

                    String filename = filenametable.get(m.getPicturesid());
                    zipOut.putNextEntry(new ZipEntry(filename));
                    zipOut.write(m.getPicturedata());
                    zipOut.closeEntry();
                }
                zipOut.flush();
            }
            zipOut.close();
            responseOS.close();
        } catch (Exception e) {
            throw new PGException(this, e);
        }
        return null;
    }
← 第一個spray應用 Jquery圖片延遲載入 →
 
comments powered by Disqus