Struts2文件上傳功能開發
技術要點
本節代碼詳細說明文件上傳功能的開發流程,介紹知識點如下:
本節代碼詳細說明文件上傳功能的開發流程,介紹知識點如下:
1. 文件上傳頁面和顯示上傳成功頁面代碼內容。
2. UploadAction類中實現上傳功能方法和上傳文件屬性介紹。
3. struts.xml中UploadAction配置,以及字符編碼、文件臨時存放路徑配置。
4. 上傳后所處路徑和最終上傳成功后效果展示。
<!---------------------文件名:upload.jsp----------------->
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>上傳文件</title>
</head>
<body>
<!-- 上傳文件表單定義 -->
<s:form action="upload" method="post" enctype="multipart/form-data">
<tr>
<!-- 上傳文件標簽定義 -->
<td>上傳文件:<s:file name="file"></s:file></td>
</tr>
<tr>
<td>再次上傳文件:<s:file name="file"></s:file></td>
</tr>
<tr>
<td align="left"><s:submit name="submit" value="提交"></s:submit></td>
</tr>
</s:form>
</body>
</html>
<!-------------------上傳文件成功后結果頁面文件名:result.jsp ----------------->
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>上傳結果</title>
</head>
<body>
上傳文件:
<!-- 顯示上傳成功文件名 -->
<s:property value="fileFileName" />
</body>
</html>
<!------------------文件名:UploadAction.java ------------------>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

//文件上傳Action
public class UploadAction extends ActionSupport {
//上傳文件存放路徑
private final static String UPLOADDIR = "/upload";
//上傳文件集合
private List<File> file;
//上傳文件名集合
private List<String> fileFileName;
//上傳文件內容類型集合
private List<String> fileContentType;

public List<File> getFile() {
return file;
}

public void setFile(List<File> file) {
this.file = file;
}

public List<String> getFileFileName() {
return fileFileName;
}

public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}

public List<String> getFileContentType() {
return fileContentType;
}

public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}

public String execute() throws Exception {
for (int i = 0; i < file.size(); i++) {
//循環上傳每個文件
uploadFile(i);
}
return "success";
}

//執行上傳功能
private void uploadFile(int i) throws FileNotFoundException, IOException {
try {
InputStream in = new FileInputStream(file.get(i));
String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);
File uploadFile = new File(dir, this.getFileFileName().get(i));
OutputStream out = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024 * 1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}

in.close();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
2. UploadAction類中實現上傳功能方法和上傳文件屬性介紹。
3. struts.xml中UploadAction配置,以及字符編碼、文件臨時存放路徑配置。
4. 上傳后所處路徑和最終上傳成功后效果展示。



















































































































struts.xml配置文件中有關文件上傳的配置:
<!--------------------文件名:struts.xml------------------->
<struts>
<!-- 系統常量定義,定義上傳文件字符集編碼 -->
<constant name="struts.i18n.encoding" value="gb2312"></constant>
<!-- 系統常量定義,定義上傳文件臨時存放路徑 -->
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<!-- Action所在包定義 -->
<package name="C04.4" extends="struts-default">
<!-- Action名字,類以及導航頁面定義 -->
<!-- 通過Action類處理才導航的的Action定義 -->
<action name="upload" class="action.UploadAction">
<result name="input">/jsp/upload.jsp</result>
<result name="success">/jsp/result.jsp</result>
</action>
</package>
</struts>
<!--------------------文件名:struts.xml------------------->
<struts>
<!-- 系統常量定義,定義上傳文件字符集編碼 -->
<constant name="struts.i18n.encoding" value="gb2312"></constant>
<!-- 系統常量定義,定義上傳文件臨時存放路徑 -->
<constant name="struts.multipart.saveDir" value="c:\"></constant>
<!-- Action所在包定義 -->
<package name="C04.4" extends="struts-default">
<!-- Action名字,類以及導航頁面定義 -->
<!-- 通過Action類處理才導航的的Action定義 -->
<action name="upload" class="action.UploadAction">
<result name="input">/jsp/upload.jsp</result>
<result name="success">/jsp/result.jsp</result>
</action>
</package>
</struts>
posted on 2012-11-29 23:47 youngturk 閱讀(316) 評論(0) 編輯 收藏 所屬分類: JSP隨筆 、Java基礎 、struts2 、servlet