Struts2 结合HttpClient 实现远程服务器文件下载
1、只实现远程文件下载未处理远程服务器连接失败的状态
1.1、页面配置部分
1.2、Struts2配置文件部分
文件下载.xls application/vnd.ms-excel attachment;filename=\"${downloadName}\" downloadFile 4096
1.3、Action层中获取远程服务器文件流程序部分
public class ImportAction{
private String downFileName;
private String downloadName;
IFileService fileService;
/**
* @param fileService the fileService to set
*/
public void setEztFileService(IFileService fileService) {
this.fileService = fileService;
}
/**
* @return the downFileName
*/
public String getDownFileName() {
return downFileName;
}
/**
* @param downFileName the downFileName to set
*/
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
/**
* @return the downloadName
*/
public String getDownloadName() {
return downloadName;
}
/**
* @param downloadName the downloadName to set
*/
public void setDownloadName(String downloadName) {
this.downloadName = downloadName;
}
public InputStream getDownloadFile(){
downloadName = fileService.getDownloadFileName(downFileName); //下载文件显示名称转编码
Properties properties = ResourceUtil.getProperties(\"file.properties\");
//取得远程服务器信息配置属性文件file.properties文件为自定义文件放置在web项目src/conf文件夹下
String strRemoteFileUrl = properties.getProperty(\"serverPath\")+ properties.getProperty(\"templateName\"); //取得远程文件路径
InputStream in = fileService.getDownloadFile(strRemoteFileUrl); //调用Service层方法取得远程服务器文件流
return in;
}
}
1.4、Service层接口实现
public class FileService implements IFileService {
public String getDownloadFileName(String downFileName) { //解决下载文件名称中文乱码问题
try {
downFileName = new String(downFileName.getBytes(), \"ISO-8859-1\");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}
public InputStream getDownloadFile(String strRemoteFileUrl) {
// TODO Auto-generated method stub
HttpClient client = new HttpClient();
GetMethod httpGet = new GetMethod(strRemoteFileUrl);
InputStream in = null;
try {
int intResponseCode = client.executeMethod(httpGet);
in = httpGet.getResponseBodyAsStream();
} catch (HttpException e) {
// TODO Auto-generated catch block
//记录日志
} catch (IOException e) {
// TODO Auto-generated catch block
//记录日志
}
return in;
}
}
1.5、读取properties文件工具类部分
public class ResourceUtil {
public static Properties getProperties(String fileName) {
try {
Properties properties = new Properties();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
properties.load(cl.getResourceAsStream(fileName));
return properties;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
1.6、总结:此项实现,在文件服务器运行正常的情况下文件下载正常,如果文件服务器运行异常或已停止运行则在jsp页面部分将会抛出异常。
2、实现远程服务器文件下载并处理文件服务器连接异常,返回提示信息
2.1、页面配置部分
2.2、Struts2配置文件部分
文件下载.xls application/vnd.ms-excel attachment;filename=\"${downloadName}\" downloadFile 4096
2.3、Action层中获取远程服务器文件流程序部分
public class ImportAction{
private String downFileName;
private String downloadName;
private InputStream downloadFile;
IFileService fileService;
/**
* @param fileService the fileService to set
*/
public void setEztFileService(IFileService fileService) {
this.fileService = fileService;
}
/**
* @return the downFileName
*/
public String getDownFileName() {
return downFileName;
}
/**
* @param downFileName the downFileName to set
*/
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
/**
* @return the downloadName
*/
public String getDownloadName() {
return downloadName;
}
/**
* @param downloadName the downloadName to set
*/
public void setDownloadName(String downloadName) {
this.downloadName = downloadName;
}
/**
* @return the downloadFile
*/
public InputStream getDownloadFile() {
return downloadFile;
}
/**
* @param downloadFile the downloadFile to set
*/
public void setDownloadFile(InputStream downloadFile) {
this.downloadFile = downloadFile;
}
public InputStream getDownloadFile(){
downloadName = fileService.getDownloadFileName(downFileName); //下载文件显示名称转编码
Properties properties = ResourceUtil.getProperties(\"file.properties\");
//取得远程服务器信息配置属性文件file.properties文件为自定义文件放置在web项目src/conf文件夹下
String strRemoteFileUrl = properties.getProperty(\"serverPath\")+ properties.getProperty(\"templateName\"); //取得远程文件路径
downloadFile = fileService.getDownloadFile(strRemoteFileUrl); //调用Service层方法取得远程服务器文件流变量名称与配置文件相符
if (null==downloadFile) {
request.setAttribute(\"meg\\"模板下载异常!\");
return ERROR;
} else {
return SUCCESS;
}
}
}
2.4、Service层接口实现
public class FileService implements IFileService {
public String getDownloadFileName(String downFileName) { 件名称中文乱码问题
//解决下载文
try {
downFileName = new String(downFileName.getBytes(), \"ISO-8859-1\");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}
public InputStream getDownloadFile(String strRemoteFileUrl) {
// TODO Auto-generated method stub
HttpClient client = new HttpClient();
GetMethod httpGet = new GetMethod(strRemoteFileUrl);
InputStream in = null;
try {
int intResponseCode = client.executeMethod(httpGet);
in = httpGet.getResponseBodyAsStream();
} catch (HttpException e) {
// TODO Auto-generated catch block
//记录日志
} catch (IOException e) {
// TODO Auto-generated catch block
//记录日志
}
return in;
}
}
2.5、读取properties文件工具类部分
public class ResourceUtil {
public static Properties getProperties(String fileName) {
try {
Properties properties = new Properties();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
properties.load(cl.getResourceAsStream(fileName));
return properties;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
2.6、总结:此项实现,在文件服务器运行正常的情况下文件下载正常,如果文件服务器运行异常则将返回下载页面并提示异常信息。
因篇幅问题不能全部显示,请点此查看更多更全内容