Struts
具體實現
前段時間Apache發布了Struts
清單
首先
<% @ page language =
<% @ taglib prefix =
<! DOCTYPE html PUBLIC
< html xmlns =
< head >
< title > Struts
</ head >
< body >
< s:form action =
< s:file name =
< s:textfield name =
< s:submit />
</ s:form >
</ body >
</ html >
在FileUpload
其次是FileUploadAction
package tutorial;
import java
import java
import java
import java
import java
import java
import java
import java
import org
import com
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID =
private static final int BUFFER_SIZE =
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
public void setMyFileContentType(String contentType) {
ntentType = contentType;
}
public void setMyFileFileName(String fileName) {
this
}
public void setMyFile(File myFile) {
this
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this
}
private static void copy(File src
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src)
out = new BufferedOutputStream( new FileOutputStream(dst)
byte [] buffer = new byte [BUFFER_SIZE];
while (in
out
}
} finally {
if ( null != in) {
in
}
if ( null != out) {
out
}
}
} catch (Exception e) {
e
}
}
private static String getExtention(String fileName) {
int pos = fileName
return fileName
}
@Override
public String execute() {
imageFileName = new Date()
File imageFile = new File(ServletActionContext
copy(myFile
return SUCCESS;
}
} 清單
在FileUploadAction中我分別寫了setMyFileContentType
FileUploadAction作用是將浏覽器上傳的文件拷貝到WEB應用程序的UploadImages文件夾下
下面我們就來看看上傳成功的頁面
<% @ page language =
<% @ taglib prefix =
<! DOCTYPE html PUBLIC
< html xmlns =
< head >
< title > Struts
</ head >
< body >
< div style =
< img src =
< br />
< s:property value =
</ div >
</ body >
</ html > 清單
ShowUpload
然後是Action的配置文件
<? xml version=
<! DOCTYPE struts PUBLIC
< struts >
< package name =
< action name =
< interceptor
< result name =
</ action >
</ package >
</ struts > 清單
fileUpload Action顯式地應用fileUploadStack的攔截器
最後是web
<? xml version=
< web
xmlns =
xmlns:xsi =
xsi:schemaLocation =
< display
< filter >
< filter
< filter
org
</ filter
</ filter >
< filter >
< filter
< filter
org
</ filter
</ filter >
< filter
< filter
< url
</ filter
< filter
< filter
< url
</ filter
< welcome
< welcome
</ welcome
</ web
發布運行應用程序
清單
選擇圖片文件
清單
更多配置
在運行上述例子
Mar
INFO: Unable to find
Mar
INFO: Removing file myFile C:\Program Files\Tomcat
上述信息告訴我們
struts
這樣上傳的文件就會臨時保存到你根目錄下的tmp文件夾中(一般為c:\tmp)
錯誤處理
上述例子實現的圖片上傳的功能
首先修改FileUpload
然後修改struts
< action name =
< interceptor
< param name =
image/bmp
</ param >
</ interceptor
< interceptor
< result name =
< result name =
</ action > 清單
顯而易見
發布運行應用程序
清單
上面的出錯提示是Struts
實現之後的出錯頁面如下圖所示
清單
同樣的做法
字符資源
多文件上傳
與單文件上傳相似
< s:form action =
< s:file label =
< s:file label =
< s:file label =
< s:submit />
</ s:form > 清單
如果你希望綁定到數組
private File[] uploads;
private String[] uploadFileNames;
private String[] uploadContentTypes;
public File[] getUpload() { return this
public void setUpload(File[] upload) { this
public String[] getUploadFileName() { return this
public void setUploadFileName(String[] uploadFileName) { this
public String[] getUploadContentType() { return this
public void setUploadContentType(String[] uploadContentType) { this
如果你想綁定到列表
private List < File > uploads = new ArrayList < File > ();
private List < String > uploadFileNames = new ArrayList < String > ();
private List < String > uploadContentTypes = new ArrayList < String > ();
public List < File > getUpload() {
return this
}
public void setUpload(List < File > uploads) {
this
}
public List < String > getUploadFileName() {
return this
}
public void setUploadFileName(List < String > uploadFileNames) {
this
}
public List < String > getUploadContentType() {
return this
}
public void setUploadContentType(List < String > contentTypes) {
this
} 清單
總結
在Struts
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28217.html