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

    1). Struts2 中使用 type="stream" 的 result 进行下载

    2). 可以为 stream 的 result 设定如下参数

    contentType: 结果类型
    contentLength: 下载的文件的长度
    contentDisposition: 设定 Content-Dispositoin 响应头. 该响应头指定接应是一个文件下载类型, 一般取值为  attachment;filename=document.pdf.

    inputName: 指定文件输入流的 getter 定义的那个属性的名字. 默认为 inputStream

    bufferSize: 缓存的大小. 默认为 1024
    allowCaching: 是否允许使用缓存
    contentCharSet: 指定下载的字符集

    3). 以上参数可以在 Action 中以 getter 方法的方式提供!

    4). 示例:

    jsp页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
     <a href="downLoad">点击下载success.jsp文件</a>
    </body>
    </html>

    Action:

    package com.elgin.action;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class DownloadAction extends ActionSupport {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	private String contentType;
    	private long contentLength;
    	private String contentDisposition;
    	private InputStream inputStream;
    	
    	
    	public String getContentType() {
    		return contentType;
    	}
    
    	public void setContentType(String contentType) {
    		this.contentType = contentType;
    	}
    
    	public long getContentLength() {
    		return contentLength;
    	}
    
    	public void setContentLength(long contentLength) {
    		this.contentLength = contentLength;
    	}
    
    	public String getContentDisposition() {
    		return contentDisposition;
    	}
    
    	public void setContentDisposition(String contentDisposition) {
    		this.contentDisposition = contentDisposition;
    	}
    
    	public InputStream getInputStream() {
    		return inputStream;
    	}
    
    	public void setInputStream(InputStream inputStream) {
    		this.inputStream = inputStream;
    	}
    
    
    	@Override
    	public String execute() throws Exception {
    		
    		//确定各个成员属性的值:
    		this.contentType="text/html";
    		this.contentDisposition="attachment;filename=success.jsp";
    		String path=ServletActionContext.getServletContext().getRealPath("/files/success.jsp");
    		File file=new File(path);
    		this.inputStream=new FileInputStream(file);
    		this.contentLength=inputStream.available();
    		return SUCCESS;
    	
    	}
    	
     
    }
    


    struts2配置文件配置:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
              "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        
        <package name="com" extends="struts-default">
            <action name="downLoad" class="com.elgin.action.DownloadAction">
               <result type="stream">
                  <param name="bufferSize">2048</param>
               </result>
            </action>
            
        </package>
    </struts>
    



  • 相关阅读:
    Android Sqlite 增删改查
    Android SQLiteOpenHelper Sqlite数据库升级onUpgrade
    Android SQLiteOpenHelper Sqlite数据库的创建与打开
    Android Xml,PullParser,解析
    Android Xml文件生成,Xml数据格式写入
    Android Studio生成get,set,tostring,构造方法
    Android SharedPreference
    Android 文件模式
    Android 权限的由来
    C++ essentials 之 union
  • 原文地址:https://www.cnblogs.com/elgin-seth/p/5293725.html
Copyright © 2011-2022 走看看