Java Ext Struts1的下载文件(多重文件夹压缩)
2014-01-10 14:11
281 查看
1.这是Ext中的js代码:
2.这是action中方法
4.下面就是把文件夹及其中的文件夹和文件压缩成".zip"格式的文件:
{ text : '下载申报中报文', iconCls : 'acceptIcon', handler : function() { Ext.Msg.confirm("提示", '确定读取申报中报文?', function(btn) { if (btn == "yes") { var myMask = new Ext.LoadMask(Ext.getBody(), { msg : "正在读取申报中报文..." }); myMask.show(); var url = 'msgInfo.ered?reqCode=downloadFtpMsg&APPTYPE='+appType; exportFile(url); myMask.hide(); } }); } }这串代码中的exportFile(url)方法如下:
function exportFile(url) { var exportIframe = document.createElement('iframe'); exportIframe.src = url; exportIframe.style.display = "none"; document.body.appendChild(exportIframe); hideWaitMsg(); }
2.这是action中方法
/** * 读取申报中报文 * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public ActionForward downloadFtpMsg(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { InputStream in = null; OutputStream out = null; try { String path = sendManager.downloadFtpMsgInfo(request); response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("报文文件.zip", "UTF-8")); in = new FileInputStream(path); int len = 0; byte[] buffer = new byte[1024]; out = response.getOutputStream(); while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } catch (Exception e) { logger.error(e.getMessage(), e); write(encodeObjectJson(new BaseDto<String, Object>(false, e.getMessage())), response); return mapping.findForward(null); } finally { if (in != null) { try { in.close(); } catch (Exception e) { throw new RuntimeException(e); } } } return mapping.findForward(null); }3.这个是读取FTP上的文件:
/** * 读取申报中报文 * @see com.saicfc.saicifx3.service.IfxMsgSendService#downloadFtpMsgInfo(javax.servlet.http.HttpServletRequest) */ public String downloadFtpMsgInfo(HttpServletRequest request) throws Exception { String baseSendPath = WebUtils.getSysParam(Constants.PARAM_XMLROOTPATH, request); if (StringUtils.isBlank(baseSendPath)) { throw new RuntimeException("缺少键名为[" + Constants.PARAM_XMLROOTPATH + "]的[报文根目录]系统参数."); } Map<String, String> dataTypeMap = WebUtils.getCodeMapByField(Constants.FIELD_MSGFILETYPE, request);// 数据类型文件名前缀 Map<String, String> msgTypeMap = new HashMap<String, String>(); for (String key : dataTypeMap.keySet()) { msgTypeMap.put(dataTypeMap.get(key), key); } msgTypeMap.put("JSHU", "PZJ_COMPANY"); logger.info("------------------------处理申报结果------------------------------"); String sendPath = baseSendPath + "_TEMP\\"; // 待读取申报中报文 File cLock = new File(sendPath + Constants.FILELOCK); String sendDirName = ""; String zipPath = ""; try { addLock(cLock); boolean alertFlag = true;// 没有申报中报文提示标识 sendDirName = baseSendPath + "\\FTPFILE_Download\\Send\\"; // 申报中报文目录 // 远程申报目录 ftpClient.setProperties(WebUtils.getSysParam(request)); String[] appType = request.getParameter("APPTYPE").split(","); for (int i = 0; i < appType.length; i++) { String path = WebUtils.getCodeDesc(MSGFILEPATH, appType[i], request); if (path == null) { throw new RuntimeException("缺少字段[MSGFILEPATH]代码[" + appType[i] + "]的数据字典"); } String ftpDir = path + "Data\\Send\\"; // 下载申报中报文 logger.info("------------------------下载读取申报中报文[" + ftpDir + "]"); ftpClient.open(); ftpClient.downLoadFile(ftpDir, sendDirName); ftpClient.close(); alertFlag = false; } if (alertFlag) { throw new RuntimeException("没有读取到申报中报文信息!"); } else { File file = new File("C:\\waihui"); if (!file.exists() && !file.isDirectory()) { logger.debug(file + "is not Exists!"); file.mkdir(); } zipPath = "C:\\waihui\\waihui.zip"; zip(zipPath, new File(sendDirName)); } } catch (Exception e) { e.printStackTrace(); throw e; } finally { deleteFile(cLock); } return zipPath; }
4.下面就是把文件夹及其中的文件夹和文件压缩成".zip"格式的文件:
/** * 把文件夹及其中的文件夹和文件压缩成".zip"格式的文件 * @param zipFileName * @param inputFile * @throws Exception */ private void zip(String zipFileName, File inputFile) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); if (inputFile.isDirectory()) { File[] fl = inputFile.listFiles(); for (int i = 0; i < fl.length; i++) { zip(out, fl[i], fl[i].getName()); } } else { zip(out, inputFile, ""); } logger.info("zip done"); out.close(); } private void zip(ZipOutputStream out, File f, String base) throws Exception { if (f.isDirectory()) { File[] fl = f.listFiles(); out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; logger.info(base); for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } } else { out.putNextEntry(new org.apache.tools.zip.ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; logger.info(base); while ((b = in.read()) != -1) { out.write(b); } in.close(); } }
相关文章推荐
- 使用WinRar压缩文件或文件夹并下载
- 下载文件夹里面的所有文件,并压缩成.zip压缩包的形式
- asp.net实现文件夹及文件压缩,并实现下载(三)——文件超过150M
- Java 小程序 实现网络下载图片到制定文件夹,压缩和删除文件
- 解压assets资源文件夹的压缩文件,并下载到SD卡中。
- .net压缩文件夹及下载压缩文件
- spring mvc 批量下载压缩文件夹中文件
- asp.net实现文件夹及文件压缩,并实现下载(一)
- 将一个文件夹的内容存到一个压缩文件并下载
- asp.net实现文件夹及文件压缩,并实现下载
- 将文件中的文件以及文件夹压缩,并下载
- asp.net实现文件夹及文件压缩,并实现下载(二)——文件超过150M
- java web 实现把文件夹下的所有文件压缩成zip并实现下载(原文链接)http://blog.csdn.net/sxdtzhaoxinguo/article/details/20550537)
- java压缩文件,在线下载文件并压缩
- java多文件压缩下载
- springmvc和servlet在上传和下载文件(保持文件夹和存储数据库Blob两种方式)
- java——压缩文件或者文件夹
- 动态解析文件夹下所有的压缩文件
- php压缩下载文件处理
- java FTP 工具类 同步文件夹 下载文件..等操作(FtpHelper 需要commons-net-3.5.jar)