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

struts2文件下载

时间:2018-03-18 23:21:44      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:param   fileinput   getter   方式   bsp   乱码   pdf   name   space   

struts2文件下载

  文件的下载:

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

  2). 具体使用细节参看 struts-2.3.15.3-all/struts-2.3.15.3/docs/WW/docs/stream-result.html

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

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

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

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

  4). 以上参数可以在 Action 中以 getter 方法的方式提供,然后在result中使用ognl表达式来获取动态的值!


package com.atguigu.struts2.download.app; import java.io.FileInputStream; import java.io.InputStream; import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownLoadAction extends ActionSupport { private long contentLength; //以字节为单位的流长度(浏览器显示一个进度条)。
	private String type = "video/mp4";
	private String fileName;
	private InputStream inputStream;

	public String execute() throws Exception {

		fileName = "7.文件下载(javaweb的形式)_.mp4";
		fileName = new String(fileName.getBytes("gbk"), "iso8859-1");// 解决文件下载时文件名的中文乱码问题
		ServletContext servletContext = ServletActionContext.getServletContext();
		String file = servletContext.getRealPath("/files/7.文件下载(javaweb的形式)_.mp4");
		inputStream = new FileInputStream(file);
		contentLength = ((FileInputStream) inputStream).getChannel().size();// 注意这里获取的文件大小如果比实际文件的小,那么会导致文件下载不完整,int最大是2147483647按字节大约2g
		System.out.println(contentLength);
		return SUCCESS;
	}

	private String getType() {
		return this.type;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public long getContentLength() {
		return contentLength;
	}

	public String getFileName() {
		return fileName;
	}

}

  

<?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="default" namespace="/" extends="struts-default">

		<action name="testDownload" class="com.atguigu.struts2.download.app.DownLoadAction">
			<result type="stream">
				<param name="contentType">${type}</param>
				<param name="inputName">inputStream</param>
				<param name="contentLength">${contentLength}</param>
				<param name="contentDisposition">attachment;filename="${fileName}"</param>
				<!-- <param name="bufferSize">1024</param> 默认就是1024 -->
			</result>
		</action>

	</package>

注意事项:

  文件下载两个重要的头:
  1:contentType("文件的mime类型")指定下载的文件mine类型
  不指定默认所有类型都支持(即如果下载的文件类型是不一样的,则不用指定,默认都支持)

 

  2:header:
  content-disposition(内容的处理形式)
  取值:
  A,contentDisposition默认是 inline(内联的),
  比如说下载的文件是文本类型的,就直接在网页上打开,不能直接打开的才会打开下载框自己选择

  B,attachment(附件);filename="下载时提示并且是下载生成的文件的文件名,可以与服务器上的原文件名不一致"
  注意:为了支持中文,文件名需要转码处理,
  filename=new String(filename.getBytes("gbk"),"iso8859-1");

struts2文件下载

标签:param   fileinput   getter   方式   bsp   乱码   pdf   name   space   

原文地址:https://www.cnblogs.com/zxwBj/p/8598071.html

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