struts2的文件上傳
一直以來自己都是看書的時(shí)候多,而實(shí)際做卻很少,最近慢慢開始做東西了,發(fā)現(xiàn),看與做是2個(gè)完全不同的事情。
記錄下面的東西是為了以后使用方便。
利用struts2上傳文件。
上傳頁面
<form action="upload" method="post" enctype="multipart/form-data">
文件標(biāo)題:<input type="text" name="title" /><br>
選擇文件:<input type="file" name="upload" /><br>
<input value="上傳" type="submit" />
</form>
然后就是struts.xml文件,在 <package>標(biāo)簽中文件標(biāo)題:<input type="text" name="title" /><br>
選擇文件:<input type="file" name="upload" /><br>
<input value="上傳" type="submit" />
</form>
<action name="upload" class="com.duduli.li.Upload">
<param name="savePath">/file</param>
<result name="showUpload">showUpload.jsp</result>
</action>
然后就是處理上傳的文件。<param name="savePath">/file</param>
<result name="showUpload">showUpload.jsp</result>
</action>
package com.duduli.li;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class Upload extends ActionSupport {
private String title;
private File upload;
private String uploadContextType;
private String uploadFileName;
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContextType() {
return uploadContextType;
}
public void setUploadContextType(String uploadContextType) {
this.uploadContextType = uploadContextType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
//通過struts2的配置文件得到上傳目錄,這個(gè)是很重要的
@SuppressWarnings("deprecation")
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String value) {
this.savePath = value;
}
@Override
public String execute() throws Exception {
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
return "showUpload";
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class Upload extends ActionSupport {
private String title;
private File upload;
private String uploadContextType;
private String uploadFileName;
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContextType() {
return uploadContextType;
}
public void setUploadContextType(String uploadContextType) {
this.uploadContextType = uploadContextType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
//通過struts2的配置文件得到上傳目錄,這個(gè)是很重要的
@SuppressWarnings("deprecation")
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String value) {
this.savePath = value;
}
@Override
public String execute() throws Exception {
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
return "showUpload";
}
}
如果你認(rèn)為配置這些就可以了,那你就錯(cuò)了,還需要配置一個(gè)struts.properties文件。定義一個(gè)臨時(shí)的上傳文件夾。


還有一個(gè)就是簡單的顯示上傳成功的頁面。














posted on 2009-05-06 22:19 duduli 閱讀(2081) 評(píng)論(1) 編輯 收藏 所屬分類: SSH/SSH2