struts2文件上传与下载

一:文件上传

1.   页面:upload.jsp
struts2文件上传与下载_第1张图片
2.   struts配置文件
<package name="upload_" extends="struts-default">
 	<action name="sssf" class="fly.sun.upload.FileUpload">
 	    <!-- 通过拦截器限制上传文件的类型 -->
 		<interceptor-ref name="defaultStack">
 			<param name="fileUpload.allowedExtensionsSet">txt,jpg</param>
 			
 		</interceptor-ref>
 	
 		<result name="success">/e/success.jsp</result>
 		<result name="input">/e/error.jsp</result>
 		
 	</action>
3.文件上传的Action
package fly.sun.upload;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport {
	
	private File file1;//表单中的上传文件;
	private String file1FileName;//上传文件的名称;
	private String file1ContentType;//上传文件的类型;

	public void setFile1(File file1) {
		this.file1 = file1;
	}

	public void setFile1FileName(String file1FileName) {
		this.file1FileName = file1FileName;
	}

	public void setFile1ContentType(String file1ContentType) {
		this.file1ContentType = file1ContentType;
	}

	@Override
	public String execute() throws Exception {
			//上传文件至upload目录(自己新建的一个目录);
		//得到上传文件目录的路径;
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		//创建目标文件对象;
		File destFile = new File(path, file1FileName);
		//将表单中的上传文件复制到目标文件中去;
		FileUtils.copyFile(file1, destFile);
		
		return SUCCESS;
	}
	

}

二:文件下载

1.首先要在页面获取下载文件列表(也就上上面upload目录下的所有文件)
可以直接在浏览器输入:http:localhost:8080/项目名/down_list.action,文件下载Action如下:
public class DownAction extends ActionSupport {

	//1.-----------文件下载首先要得到文件列表--------------
	public String list(){
	
		//得到upload目录路径
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		File file = new File(path);
		//得到要下载文件的文件名
		String[] fileNames = file.list();
		
		//保存
		ActionContext ac = ActionContext.getContext();
		//得到代表request的map
		Map<String,Object> request = (Map<String, Object>) ac.get("request");
		request.put("fileNames",fileNames);
		return "list";
	}
	
	/**
	 * 2.-----------文件下载--------------
	 * @return
	 */
	
	//1.获取要下载文件的名称;
	private String fileName;
	
	public void setFileName(String fileName) {
		//处理传入的参数中文问题;
		try {
			fileName = new String(fileName.getBytes("ISO8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		//把处理好的文件名赋值
		this.fileName = fileName;
	}
	
   //2,下载提交的业务方法;(在struts.xml中配置返回stream)
	public String down(){
		return "download";
	}
	
	//3,返回文件流的方法;
	public InputStream getAttrInputStream(){
		return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fileName);
	}
	
    //4,下载显示的文件名
	public String getDownFileName(){
		try {
			fileName = URLEncoder.encode(fileName, "utf-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		return fileName;
	}
	
	
}
2.文件下载的配置文件如下:

	<action name="down_*" class="fly.sun.upload.DownAction" method="{1}">
 	   <!-- 下载列表展示 -->
 		<result name="list">/e/list.jsp</result>
 	   <!-- 下载操作 -->
 		<result name="download" type="stream">
 			<!-- 运行下载的文件的类型:指定为所有的二进制文件类型 -->
			   <param name="contentType">application/octet-stream</param>
			   
			   <!-- 对应的是Action中属性: 返回流的属性【其实就是getAttrInputStream()】 -->
			   <param name="inputName">attrInputStream</param>
			   
			   <!-- 下载头,包括:浏览器显示的文件名 -->
			   <param name="contentDisposition">attachment;filename=${downFileName}</param>
			 
			 	<!-- 缓冲区大小设置 -->
			   <param name="bufferSize">1024</param>
 		</result>
 	</action>

3.下载的jsp页面list.jsp如下所示;
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>下载列表页面</title>
  </head>
  
  <body>
  		<table border="1" align="center">
  			<tr>
  				<td>编号</td>
  				<td>文件名</td>
  				<td>操作</td>
  			</tr>
  			<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  			<c:forEach var="fileName" items="${fileNames}" varStatus="vs">
  				<tr>
  					<td>${vs.count}</td>
  					<td>${fileName}</td>
  					<td>
  						<!-- 构建一个url -->
  						<c:url var="url" value="down_down">
  							<c:param name="fileName" value="${fileName}"></c:param>
  						</c:url>
  						<a href="${url}">下载</a>
  					</td>
  				</tr>
  			</c:forEach>
  				
  			
  		</table>
  </body>
</html>



你可能感兴趣的:(struts2.0,文件上传下载)