天天看點

Struts2.0學習筆記---上傳檔案和多檔案上傳

Struts2.0學習筆記---上傳檔案和多檔案上傳

粘上自己的代碼:

ShowWords.java

package action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;


public class ShowWords{
	private File uploadImage;
	private String uploadImageContentType;
	private String uploadImageFileName;
	
	public File getUploadImage() {
		return uploadImage;
	}
	public void setUploadImage(File uploadImage) {
		this.uploadImage = uploadImage;
	}
	public String getUploadImageContentType() {
		return uploadImageContentType;
	}
	public void setUploadImageContentType(String uploadImageContentType) {
		this.uploadImageContentType = uploadImageContentType;
	}
	public String getUploadImageFileName() {
		return uploadImageFileName;
	}
	public void setUploadImageFileName(String uploadImageFileName) {
		this.uploadImageFileName = uploadImageFileName;
	}
	public String execute() {
		String realpath = ServletActionContext.getServletContext().getRealPath("/image");
		File f=new File(realpath);
		if(!f.exists())
		{ f.mkdirs(); }
		try {
			FileUtils.copyFile(uploadImage,new File(f,uploadImageFileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
}
           

inf.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'inf.jsp' starting page</title>
  </head>
  
  <body>
   <form  action="showWords.action"   method="post"  enctype="multipart/form-data">
   	    檔案: <input type="file"  name="uploadImage">
   		<input type="submit" name="送出">
   </form>
  </body>
</html>
           

要注意:form表單name屬性要與ShowWords中的File  uploadImage名稱相同。

多檔案上傳,就是要把    uploadImage  uploadImageFileName改為數組,原來form表單上幾個同時送出的資料的名稱要設為一緻的。

代碼:

ShowWords.java

package action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;


public class ShowWords{
	private File[] uploadImage;
	private String[] uploadImageFileName;
	
	public File[] getUploadImage() {
		return uploadImage;
	}
	public void setUploadImage(File[] uploadImage) {
		this.uploadImage = uploadImage;
	}
	public String[] getUploadImageFileName() {
		return uploadImageFileName;
	}


	public void setUploadImageFileName(String[] uploadImageFileName) {
		this.uploadImageFileName = uploadImageFileName;
	}


	public String execute() {
		String realpath = ServletActionContext.getServletContext().getRealPath("/image");
		File f=new File(realpath);
		if(!f.exists())
		{ f.mkdirs(); }
		try {
			for(int i=0;i<uploadImage.length;++i)
			{
				FileUtils.copyFile(uploadImage[i],new File(f,uploadImageFileName[i]));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
}
           

inf.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'inf.jsp' starting page</title>
  </head>
  
  <body>
   <form  action="showWords.action"   method="post"  enctype="multipart/form-data">
   	    檔案: <input type="file"  name="uploadImage"><br/>
   	    檔案: <input type="file"  name="uploadImage"><br/>
   		<input type="submit" name="送出">
   </form>
  </body>
</html>