Struts2 结合HttpClient 实现远程服务器文件下载

1、只实现远程文件下载未处理远程服务器连接失败的状态

1.1、页面配置部分

<button id="download" class="button" onclick="window.location.href = 'downloadExample.action';return false;">下载模板</button> 
<s:submit id="importButton" value="上传文件" theme="simple" cssClass="button"></s:submit> 
<s:reset  value="重置路径" theme="simple" cssClass="button" ></s:reset>

1.2、Struts2配置文件部分

<!--下载模板--> 
      <action name="downloadExample" class="com.web.action.file.ImportAction"> 
            <param name="downFileName">文件下载.xls</param> 
            <result name="success" type="stream"> 
                <param name="contentType">application/vnd.ms-excel</param> 
                <param name="contentDisposition">attachment;filename="${downloadName}"</param> 
                <param name="inputName">downloadFile</param> 
                <param name="bufferSize">4096</param> 
            </result> 
       </action>

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、页面配置部分

<div><font color="#FF0000">${meg}</font></div>

<button id="download" class="button" onclick="window.location.href = 'downloadExample.action';return false;">下载模板</button> 
<s:submit id="importButton" value="上传文件" theme="simple" cssClass="button"></s:submit> 
<s:reset  value="重置路径" theme="simple" cssClass="button" ></s:reset>

2.2、Struts2配置文件部分

<!--下载模板--> 
      <action name="downloadExample" class="com.web.action.file.ImportAction"> 
            <param name="downFileName">文件下载.xls</param> 
            <result name="success" type="stream"> 
                <param name="contentType">application/vnd.ms-excel</param> 
                <param name="contentDisposition">attachment;filename="${downloadName}"</param> 
                <param name="inputName">downloadFile</param> 
                <param name="bufferSize">4096</param> 
            </result>

            <result name="error">/com/import/download.jsp</result> 
       </action>

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、总结:此项实现,在文件服务器运行正常的情况下文件下载正常,如果文件服务器运行异常则将返回下载页面并提示异常信息。

 
 
posted @ 2012-04-05 15:25  totozlj  阅读(3219)  评论(0编辑  收藏  举报