//===============服务端程序
package com.servlet;
import java.io.BufferedInputStream;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.util.StringUtil;
public class FileDownload extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
protected final Log log = LogFactory.getLog(getClass());
public void doGet(HttpServletRequest request,HttpServletResponse response){
this.doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response){
//01:获取上传目录(downloadPath)
String downloadPath = request.getParameter("downloadPath");
log.info("==========文件下载请求!====================");
log.info("请求来源:"+request.getRemoteAddr());
log.info("请求URL:"+request.getRequestURL());
//02: 检测上传目录是否存在
File file = new File(StringUtil.toStringWithTrim(downloadPath));
if(!file.exists()){
log.error("回执文件下载找不到源目录!"+downloadPath);
return;
}
//03: 获得文件名数组
String[] filenames = file.list();
int fileCount=0;
byte[] buffer = null;
StringBuffer sb = new StringBuffer();
log.info("已加载回执文件["+filenames.length+"]个!");
for(int i=0;i<filenames.length;i++){
buffer = null;
String filename = filenames[i];
//04: 获取文件后缀名,通过后缀名判断文件是否是需要下载的文件
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
if("XML".equals(ext)){
//05: 读取下载文件。
InputStream fis = null;
try {
fis = new BufferedInputStream(new FileInputStream(downloadPath+"/"+filename));
buffer = new byte[fis.available()];
fis.read(buffer);
String str = xmlAnalyze(new String(buffer).replace("\r\n", "").replace("\t",""),downloadPath+"/"+filename);
sb.append(str+","+filename+";");
log.info("文件"+downloadPath+"/"+filename+"已读!");
} catch (Exception e) {
log.info("文件"+downloadPath+"/"+filename+"读取失败");
log.error(e);
continue;
} finally{
try {
if(null!=fis){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
log.error(e);
}
}
}else{
log.info("文件"+downloadPath+"/"+filename+"为非回执文件,未读!");
}
fileCount++;
}
//06:定义文件内容字符串容器
String str = sb.toString();
PrintWriter out = null;
try {
out= response.getWriter();
if(!StringUtil.isEmpty(str) || filenames.length>0){
//08:将文件内容送回客户端程序
out.print(str.substring(0, str.length()-1));
}else{
out.print("");
}
out.flush();
log.info("成功下载回执文件"+fileCount+"个!");
log.info("失败下载回执文件"+(filenames.length-fileCount)+"个!");
} catch (IOException e) {
log.info("下载失败");
log.error(e);
} catch(Throwable ex){
log.info("下载失败");
log.error(ex);
}finally{
if(out!=null)
out.close();
}
}
/*
* 文件解析
* */
public String xmlAnalyze(String xmlStr,String filePath){
//01:定义xml报文解析对象
Document doc = null;
String paymentNo = "";
String status = "";
try{
//02: 解析xml报文内容
doc = DocumentHelper.parseText(xmlStr);
Element rootElt = doc.getRootElement();
paymentNo = rootElt.elementTextTrim("paymentNo");
status = rootElt.elementTextTrim("status");
if(StringUtil.isEmpty(paymentNo)){
log.error("回执文件"+filePath+"的支付号为空!");
}
if(StringUtil.isEmpty(status)){
log.error("回执文件"+filePath+"的状态为空!");
}
}catch(Exception e){
log.error(e);
return ",";
}
//03:组装解析结果并返回
return paymentNo+","+status;
}
}
//===============客户端程序
package com.test;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class MyClientDown {
public static void main(String [] arg){
HttpClient httpclient = new HttpClient();
PostMethod postMethod = new PostMethod("http://10.0.40.170:8080/File-swap/fileDownload");
NameValuePair[] data ={new NameValuePair("downloadPath","e:/down")};
postMethod.setRequestBody(data);
int responseCode;
try {
responseCode = httpclient.executeMethod(postMethod);
if (HttpStatus.SC_OK==responseCode) {
String responseBody = new String(postMethod.getResponseBody());
System.out.println("================\n"+responseBody);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
postMethod.releaseConnection();
}
}
}