標籤:des style blog http io ar color os sp
網頁:
<a href="${ctx}/tactivity/redPacketDetailsZip?activityid=${activityid }" target="_blank">匯出昨日openid</a>
java代碼:
/** * 匯出zip檔案。zip內是txt檔案。txt檔案內是500個openid。openid一個一行。 * @author xuyong * @date 2014-09-26 */ @RequestMapping(value = "/redPacketDetailsZip") public String redPacketDetailsZip(ModelMap model, TtakepartQuery query, HttpServletRequest request, HttpServletResponse response) throws Exception { logger.info("redPacketDetailsZip 紅包匯出zip檔案 start..."); // 查詢條件 活動ID為activityid,and參與時間小於DateUtil.getCurrentDate(DateUtil.DATE_FMT) 的10000名未發送過得記錄 String activityidstr = request.getParameter("activityid"); // 活動ID Tactivity tactivity = tactivityManager.getById(Long.valueOf(activityidstr)); Long activityid = new Long(activityidstr); query.setSendTimeZip(DateUtil.getCurrentDate(DateUtil.DATE_FMT)); // 年月日,時間小於此時間的參與 query.setActivityid(activityid);//活動 query.setSortColumns("t.createtime desc "); // 排序 query.setPageSize(10000); // 條數 Page page = ttakepartManager.findPage(query); logger.info("組建檔案 檔案...."); String zipFileName = this.saveFile(request, tactivity.getShopid(), page.getResult()); logger.info("組建檔案 ...."); OutputStream o = response.getOutputStream(); byte b[] = new byte[1024]; File fileLoad = new File(zipFileName); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileLoad.getName(),"utf-8")); response.setContentType("application/zip"); // set the MIME type. response.setHeader("Content_Length", String.valueOf(fileLoad.length())); // get the file length. FileInputStream in = new FileInputStream(fileLoad); // download the file. int n = 0; while ((n = in.read(b)) != -1) { o.write(b, 0, n); } logger.info("redPacketDetailsZip 紅包匯出zip檔案 end."); return null; }
/**儲存檔案,並且聲稱壓縮包,返回壓縮包地址 * * @date Nov 20, 20142:06:25 PM * @param resultList * @return 壓縮包地址 * @throws IOException */ private String saveFile(HttpServletRequest request, Long shopid, List resultList) throws IOException{ logger.info("組建檔案 start...."); String zippath = DateUtil.getCurrentDateStr(DateUtil.DATE_FMT)+resultList.size()+"openid壓縮包.zip"; // 上傳檔案路徑 int pageSize = 3;// 每個檔案儲存體大小500 String txtFullPath = "檔案名稱"; StringBuffer openidBuffer = new StringBuffer(100); // openid內容 List<File> allFilesList = new ArrayList<File>();// 檔案集合 // 儲存匯出檔案。最多20個txt logger.info("組建檔案 txt檔案 start..."); for (int i=0;i<resultList.size();i++){ Ttakepart ttakepart = (Ttakepart)resultList.get(i); if (i%pageSize==0 && i>0){ txtFullPath = FileUtil.getFilePath(request, Constant.CARDDOWN_PATH, shopid, i+".txt"); logger.info("組建檔案 txt檔案 txtFullPath="+txtFullPath); // 建立儲存檔案 FileUtil.saveOneFile(txtFullPath, openidBuffer.toString()); allFilesList.add(new File(txtFullPath)); // 重設內容 openidBuffer = new StringBuffer(100); } openidBuffer.append(ttakepart.getWxno()).append("\n"); // openid內容 } if (openidBuffer.length() > 0){// 還存在檔案剩餘內容,儲存檔案 txtFullPath = FileUtil.getFilePath(request, Constant.CARDDOWN_PATH, shopid, "last.txt"); logger.info("組建檔案 txt檔案 txtFullPath="+txtFullPath); // 建立儲存檔案 FileUtil.saveOneFile(txtFullPath, openidBuffer.toString()); allFilesList.add(new File(txtFullPath)); // 重設內容 openidBuffer = new StringBuffer(100); } logger.info("組建檔案 txt檔案 end..."); logger.info("壓縮txt檔案 start..."); // 刪除原有檔案 File zipFile = new File(zippath); if(zipFile.exists()){ zipFile.delete(); } zippath = FileUtil.getFilePath(request, Constant.CARDDOWN_PATH, shopid, zippath); // 壓縮檔打包 byte[] buffer = new byte[1024]; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippath)); //通過相應的業務來封裝自己的list。 FileInputStream fis = null; for (File eachFile : allFilesList) { fis = new FileInputStream(eachFile); out.putNextEntry(new ZipEntry(eachFile.getName())); int len; while ((len = fis.read(buffer)) > 0) { out.write(buffer, 0, len); } out.closeEntry(); fis.close(); logger.info("刪除檔案 txt檔案 txtFullPath="+eachFile.getCanonicalPath()); eachFile.delete(); } out.close(); logger.info("壓縮txt檔案 end."); logger.info("組建檔案 end....zippath="+zippath); return zippath; }
/**儲存檔案 * * @date Nov 20, 20144:27:49 PM * @param txtFullPath 檔案全路徑 * @param fileContent 檔案內容 * @throws IOException */ public static void saveOneFile(String fullPath, String fileContent) throws IOException { File file = new File(fullPath); // 建立檔案 if (!file.exists()){ if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); // 建立目錄 } file.createNewFile(); } // 儲存檔案 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); bufferedOutputStream.write(fileContent.toString().getBytes()); bufferedOutputStream.close();// 寫入內容 }
產生多個檔案,並打包成壓縮包,在網頁上下載。