`
guomingzhang2008
  • 浏览: 156998 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

文件下载 struts2文件下载 struts2注解

阅读更多
1、配置文件方式下载文件:

这种方式,网上有很多例子,我没有试过,我用的是注解方式。


2、注解方式下载文件:
没有上传工程,直接附上代码。
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

/**
 * 文件下载类
 *
 * @author 张国明 guomingzhang2008@gmail.com
 * @version 2012-11-29 上午10:34
 */
@ParentPackage("struts-default")
@Namespace("/test")
@ResultPath(value = "/")
@Results({
        @Result(params = {
                "contentType", "application/octet-stream",
                "inputName", "inputStream",
                "contentDisposition", "attachment;filename=\"${fileName}\"",
                "bufferSize", "4096"},
                name = "download", type = "stream")
})
public class DownloadAction extends ActionSupport {
    /**
     * 下载文档,下载问价的访问入口方法
     *
     * @return resultName
     */
    public String testDownload() {
        // 返回 @Result 注解中的 name 值
        return "download";
    }

    
    /**
     * 获取下载读入流 必须要有方法
     * 在 @Result 注解的参数 params 中, key 为 "inputName" 所对应的 value "inputStream";
     * value "inputStream" ,决定该方法名为getInputStream;
     * 如果 value 的值为 hello ,则该方法名为 getHello;
     * @return InputStream
     */
    public InputStream getInputStream() {
        String filePath = "E:\\works_eclipse\\四.jpg";
        try {
            setFileName(new String("张国明测试 图片.jpg".getBytes(), "iso8859-1") );
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        这种方式取到的流为null,原因不详,所以直接 new FileInputStream
//        return ServletActionContext.getServletContext().getResourceAsStream(filePath);
        try {
            return new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 下载文件的名称,用于动态获取下载文件的名称(可选,即下载的文件名可以写死在@Result的params参数中,但是没有人会这么傻。)
     * 在 @Result 注解的参数 params 中通过 ${fileName} 来获取该属性 fileName 的值;
     * 和EL表达式的取值方式类似,呵呵;
     */
    private String fileName;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}
分享到:
评论
2 楼 1021082712 2015-01-19  
楼主,你的这个代码,你测试了没?有没有问题,当多次点击后,tomcat会不会意外就down掉,当重启tomcat后,会报java.io.EOFException错误?
当return new FileInputStream(filePath);其实这个inputStream没close呢,楼主应该在try catch再加上finally,代码应该这样写:
InputStream stream = null;
        try {
// return new FileInputStream(new File(filePath));
        stream =  new FileInputStream(filePath);//
        return stream;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{// 正确写法必须要close inputStream
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
1 楼 杨莎莎 2014-01-14  
为什么有两个图片文件呢?下的是哪一个啊

相关推荐

Global site tag (gtag.js) - Google Analytics