struts2入门教程三(上传与下载)(二)

2014-11-24 02:01:30 · 作者: · 浏览: 1
public void setSavePath(String savePath) { this.savePath = savePath; } }

struts.xml

  


  
 
	
    
     
     /uploadFiles 
     
       /succ.jsp 
      
     
      /login.jsp
      
     
   
	

  
成功页面

succ.jsp

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



  
    
    My JSP 'index.jsp' starting page
	
  
  
  
  
  <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
   上传成功!
文件标题:
文件为:"/>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>




  
    
    My JSP 'index.jsp' starting page
	
  
  
  
   
  
文件名: 文件:

Struts 多文件上传

一:使用struts2自身的验证

java 代码
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 UploadAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] titles;
	private File[] upload;
	private String[] uploadContentType;
	private String[] uploadFileName;
	private String savePath;
    private String allowTypes;
	
	public String upload() throws Exception {

		if (upload.length <= 0) {
			return null;
		}

		for (int i = 0; i < upload.length; i++) {

			FileInputStream fis = new FileInputStream(getUpload()[i]);
			System.out.println(getUploadFileName());
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
					+ getUploadFileName()[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 boolean check(String type){
		String[] types=allowTypes.split(",");
		
		for (String s: types) {
			if(s.equals(type)){
				return true;
			}
		}
		
		return false;
	}
	
//	public void validate(){
//		
//		for (String Type : uploadContentType) {
//			
//			boolean b=check(Type);
//			
//			if(!b){
//				// 添加FieldError
//				addFieldError("upload", "您要上传的文件类型不正确!");			}
//		}
//		
//	}
//	

	public String[] getTitles() {
		return titles;
	}

	public void setTitles(String[] titles) {
		this.titles = titles;
	}

	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;
	}

	// 设计校验类型的 注入
	public void setAllowTypes(String allowTypes) {
		this.allowTypes = allowTypes;
	}

	
	
	
	/*
	 * // 返回 绝对路径 return
	 * ServletActionContext.getServletContext().getRealPath(savePath);
	 */
}

struts.xml
  


  
  
   
  
   
	
    
     
     /uploadFiles 
     image/png,image/gif,image/jpg,image/jpeg 
     
       /succ.jsp 
      
     
      /index.jsp
      
      
      
       
      image/png,image/gif,image/jpeg 
       
      2000 
      
      
      
     
   


  

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



  
    
    My JSP 'index.jsp' starting page
	
  
  
  
  
  
   
  
文件名:
文件:

文件名:
文件:

mess.properties---国际化的实现
struts.messages.error.file.too.large=\u4E0A\u4F20\u7684\u6587\u4EF6\u592A\u5927\u8D85\u8FC7\u89C4\u5B9A\u9650\u5236
struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u4E0D\u5141\u8BB8
struts.messages.error.uploading=\u4E0A\u4F20\u9519\u8BEF

二: 使用自定义的方式验证

java 代码
package action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xw