您的位置:首页 > 编程语言 > Java开发

Java Ext Struts1的下载文件(多重文件夹压缩)

2014-01-10 14:11 281 查看
1.这是Ext中的js代码:

{
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: