zoukankan      html  css  js  c++  java
  • Struts2实现文件下载

    实现文件下载:

    1.导包:commons-fileload-xx.jar

                 commons-io-xx.jar

    2.jsp页面:

    <s:iterator value="#session.fileList">
    
          <a href="download.action?fileName=<s:property />"><s:property /></a>
    
            <br>
    
    </s:iterator>

    3.创建Action:DownLoAction

    import java.io.BufferedInputStream;
    
    import java.io.FileInputStream;
    
    import java.io.FileNotFoundException;
    
    import java.io.InputStream;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class DownloadAction extends ActionSupport {
    
    	private String fileName;	//要下载的文件名
    
    	private String path;	//文件目录
    
    	
    
    	private InputStream inputStream; //读取文件,输入流
    
    	private String contentType; //文件类型
    
    	
    
    	
    
    	public String getContentType() {
    
    		return contentType;
    
    	}
    
    	public void setContentType(String contentType) {
    
    		this.contentType = contentType;
    
    	}
    
    	public void setInputStream(InputStream inputStream) {
    
    		this.inputStream = inputStream;
    
    	}
    
    	public String getFileName() {
    
    		return fileName;
    
    	}
    
    	public void setFileName(String fileName) {
    
    		this.fileName = fileName;
    
    	}
    
    	public String getPath() {
    
    		path=ServletActionContext.getServletContext().getRealPath("/upload");
    
    		return path;
    
    	}
    
    	public void setPath(String path) {
    
    		this.path = path;
    
    	}
    
    	
    
    	public InputStream getInputStream() throws FileNotFoundException {
    
    		inputStream = new BufferedInputStream(
    
                        new FileInputStream(this.getPath()+"/"+this.getFileName()));
    
    		return inputStream;
    
    	}
    
    	@Override
    
    	public String execute() throws Exception {
    
    		
    
    		return super.execute();
    
    	}
    
    }
    

    4.配置action

    <action name="download" class="com.action.DownloadAction">
    
      <result type="stream">
    
        <param name="contentType">application/octet-stream</param>//文件类型
    
        <param name="inputName">inputStream</param>//路径+文件名
    
        <param name="contentDisposition">attachment;filename="${fileName}"</param>
    
        <param name="bufferSize">50000</param>//缓冲区大小
    
      </result>
    
    </action>
     
  • 相关阅读:
    Sqoop详细知识
    数据分析与数据挖掘
    数仓 星形模型与雪花模型 简单理解
    mapreduce多进程与spark多线程比较
    ETL工具总结
    数据仓库概述
    利用 Azure Devops 创建和发布 Nuget 包
    设置 Nuget 本地源、在线私有源、自动构建打包
    简单理解 OAuth 2.0 及资料收集,IdentityServer4 部分源码解析
    asp.net core 健康检查
  • 原文地址:https://www.cnblogs.com/next-open/p/3462165.html
Copyright © 2011-2022 走看看