使用Struts2实现下载很简单:
1.download.jsp
在这个界面,将获取项目data文件夹下的所有文件,显示名称
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>下载页面</title>
</head>
<body>
<h3>文件列表</h3>
<s:iterator var="each" value="mulFileFileName" status="state">
<s:property value="#state.index"/>
<a href="file/download?sgFileFileName=<s:property value='#each'/>">
<s:property value="#each"/>
</a>
<br/>
</s:iterator>
<s:debug />
</body>
</html>
2.FileAction
这里直接使用《单文件上传》中的sgFileFileName来接收参数
注意加深颜色的几个方法,
package com.deppon.demo.struts.action;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.deppon.demo.struts.util.FileUtil;
import com.opensymphony.xwork2.ActionSupport;
public class FileAction extends ActionSupport {
private static final long serialVersionUID = 3130770865205322375L;
/**
* 这里是有一个对应关系的
*/
private File sgFile;//xxx
private String sgFileFileName;//xxxFileName
private String sgFileContentType;//xxxContentType
private File[] mulFile;
private String[] mulFileFileName;
private InputStream downloadFile;
public InputStream getDownloadFile() {
downloadFile = ServletActionContext.getServletContext().getResourceAsStream("data/" + sgFileFileName);
return downloadFile;
}
public void setDownloadFile(InputStream downloadFile) {
this.downloadFile = downloadFile;
}
public File[] getMulFile() {
return mulFile;
}
public void setMulFile(File[] mulFile) {
this.mulFile = mulFile;
}
public String[] getMulFileFileName() {
return mulFileFileName;
}
public void setMulFileFileName(String[] mulFileFileName) {
this.mulFileFileName = mulFileFileName;
}
public File getSgFile() {
return sgFile;
}
public void setSgFile(File sgFile) {
this.sgFile = sgFile;
}
public String getSgFileFileName() {
//注意这里,如果这里不进行编码转换的话,会出现乱码(好像不同的浏览器不一样,记得IE6好像就不行)
if(sgFileFileName != null) {
String chinese = "";
try {
chinese = new String(sgFileFileName.getBytes("UTF-8"), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return chinese;
} else {
return sgFileFileName;
}
}
public void setSgFileFileName(String sgFileFileName) {
this.sgFileFileName = sgFileFileName;
}
public String getSgFileContentType() {
return sgFileContentType;
}
public void setSgFileContentType(String sgFileContentType) {
this.sgFileContentType = sgFileContentType;
}
/**
* 单文件上传
* @return
*/
public String upload() {
System.out.println("sgFileFileName->" + sgFileFileName);//可以获取文件的名字
System.out.println("sgFileContentType->" + sgFileContentType);
/*
* 这里获取项目的绝对路径,有2种方法
* 目的都是获取ServletContext,然后,通过getRealPath(),来获取路径。
*
*/
//1.使用ActionContext
// ActionContext ac = ActionContext.getContext();
// ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
// String path = sc.getRealPath("/data");
//2.使用ServletActionContext
StringBuilder path = new StringBuilder(ServletActionContext.getServletContext().getRealPath("/data"));
path.append(File.separator);
path.append(sgFileFileName);
FileUtil.save(sgFile, path.toString());
return SUCCESS;
}
/**
* 多文件上传
* @return
*/
public String upload2() {
//输出文件名
for(String fileName : mulFileFileName) {
System.out.println("--------->" + fileName);
}
String base = ServletActionContext.getServletContext().getRealPath("/data");
StringBuilder path = null;
for(int i=0; i<mulFile.length; i++) {
path = new StringBuilder(base);
path.append(File.separator);
path.append(mulFileFileName[i]);
FileUtil.save(mulFile[i], path.toString());
}
return SUCCESS;
}
/**
* 跳转到下载界面
* @return
*/
public String dl_list() {
//显示所有的上传的文件,列表
mulFileFileName = FileUtil.listFileName(ServletActionContext.getServletContext().getRealPath("/data"));
return "dl";
}
/**
* 下载
* @return
*/
public String download() {
try {
//因为使用Get方式将名称传过来,所以需要转码
setSgFileFileName(new String(sgFileFileName.getBytes("ISO-8859-1") , "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("-----------" + sgFileFileName);
return SUCCESS;
}
}
3.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<constant name="struts.multipart.maxSize" value="20971520"/>
<constant name="struts.devMode" value="true" />
<!-- 配置一个抽象的包 -->
<package name="app-default" abstract="true">
</package>
<package name="p_user" namespace="/user" extends="struts-default">
<action name="common" class="com.deppon.demo.struts.action.UserAction">
<result type="redirect">/index.jsp</result>
<result name="checkbox" type="redirect">/checkbox.jsp</result>
</action>
</package>
<package name="p_file" namespace="/file" extends="struts-default">
<action name="common" class="com.deppon.demo.struts.action.FileAction">
<result name="success" type="redirect">/upload_success.jsp</result>
<result name="dl">/download.jsp</result>
</action>
<action name="download" class="com.deppon.demo.struts.action.FileAction" method="download">
<result name="success" type="stream">
<param name="contentType">application/octet-stream;charset=UTF-8</param>
<param name="contentDisposition">attachment;filename="${sgFileFileName}"</param>
<param name="inputName">downloadFile</param><!-- 就是返回流的那个变量名 -->
</result>
</action>
</package>
</struts>
4.小结:
使用Struts2实现下载功能:
1.下载的文件名
2.InputStream类型的变量:(在get方法中,正确的返回需要的文件流)
3.配置文件
4.会有一些乱码问题,注意解决