struts2 实现文件上传和下载

2014-11-24 03:21:50 · 作者: · 浏览: 0

项目目录:

\

web.xml:主要是配好struts2.

  

  
	
   
    uploadTest
   

	
   
	
    
    
     struts2
     
    
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
     
   
	
    
    
     struts2
     
    
     /*
     
   

	
    
    
     index.jsp
     
   


  

struts.xml:配好上传下载的两个action。

  



  

	
   

	
    
     
     
     
      index.jsp
      
     
      index.jsp
      
      
       
      1024000 
       application/msword,image/jpeg  
      
      
     
     
     
      
       
      application/octet-stream 
       
      inputStream 
       
      attachment;filename="${fileName}" 
      4096 
      
     
   


  

UploadAction.java

package upload;

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

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private File myFile;//上传的文件,对应表单的file的name属性
	private String myFileContentType;//文件类型,xxxContentType,xxx对应表单file的name属性
	private String myFileFileName;

	@Override
	public String execute() throws Exception {
		if (myFile == null) {
			this.addFieldError("file", "文件不能为空,请选择");
			return INPUT;
		} else {
			InputStream is = new FileInputStream(this.getMyFile());
			OutputStream os = new FileOutputStream(new File("F:/", this.getMyFileFileName()));
			byte[] buf = new byte[1024];
			int length = 0;
			while ((length = is.read(buf)) > 0) {
				os.write(buf, 0, length);
			}
			is.close();
			os.close();
		}
		return SUCCESS;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

}

DownloadAction.java

package upload;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private String fileName;//要下载的文件名
	private InputStream inputStream;

	@Override
	public String execute() throws Exception {
		inputStream = ServletActionContext.getServletContext().getResourceAsStream("/" + fileName);
		return SUCCESS;
	}

	public String getFileName() {
		return fileName;
	}

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

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
}

index.jsp:访问:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>





  
Insert title here



	
  
	
  
		
   
		
   
	
  
	
	
  
	下载index.jsp文件
	


就这样就可以实现基本的上传下载功能了,要实现复杂一点的再往上面加东西。


参考博客:http://www.blogjava.net/thisliy/archive/2009/08/14/291153.html