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


 

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

1.1、页面配置部分

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

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值