Struts文件下载

DownloadAction:

package crm.action;

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
    private String paramName; 
    private File download; 
    private String downloadFileName;
    private String downloadContentType; 

    public String getParamName() {
  return paramName;
 }

 public void setParamName(String paramName) {
  this.paramName = paramName;
 }

 public File getDownload() {
  return download;
 }

 public void setDownload(File download) {
  this.download = download;
 }

 public String getDownloadFileName() {
  return downloadFileName;
 }

 public void setDownloadFileName(String downloadFileName) {
  this.downloadFileName = downloadFileName;
 }

 public String getDownloadContentType() {
  return downloadContentType;
 }

 public void setDownloadContentType(String downloadContentType) {
  this.downloadContentType = downloadContentType;
 }

 // 从下载文件原始存放路径读取得到文件输出流  
  public InputStream getDownloadFile() throws UnsupportedEncodingException {
     paramName = new String(paramName 
                .getBytes("ISO8859-1"), "utf-8");
        downloadFileName = paramName;

        //相对路径
        return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+downloadFileName);

        //绝对路径

        //return new FileInputStream("E:/贾秋峰备份/软件库/"+downloadFileName);
    } 
  
    // 如果下载文件名为中文,进行字符编码转换 
    public String getDownloadChineseFileName() {
        String downloadChineseFileName = ""; 
        try { 
            downloadChineseFileName = new String(downloadFileName 
                    .getBytes("GBK"), "ISO8859-1"); 
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } 
        return downloadChineseFileName; 
    } 
}

struts.xml:

<!-- 文件下载 -->
  <action name="downloadFile" class="downloadAction">
   <param name="paramName">123.jpg</param>
   <result type="stream">
       <!-- 指定下载的文件类型 -->
    <param name="contentType">application/octet-stream</param>
    <!-- 由getDownloadFile()方法返回下载文件的inputStream -->
    <param name="inputName">downloadFile</param>
    <!-- 下载的文件名 -->
    <param name="contentDisposition">attachment;filename="${downloadChineseFileName}"</param>
    <!-- 下载文件的缓存大小 -->
    <param name="bufferSize">70960</param>
   </result>
  </action>

applicationContext.xml:

<bean id="downloadAction" class="crm.action.DownloadAction" scope="prototype"/>

download.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>上传文件</title>  
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
  </head>
  <body style="text-align: center;">
   <a href="downloadFile.action">123.jpg</a>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值