文件上傳大體我給分這麼幾步。1:在WEB-INF/lib下面添加commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,這兩個文件全部在Struts2提供的lib包下面;2:在文件上傳的表單處做設置,將form表單的enctype設置成爲:“multipart/form-data”,提交方式設置爲post;3:做Action。
具體代碼如下:
fileupload.jsp
struts.xml
FileUploadAction.java
message.jsp
幾點注意事項:在Action中的屬性,比如image,都必須跟表單當中的屬性值一樣,都是image,還有屬性的名字都必須按照固定的格式寫,如image,imageFileName,如果不這麼寫,會報錯的。
具體代碼如下:
fileupload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${pageContext.request.contextPath}<br />
<form action="${pageContext.request.contextPath }/fileupload" enctype="multipart/form-data" method="post">
選擇資源1:<input type="file" name="image"><BR />
選擇資源2:<input type="file" name="image"><BR />
<input type="submit" value="上傳"/>
</form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="yuan" extends="struts-default">
<action name="fileupload" class="com.ambow.Action.FileUploadAction">
<result>
/WEB-INF/page/message.jsp
</result>
</action>
</package>
</struts>
FileUploadAction.java
package com.ambow.Action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
//這裏是固定的寫法,文件名子必須跟上一個表單的name屬性名字相同,都是image
private File[] image ;
private String[] imageFileName ;
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
//得到當前工程的目錄,然後自己再定義下保存的路徑
String realpath = ServletActionContext.getServletContext().getRealPath("/file") ;
//打印路徑看是否存在
System.out.println(realpath);
if(image != null){
File savedir = new File(realpath) ;
if(!savedir.exists()){
savedir.mkdirs() ;
}
for(int i=0;i<image.length;i++){
File savefile = new File(savedir,imageFileName[i]) ;
FileUtils.copyFile(image[i], savefile) ;
}
}
ServletActionContext.getServletContext().setAttribute("message", "文件上傳成功!") ;
return SUCCESS;
}
}
message.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${message }
</body>
</html>
幾點注意事項:在Action中的屬性,比如image,都必須跟表單當中的屬性值一樣,都是image,還有屬性的名字都必須按照固定的格式寫,如image,imageFileName,如果不這麼寫,會報錯的。