sturts2下载文件

最近开发中用到sturts2的文件下载,刚开始用的本地下载,上传服务器后发现文件服务器和应用服务器不在同一地址.就会导致下载失败,于是全部改成用url下载的方式下载,并增加下载失败后弹出提示框的操作.

1.download.jsp代码,downloadFileIframe用于文件不存在时弹出提示框

<body>
<head>
  <script type="text/javascript">
     function fileDownload(xpath){
            $("#xpath").val(xpath);
            //window.location.href(""+xpath);
            document.download.submit();
        } 
  </script>
</head>

.................
<a  href="javascript:fileDownload('assetPath');" style="border: 0"> <img src="img/xiaz.jpg" /> </a> 
<form action="fileDownload_downloadLocal.action" name="download" method="post" enctype="multipart/form-data" target="downloadFileIframe">
    <input name="path" id="xpath" type="hidden"/>
    <iframe name="downloadFileIframe" style="display:none"></iframe> 
</form>
.............
</body>

 

2.nofile.jsp 代码,用于文件不存在时提示信息弹出框

<body>
...................
<head>
    <script type="text/javascript">  
       var downloadFileMessage ="<%=request.getAttribute("downloadFileMessage")%>";
       if(downloadFileMessage != "null"){
             alert(downloadFileMessage);
       }
     </script> 

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
.................
<body>

3.struts2 配置文件

<!--下载资源action -->
<action name="fileDownload_*" class="com.zhl.web.resourcecenter.action.FileDownloadAction" method="{1}">
    <result name="success" type="stream">
        <param name="contentCharSet">UTF-8</param>
        <param name="contentType">application/x-download</param>
        <param name="inputName">downloadStream</param>  
        <param name="contentDisposition">attachment;filename="${fileName}"</param>
        <param name="bufferSize">4096</param>
    </result>
    <result name="error">/resource_center/nofile.jsp</result>
</action> 

4.java下载代码

public class FileDownloadAction extends ActionSupport implements ServletRequestAware{
    private static final long serialVersionUID = 3732213485796116174L;
    private String path;// 要下载的文件路径
    private InputStream downloadStream;// 输出流
    private String fileName;// 输出流生成的文件名
    private String downloadFileMessage;
    
    public String getDownloadFileMessage() {
        return downloadFileMessage;
    }

    public void setDownloadFileMessage(String downloadFileMessage) {
        this.downloadFileMessage = downloadFileMessage;
    }

    public InputStream getDownloadStream() {
        return downloadStream;
    }

    public void setDownloadStream(InputStream downloadStream) {
        this.downloadStream = downloadStream;
    }

    public String getFileName() {
        return fileName;
    }public String downloadLocal() {
        String result = "success";
        URL url = null;
        HttpURLConnection connect = null;
        try {
            fileName = path.substring(path.lastIndexOf("/") + 1, path.length());
            fileName = URLEncoder.encode(fileName, "utf-8");
       path = path.replaceAll(" ", "%20");
       //path like http://192.168.0.2/test/test.txt url
= new URL(path); connect = (HttpURLConnection) url.openConnection(); connect.setConnectTimeout(5000); downloadStream = connect.getInputStream(); } catch (Exception e) { downloadFileMessage = "对不起,您下载的文件不存在或者被损坏,请重新选择其他文件下载!"; result = "error"; e.printStackTrace(); } finally { connect = null; url = null; } return result; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public void setFileName(String fileName) { this.fileName = fileName; } @Override public void setServletRequest(HttpServletRequest arg0) { // TODO Auto-generated method stub }

 

 

转载于:https://www.cnblogs.com/cyanqx/p/3481738.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值