struts2 单个文件上传的三种方法以及多文件上传

 

<!--[if !supportLists]-->1、  <!--[endif]-->填加JAR包:commons-fileupload-1.2.1.jar connons-io-1.3.2.jar放在WEB-INF/lib

2、在from表单增加enctype属性<form action=”” method=”” enctype=”multipart/form-data

3、Struts.xml配置文件中引入上传文件的配置

       <include file=”cn/csdn/struts2/xml/file/struts-upload.xml”></include>

4、Struts-upload.xml配置文件中填加上传的action处理.并且初始化上传文件路径参数

 

   <!-- 上传文件action的处理 -->

Xml代码
  1. <action name="uploadfile" class="cn.csdn.action.GoodsAction2" method="uploadfile">  
  2.  <!-- 初始化文件保存路径参数为/uploads -->  
  3.  <param name="savePath">/uploads</param>  
  4.  <result name="success">/ac.jsp</result>  
  5. </action>  

 以上是准备工作 ,做好之后就是上传功能的实现

 

第一种方法:用字节流实现

核心代码:

 

Java代码
  1. private String savePath;   
  2.     private String title;   
  3.     private File pic;// 文件名   与视图层的名称一致   
  4.     private String picContentType;//文件名+ContentType   
  5.     private String picFileName;//文件名+FileName   
  6.        
  7.        
  8.     public String getSavePath() {   
  9.         return ServletActionContext.getServletContext().getRealPath(savePath);   
  10.     }   
  11.     public void setSavePath(String savePath) {   
  12.         this.savePath = savePath;   
  13.     }   
  14.     public String getTitle() {   
  15.         return title;   
  16.     }   
  17.     public void setTitle(String title) {   
  18.         this.title = title;   
  19.     }   
  20.     public File getPic() {   
  21.         return pic;   
  22.     }   
  23.     public void setPic(File pic) {   
  24.         this.pic = pic;   
  25.     }   
  26.     public String getPicContentType() {   
  27.         return picContentType;   
  28.     }   
  29.     public void setPicContentType(String picContentType) {   
  30.         this.picContentType = picContentType;   
  31.     }   
  32.     public String getPicFileName() {   
  33.         return picFileName;   
  34.     }   
  35.     public void setPicFileName(String picFileName) {   
  36.         this.picFileName = picFileName;   
  37.     }   
  38.     //文件上传功能实现:方法一:字节流   
  39.     public String uploadfile(){   
  40.         System.out.println("getSavePath()"+getSavePath());   
  41.         System.out.println("savePath:"+savePath);   
  42.         System.out.println("title:"+title);   
  43.         System.out.println("pic:"+pic);   
  44.         System.out.println("picContentType:"+picContentType);   
  45.         System.out.println("picFileName:"+picFileName);   
  46.            
  47.         FileInputStream fis=null;   
  48.         FileOutputStream fos=null;   
  49.         //定义保存的路径   
  50.         String savepath=getSavePath();   
  51.            
  52.         //根据路径创建文件路径对象   
  53.         File file=new File(savepath);   
  54.         if(!file.exists()){   
  55.             file.mkdirs();   
  56.         }   
  57.        
  58.         try {   
  59.             //创建输入流   
  60.             fis=new FileInputStream(pic);   
  61.             //创建输出流   
  62.             fos=new FileOutputStream(savepath+"//"+picFileName);   
  63.                
  64.             byte buf[]=new byte[1024];   
  65.             int n=0;   
  66.             while((n=fis.read(buf))!=-1){   
  67.                 fos.write(buf, 0, n);   
  68.             }   
  69.             if(fis!=null){   
  70.                 fis.close();   
  71.             }   
  72.             if(fos!=null){   
  73.                 fos.close();   
  74.             }   
  75.         } catch (FileNotFoundException e) {   
  76.             // TODO Auto-generated catch block   
  77.             e.printStackTrace();   
  78.         } catch (IOException e) {   
  79.             // TODO Auto-generated catch block   
  80.             e.printStackTrace();   
  81.         }   
  82.         return SUCCESS;    
  83.     }  
