struts2之文件上传和下载

一个文件上传和下载的例子,思路:先上传几个文件,然后获取文件列表,列表中每个文件对应一个下载链接

文件上传:

Action:get/set方法省略

package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	/*
	 * 文件上传
	 */
	@Override
	public String execute() throws Exception {
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		FileOutputStream fos = new FileOutputStream(new File(path + "/" + getUploadFileName()));
		FileInputStream inputStrem = new FileInputStream(getUpload());
		byte[] byts = new byte[1024];
		int temp = 0;
		while ((temp = inputStrem.read(byts)) > 0) {
			fos.write(byts, 0, temp);
		}
		fos.close();
		inputStrem.close();
		return "success";
	}
} 

jsp中表单:这里使用struts2标签

<s:form action="uploadAction" enctype="multipart/form-data">
    	<s:file name="upload" label="选择文件"></s:file>
    	<s:submit>上传</s:submit>
    </s:form> 

struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="user" extends="struts-default">
		<!--文件上传  -->
		<action name="uploadAction" class="com.action.UploadAction">
			<result name="success">/upsuccess.jsp</result>
		</action>
		<!--文件下载  -->
		<action name="downAction" class="com.action.DownAction">
			<result type="stream">
				<!--文件下载入口  -->
				<param name="inputName">downFile</param>
				<!--告诉浏览器下载此文件,设置文件名  -->
				<param name="contentDisposition">attachment;filename=${tergetfile}</param>
				<!--  -->
				<param name="bufferSize">2048</param>
			</result>
		</action>
		<!--获取文件列表  -->
		<action name="downlistAction" class="com.action.DownAction" method="getFileList">
			<result name="success">/download.jsp</result>
			<result name="error">/500.jsp</result>
		</action>
		<action name="userAction" class="com.action.UserAction" >
			<result name="success" >/ognl.jsp</result>
			<result name="input">/500.jsp</result>
		</action>
		<!--另一种文件上传方法  -->
		<action name="upfile"  class="com.action.UpFile"  method="upfile">
			<interceptor-ref name="fileUpload" >
			<param name="maxmmSize">1024000</param>
			</interceptor-ref> 
			<interceptor-ref name="defaultStack" />
			<result name="success" >/upsuccess.jsp</result>
			<result name="error">/500.jsp</result>
		</action>
	</package>
</struts>   

文件下载:Action

package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownAction extends ActionSupport {
	private String tergetfile;
	/*
	 * 下载文件,返回一个输入流
	 */
	public InputStream getDownFile() throws Exception {
		String path = ServletActionContext.getServletContext().getRealPath("/upload/" + getTergetfile());
		System.out.println(path);
		InputStream in = new FileInputStream(new File(path));
		return in;
	}
	/*
	 * 获取下载文件列表
	 */
	public String getFileList() {
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		File file = new File(path);
		File[] files = file.listFiles();
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute("files", files);
		if (files.length > 0) {
			return "success";
		}
		return "error";
	}
	public String getTergetfile() {
		return tergetfile;
	}
	public void setTergetfile(String tergetfile) {
		this.tergetfile = tergetfile;
	}
}

文件下载JSP:

  <body>
   	<s:iterator value="#session.files" var="file">
   		<a href='downAction?tergetfile=<s:property value="#file.name"/>'><s:property value="#file.name"/></br></a>
   	</s:iterator>
  </body> 


猜你喜欢

转载自blog.csdn.net/qq_37150258/article/details/80948165