struts2 实现文件下载

struts2文件下载步骤

1、创建文件下载Action类

2、struts.xml配置

3、注意事项


一、创建文件下载Action类

package com.web.test;

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

import javax.activation.MimetypesFileTypeMap;

/**
 * 文件下载控制层
 * 
 * @author ylf尘风
 *
 */
public class DownloadFileAction {
	// 通用文件下载
	private InputStream downloadFileInputStream;
	private String downloadFileName;// 下载文件名称
	private String downloadFileContentType;// 下载文件类型
	private Long downloadFileLength;//下载文件大小

	public InputStream getDownloadFileInputStream() {
		return downloadFileInputStream;
	}

	public void setDownloadFileInputStream(InputStream downloadFileInputStream) {
		this.downloadFileInputStream = downloadFileInputStream;
	}

	public String getDownloadFileName() {
		return downloadFileName;
	}

	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}

	public String getDownloadFileContentType() {
		return downloadFileContentType;
	}

	public void setDownloadFileContentType(String downloadFileContentType) {
		this.downloadFileContentType = downloadFileContentType;
	}
	
	public Long getDownloadFileLength() {
		return downloadFileLength;
	}

	public void setDownloadFileLength(Long downloadFileLength) {
		this.downloadFileLength = downloadFileLength;
	}

	public String execute() throws UnsupportedEncodingException {
		File file = new File("D:\\temp_upload\\exe4j.zip");
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		this.setDownloadFileName(java.net.URLEncoder.encode("测试.zip", "UTF-8"));// 解决中文乱码
		this.setDownloadFileContentType((new MimetypesFileTypeMap()).getContentType(file));// 获取文件类型
		this.setDownloadFileInputStream(inputStream);
		//文件大小,单位:字节byte(必须和文件实际大小相同,不然会下载失败)
		this.setDownloadFileLength(6148344L);
		return "success";
	}

}


二、struts.xml配置

<?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>
	<!-- 指定Web应用的默认编码,相当于调用request的setCharacterEncoding方法 -->
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<!-- Struts2处理的请求后缀,默认值是action -->
	<constant name="struts.action.extension" value="do" />
	<!-- 开发模式下使用,这样可以打印出更详细的日志信息 -->
	<constant name="struts.devMode" value="false" />
	
	<package name="ssh" namespace="/" extends="struts-default">
		<action name="downloadFile" class="com.web.test.DownloadFileAction">
			<result name="success" type="stream">
				<!-- 文件下载类型,等价于getDownloadFileContentType()方法 -->
				<param name="contentType">${downloadFileContentType}</param>
				
				<!-- 文件下载输入流,等价于getDownloadFileInputStream()方法 -->
				<param name="inputName">downloadFileInputStream</param>
				
				<!-- 文件下载名称,等价于getDownloadFileName()方法 -->
				<param name="contentDisposition">attachment;fileName="${downloadFileName}"</param>
				
				<!-- 文件下载大小(有下载进度显示),等价于getDownloadFileLength()方法 -->
				<param name="contentLength">${downloadFileLength}</param>
				<param name="bufferSize">1024</param>
			</result>
		</action>
	</package>
</struts>


三、注意事项

暂时没有,有问题请留言,有时间会加上去



最终效果
http://192.168.2.177:9000/ssh/downloadFile.do




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值