private String savePath;
	private String title;
	private File pic;// 文件名   与视图层的名称一致
	private String picContentType;//文件名+ContentType
	private String picFileName;//文件名+FileName
	
	
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public File getPic() {
		return pic;
	}
	public void setPic(File pic) {
		this.pic = pic;
	}
	public String getPicContentType() {
		return picContentType;
	}
	public void setPicContentType(String picContentType) {
		this.picContentType = picContentType;
	}
	public String getPicFileName() {
		return picFileName;
	}
	public void setPicFileName(String picFileName) {
		this.picFileName = picFileName;
	}
	//文件上传功能实现:方法一:字节流
	public String uploadfile(){
		System.out.println("getSavePath()"+getSavePath());
		System.out.println("savePath:"+savePath);
		System.out.println("title:"+title);
		System.out.println("pic:"+pic);
		System.out.println("picContentType:"+picContentType);
		System.out.println("picFileName:"+picFileName);
		
		FileInputStream fis=null;
		FileOutputStream fos=null;
		//定义保存的路径
		String savepath=getSavePath();
		
		//根据路径创建文件路径对象
		File file=new File(savepath);
		if(!file.exists()){
			file.mkdirs();
		}
	
		try {
			//创建输入流
			fis=new FileInputStream(pic);
			//创建输出流
			fos=new FileOutputStream(savepath+"//"+picFileName);
			
			byte buf[]=new byte[1024];
			int n=0;
			while((n=fis.read(buf))!=-1){
				fos.write(buf, 0, n);
			}
			if(fis!=null){
				fis.close();
			}
			if(fos!=null){
				fos.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;	
	}

 第二种方法:用FileUtils

 核心代码:

 

Java代码
  1. // 文件上传功能实现:方法二:FileUtils   
  2.     public String uploadfile() {   
  3.         // 定义保存的路径   
  4.         String savepath = getSavePath();   
  5.   
  6.         // 根据路径创建文件路径对象   
  7.         File file = new File(savepath);   
  8.         if (!file.exists()) {   
  9.             file.mkdirs();   
  10.         }   
  11.   
  12.         try {   
  13.             FileUtils.copyFile(pic, new File(file, getPicFileName()));   
  14.         } catch (Exception ex) {   
  15.             ex.printStackTrace();   
  16.         }   
  17.         return SUCCESS;   
  18.     }   
  19.       
// 文件上传功能实现:方法二:FileUtils
	public String uploadfile() {
		// 定义保存的路径
		String savepath = getSavePath();

		// 根据路径创建文件路径对象
		File file = new File(savepath);
		if (!file.exists()) {
			file.mkdirs();
		}

		try {
			FileUtils.copyFile(pic, new File(file, getPicFileName()));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return SUCCESS;
	}
	

 第三种方法:三层管道 

 核心代码:

 

Java代码
  1. // 文件上传功能实现:方法三:三层管道   
  2.     public String uploadfile() {   
  3.   
  4.         BufferedReader br = null;   
  5.         BufferedWriter bw = null;   
  6.   
  7.         // 定义保存的路径   
  8.         String savepath = getSavePath();   
  9.   
  10.         // 根据路径创建文件路径对象   
  11.         File file = new File(savepath);   
  12.         if (!file.exists()) {   
  13.             file.mkdirs();   
  14.         }   
  15.   
  16.         try {   
  17.             // 创建输入流   
  18.             br = new BufferedReader(new InputStreamReader(new FileInputStream(   
  19.                     pic)));   
  20.             // 创建输出流   
  21.             bw = new BufferedWriter(new OutputStreamWriter(   
  22.                     new FileOutputStream(file + "//" + getPicFileName())));   
  23.   
  24.             char buf[] = new char[1024];   
  25.             int n = 0;   
  26.             while ((n = br.read(buf)) != -1) {   
  27.                 bw.write(buf, 0, n);   
  28.             }   
  29.             if (br != null) {   
  30.                 br.close();   
  31.             }   
  32.             if (bw != null) {   
  33.                 bw.close();   
  34.             }   
  35.         } catch (FileNotFoundException e) {   
  36.             // TODO Auto-generated catch block   
  37.             e.printStackTrace();   
  38.         } catch (IOException e) {   
  39.             // TODO Auto-generated catch block   
  40.             e.printStackTrace();   
  41.         }   
  42.         return SUCCESS;   
  43.     }  
// 文件上传功能实现:方法三:三层管道
	public String uploadfile() {

		BufferedReader br = null;
		BufferedWriter bw = null;

		// 定义保存的路径
		String savepath = getSavePath();

		// 根据路径创建文件路径对象
		File file = new File(savepath);
		if (!file.exists()) {
			file.mkdirs();
		}

		try {
			// 创建输入流
			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					pic)));
			// 创建输出流
			bw = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file + "//" + getPicFileName())));

			char buf[] = new char[1024];
			int n = 0;
			while ((n = br.read(buf)) != -1) {
				bw.write(buf, 0, n);
			}
			if (br != null) {
				br.close();
			}
			if (bw != null) {
				bw.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

 三种比较 ,笔者认为还是第二种比较简单。

 

会了单文件上传,多文件上传就简单了。只需要把获得文件、文件类型、文件名称 定义声明时定义为数组的就可以了

 

核心代码:

 

Java代码
  1. private String savePath;   
  2.     private String title;   
  3.     private File[] pic;// 文件名 与视图层的名称一致   
  4.     private String[] picContentType;// 文件名+ContentType   
  5.     private String[] picFileName;// 文件名+FileName   
  6.   
  7.        
  8.   
  9.     public String getSavePath() {   
  10.         return ServletActionContext.getServletContext().getRealPath(savePath);   
  11.     }   
  12.   
  13.   
  14.   
  15.     public void setSavePath(String savePath) {   
  16.         this.savePath = savePath;   
  17.     }   
  18.   
  19.   
  20.   
  21.     public String getTitle() {   
  22.         return title;   
  23.     }   
  24.   
  25.   
  26.   
  27.     public void setTitle(String title) {   
  28.         this.title = title;   
  29.     }   
  30.   
  31.   
  32.   
  33.     public File[] getPic() {   
  34.         return pic;   
  35.     }   
  36.   
  37.   
  38.   
  39.     public void setPic(File[] pic) {   
  40.         this.pic = pic;   
  41.     }   
  42.   
  43.   
  44.   
  45.     public String[] getPicContentType() {   
  46.         return picContentType;   
  47.     }   
  48.   
  49.   
  50.   
  51.     public void setPicContentType(String[] picContentType) {   
  52.         this.picContentType = picContentType;   
  53.     }   
  54.   
  55.   
  56.   
  57.     public String[] getPicFileName() {   
  58.         return picFileName;   
  59.     }   
  60.   
  61.   
  62.   
  63.     public void setPicFileName(String[] picFileName) {   
  64.         this.picFileName = picFileName;   
  65.     }   
  66.   
  67.   
  68.   
  69.     // 文件上传功能实现:多文件上传   
  70.     public String uploadfiles() {   
  71.         // 定义保存的路径   
  72.         String savepath = getSavePath();   
  73.   
  74.         // 根据路径创建文件路径对象   
  75.         File file = new File(savepath);   
  76.         if (!file.exists()) {   
  77.             file.mkdirs();   
  78.         }   
  79.   
  80.         try {   
  81.             for(int i=0;i<pic.length;i++){   
  82.                 FileUtils.copyFile(pic[i], new File(file, getPicFileName()[i]));   
  83.             }   
  84.                
  85.         } catch (Exception ex) {   
  86.             ex.printStackTrace();   
  87.         }   
  88.         return SUCCESS;   
  89.     }   
  90.        
  91.        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值