今天寫了一個web網頁上傳文件的功能、這也是一個很常見的功能、我使用的是struts2自帶的一個插件需要導入以下幾個包:
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-io-1.3.1.jar
commons-fileupload-1.2.jar
上傳文件頁面要包含一個
<s:file name="upload" label="上傳包"></s:file>
接着寫我們在action
public class PlatformAction extends BaseAction {
/**
*
*/
private static final long serialVersionUID = -2775753048076575261L;
//上傳文件(這裏要用File對象來封裝)
private File upload;
//下面兩個屬性的get和set方法要以upload的get和set打頭來命名,最簡單的就是upload+xxxx 就可以去自動生成了
private String uploadContentType; // 文件的內容類型
private String uploadFileName; // 上傳文件名
/**
*
* 方法名:saveServicePack 描述: 作者:白鵬飛 日期:2012-7-13 下午02:42:31
*
* @param @return
* @param @throws BzException
* @return String
*/
public String saveServicePack() throws BzException {
//檢測記錄是否存在
getEasyServicePackService().checkPackInfo(getPackName(), getVersion());
//檢測服務器是否有同名軟件包
File fDir = new File(Constant.SERVICE_PACK_ADDRESS);
// File fDir = new File("c:/");
//檢測路徑
if(!fDir.exists()){
fDir.mkdirs();
}
String[] fNames = fDir.list();
for(int i=0;i<fNames.length;i++){
if(fNames[i].equals(this.getUploadFileName())){
throw new BzException("服務包重名");
}
}
//上傳軟件包
try {
//基於myFile創建一個文件輸入流
InputStream is = new FileInputStream(getUpload());
// 創建一個輸出流
// OutputStream os = new FileOutputStream(new File("c:/", this.getUploadFileName()));
OutputStream os = new FileOutputStream(new File(Constant.SERVICE_PACK_ADDRESS, this.getUploadFileName()));
//設置緩存
byte[] buffer = new byte[1024];
int length = 0;
//讀取File文件輸出到toFile文件中
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
//關閉輸入流
is.close();
//刷新
os.flush();
//關閉輸出流
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new BzException("文件上傳失敗");
}
// 獲取Session對象將UserVO放入Session
ActionContext actionContext = ActionContext.getContext();
session = actionContext.getSession();
EasyUser user = (EasyUser) session.get(Constant.SESSION_OPERTAOR);
getEasyServicePackService().saveServicePack(getPackName(),
getVersion(), getEnabled(), user.getUserid(), getInstallDir(),
getPlatId(), getIspublic(), getServerName());
setActionMsg("上傳成功");
return SUCCESS;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
接着配置我們的web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
這樣就可以了,但是我們寫完進行測試時會發現稍微大一點的文件struts2就會報input錯誤,這個原因是因爲struts2默認的文件大小是2M超過這個數值程序就會認爲處理不了Action也不會去執行我們
我們可以在struts.properties或者struts.xml裏面配置
<constant name="struts.multipart.maxSize" value="104857600" />
在struts.xml裏面加入這一句話就可以上傳100M的文件
當然上傳文件的功能還不只如此還可以設定限制上傳的文件和文件類型不匹配和文件過大異常的處理
google一搜一大堆、這裏就不再贅述了。