標籤:io os ar java for sp 檔案 on art
今天在寫文檔上傳的時候出現了INFO: Unable to find ‘struts.multipart.saveDir‘ property setting. Defaulting to javax.servlet.contex提示錯誤,後來尋找了寫資料發現是struts.xml中少了配置
修改辦法為在struts2.xml中加入<constant name="struts.multipart.saveDir" value="/tmp"></constant>就ok了。如果實現文檔的多上傳,只要在前台jsp頁面多加幾個<input type="file" name="file">
的表單。後台action類中加入 private static final String PATH="D:\\file\\";//檔案存放路徑
private List<File> file;//上傳檔案數組
private List<String> fileFileName;//檔案名稱,是檔案名稱的一個數組
private String fileContentType;//記錄每個上傳檔案的類型,String類型
此時file為input控制項中的name屬性(這個name值是自己起名字的比如我起的名字為file)通過如下代碼可以將上傳的文檔存放到檔案夾中:
if(upload!=null){
//迴圈遍曆檔案
for(int i=0;i<upload.size();i++){
//取到檔案流
InputStream is=new FileInputStream(upload.get(i));
//建立檔案輸出資料流
OutputStream os=new FileOutputStream(PATH+uploadFileName.get(i));
//緩衝位元組
byte buffer[]=new byte[1024];
//緩衝位元組大小
int count=0;
//寫檔案
while((count=is.read(buffer))>0){
os.write(buffer,0,count);
}
//關閉流
os.close();
is.close();
}
}
struts2文檔上傳報錯