881
技術社區[雲棲]
基於Struts2的文件上傳(轉)
1 文件上傳的表單
<html>
<head>
<base href="<%=basePath%>">
<title>Struts2文件上傳</title>
</head>
<body>
<center>
<h1>Struts 2完成上傳</h1>
<form action="upload.action" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username" ></td>
</tr>
<tr>
<td>上傳文件:</td>
<td><input type="file" name="myFile"></td>
</tr>
<tr>
<td><input type="submit" value="上傳"></td>
<td><input type="reset"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
2 文件上傳的Action
這裏需要除了Struts本身需要的包以外,還有
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
這兩個包。
Action中字段必須遵守一下命名規則
private File xxx;封裝該文件對應的文件內容。
private String xxxFileName;該文件的文件名稱。
private String xxxContentType;該文件的文件類型。
import
java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
// username屬性用來封裝用戶名
private String username;
// myFile屬性用來封裝上傳的文件
private File myFile;
// myFileContentType屬性用來封裝上傳文件的類型
private String myFileContentType;
//
myFileFileName屬性用來封裝上傳文件的文件名
private String myFileFileName;
//獲得username值
public String getUsername() {
return username;
}
//設置username值
public void setUsername(String username) {
this.username = username;
}
//獲得myFile值
public File getMyFile() {
return myFile;
}
//設置myFile值
public void setMyFile(File myFile) {
this.myFile = myFile;
}
//獲得myFileContentType值
public String getMyFileContentType() {
return myFileContentType;
}
//設置myFileContentType值
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
//獲得myFileFileName值
public String getMyFileFileName() {
return myFileFileName;
}
//設置myFileFileName值
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public
String execute() throws Exception {
//基於myFile創建一個文件輸入流
InputStream is = new FileInputStream(myFile);
// 設置上傳文件目錄(這樣設置的目錄在tomcat中)
String uploadPath = ServletActionContext.getServletContext()
.getRealPath("/upload");
// 設置目標文件
File toFile = new File(uploadPath, this.getMyFileFileName());
// 創建一個輸出流
OutputStream os = new FileOutputStream(toFile);
//設置緩存
byte[] buffer = new byte[1024];
int length = 0;
//讀取myFile文件輸出到toFile文件中
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
System.out.println("上傳用戶"+username);
System.out.println("上傳文件名"+myFileFileName);
System.out.println("上傳文件類型"+myFileContentType);
//關閉輸入流
is.close();
//關閉輸出流
os.close();
return SUCCESS;
}
}
3 Struts.xml
<struts>
<package name="struts2" extends="struts-default">
<action name="upload" >
<interceptor-ref name="fileUpload">
<param name="allowedTypes"> application/msword</param>
<param name="maximumSize">20000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/result.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
關於上傳文件大小的控製,請參見:https://blog.sina.com.cn/s/blog_67aaf4440100ya3w.html
原帖地址:https://ihanfeng.iteye.com/blog/834232
最後更新:2017-04-02 22:16:36