Struts2day04Struts2.0文件的下载

1.写一个Action,在DownloadAction extends ActionSuport

package com.jsu.struts2.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport{
	private List<String> fileNames;
	private String fname;
	private InputStream downFile;//传给用户的是一个文件流
	/*获得文件夹中的所有文件名称*/
	public String findAll(){
		String path =ServletActionContext.getServletContext().getRealPath("/upload");
		File file  = new File(path);
		File [] files = file.listFiles();
		fileNames = new ArrayList<String>();
		for (File f:files) {
			fileNames.add(f.getName());
		}
		return "list";
	}
	/*执行下载操作*/
	@Override
	public String execute() {
	//获得文件的路径
		String path =ServletActionContext.getServletContext().getRealPath("/upload");
		System.out.println(path);
		String fileRealPath = path+"\\"+fname;
		System.out.println(fileRealPath);
		try {
				downFile = new FileInputStream(fileRealPath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return "success";
	}
	public List<String> getFileNames() {
		return fileNames;
	}
	public void setFileNames(List<String> fileNames) {
		this.fileNames = fileNames;
	}
	public String getFname() {
		try {
			fname = new String(fname.getBytes(),"ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return fname;
	}
	public void setFname(String fname) {
		try {
		//这里编码格式为gbk,在jsp页面的头信息也要设置为gbk
			fname=new String(fname.getBytes("ISO-8859-1"),"gbk");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		this.fname = fname;
	}
	public InputStream getDownFile() {
		return downFile;
	}
	public void setDownFile(InputStream downFile) {
		this.downFile = downFile;
	}
	
	
}

 2.在download.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> </head>
  <body>
  <center>
  <h1>文件下载</h1>
	 <s:iterator var="file" value="fileNames">
	 <a href="down?fname=<s:property value="#file"></s:property>">
	 <s:property value="#file"></s:property></a>
	 </s:iterator>
   </center>
  </body>
</html>

 3.在struts.xml文件配置

<!-- 文件下载 -->
		<action name="down" class="com.jsu.struts2.action.DownloadAction">
		  <result name="list">/download.jsp</result>
		  <!-- 如果Struts2.0框架以流的形式反馈用户的时候,type="stream" -->
		  <result name="success" type="stream">
		    <!-- 指明那个属性是以流的形式反馈 -->
		    <param name="inputName">downFile</param>
		    <!-- 设置缓存区 -->
		    <param name="buffSize">1024</param>
		    <!-- 设置浏览器的打开方式:inline:打开直接显示;
		         attachment:浏览器以附件的形式进行存储
		         filename:文件的名称
		          -->
		    <param name="contentDisposition">attachment;filename=${fname}</param>
		  </result>
		</action>

 4.地址栏访问:http://localhost:8080/struts2_04/down!findAll.action

       注意:upload文件夹下面一定要有文件。

你可能感兴趣的:(struts2)