码迷,mamicode.com
首页 > 其他好文 > 详细

Struts的文件下载功能实现代码

时间:2016-02-24 17:28:35      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

Action:

package com.tengfeiyang.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

/**
* @author yangtf *  文件下载 16/02/23 */ public class FileDownloadAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getDownloadFile()throws Exception{ String path; if ("Java".equals(fileName)) { path = "C:\\Users\\yangtf\\Desktop\\Java.pdf"; // 绝对路径 File file = new File(path); this.fileName = "JavaAnalysisReport.pdf"; return new FileInputStream(file); } if ("PHP".equals(fileName)) { path = "C:\\Users\\yangtf\\Desktop\\PHP.pdf"; File file = new File(path); this.fileName = "PHPAnalysisReport.pdf"; return new FileInputStream(file); } return null; } @Override public String execute() throws Exception { return SUCCESS; } }

XML:

<action name="fileDownload" class="com.tengfeiyang.action.FileDownloadAction">
            <result name="success" type="stream">
                <param name="contentDisposition">attachment;filename=${fileName}</param>
                <param name="inputName">downloadFile</param>
                <param name="bufferSize">1024</param>
            </result>
        </action>

jsp:

<a href="fileDownload.action?fileName=Java">Java分析文档下载</a><br>
<a href="fileDownload.action?fileName=PHP">PHP分析文档下载</a>
 

Struts的文件下载功能实现代码

标签:

原文地址:http://www.cnblogs.com/tf-Y/p/5213431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!