Struts2实现多文件上传

2014-11-24 02:03:49 · 作者: · 浏览: 0

\


struts2配置文件

  




  


	
    
     
     /uploadFile 
     
      /success.
      jsp
      
     
   



  

action配置

package 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 UplodeAction extends ActionSupport {

	private String title;
	private File[] upload;
	private String[] uploadContentType;
	private String[] uploadFileName;
	private String savePath;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}

	public String upload() throws Exception {

		System.out.println("upload=============");

		
		
		for (int i = 0; i < upload.length; i++) {
			System.out.println("======================="+i);
			FileInputStream fis = new FileInputStream(upload[i]);

			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
					+ getTitle()+uploadFileName[i]);

			byte[] buffer = new byte[1024];

			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}

			fos.close();
			fis.close();
		}

		return SUCCESS;

	}

	

	

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public String[] getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

}
index页面即上传界面的设置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>

<html>



My JSP 'index.jsp' starting page

  

  

  

  

  

  



	
  
		
   
		
   
		
   
		
   
		
   
	
  



上传成功的设置

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>





My JSP 'success.jsp' starting page


  

  

  

  

  

  




	上传成功!
	
文件标题: http://blog.csdn.net/dong_martin/article/details/
文件为: " />

web.xml文件配置


  

  
  
   
    Struts2Uplode
   
   
    
    
     index.jsp
     
   
  
    
    
     struts2
     
    
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
     
   
  
    
    
     struts2
     
    
     /*