about 7 years ago
之前有用個不錯的檔案下載套件,既不會被瀏覽器檔,也可以異步下載,就記錄下來
https://github.com/johnculviner/jquery.fileDownload
使用方式
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);
});
//這種寫法有時會被擋
//window.open("picCustom.do?act=downloadZipImages&jsonstr="+JSON.stringify(jsonObj));
//用這套件就沒有阻擋問題
$.fileDownload("picCustom.do", {
httpMethod: "POST",
data: {act: "downloadZipImages", jsonstr: JSON.stringify(jsonObj)}
});
}
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;
}
紀錄用途所以沒有簡化程式碼