您的位置:首页 > 编程语言 > Java开发

struts2文件下载

2016-06-18 19:19 337 查看

struts2文件下载

1、引入需要的jar包
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2、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>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="xueyoupackage" namespace="/" extends="struts-default">
<action name="aa">
<result>WEB-INF/content/aa.jsp</result>
</action>

<action name="download" class="com.xueyoucto.random.DownloadFile">
<result type="stream">
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">4096</param>
</result>
<result name="login">WEB-INF/content/login.jsp</result>
</action>

</package>
</struts>
3、aa.jsp和login.jsp
aa.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>aa.jsp</title>
</head>
<body>
<h1>aa.jsp</h1>
<form action="download.action" method="post">
<input type="submit" value="下载"/>
</form>
</body>
</html>
login.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>login.jsp</title>
</head>
<body>
<h1>login</h1>
<form action="">
用户名:<input type="text"/><br/>
密码:<input type="password"/><br/>
<input type="submit"/>
</form>
</body>
</html>
4、action
package com.xueyoucto.random;

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

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadFile extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 3115966211934483866L;
InputStream fileInputStream;
String fileName;

public InputStream getFileInputStream() {
return fileInputStream;
}

public void setFileInputStream(InputStream fileInputStream) {
this.fileInputStream = fileInputStream;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String execute(){
ActionContext ctx = ActionContext.getContext();
Map session = ctx.getSession();
String user = (String)session.get("user");
System.out.println(user);
if(user == null){
//		if(user != null){
File file = new File("d:/123/123.txt");
fileName = file.getName();
try {
fileInputStream = new FileInputStream(file);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return ActionSupport.SUCCESS;
}
return ActionSupport.LOGIN;
}

}
5、效果截图



6、如果想在下载前验证是否登录,可以在action中把注释放开。user!=null即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: