?? struts多文件上傳.txt
字號:
Struts中實現多文件上傳
現在在網絡上有很多關于struts文件上傳的文章,但是很多都是直接摘抄struts自帶的例子,而且很多都如同一轍,關于多文件上傳的更是少之又少。我這幾天剛好要做一個多文件上傳的DEMO,就自己寫了一個多文件上傳的例子,不好的地方請大家多多指教。
actionForm采用map來對應上傳頁面,action采用STRUTS自帶的FormFile來聯系上傳的文件。 我就直接按actionForm,JSP->struts_config->action的順序來說,程序就列出關鍵部分,由于時間關系我就不做詳細解釋了。
actionForm的程序:
public class MutiUploadForm extends ActionForm
...{
protected Map fileMap;
public MutiUploadForm()
...{
fileMap = new HashMap();
}
public Map getFileMap()
...{
return fileMap;
}
public void setFileMap(Map fileMap)
...{
this.fileMap = fileMap;
}
public void setValue(String attributeKey, Object attributeValue)
...{
getFileMap().put(attributeKey, attributeValue);
}
public Object getValue(String attributeKey)
...{
Object keyValue = getFileMap().get(attributeKey);
return keyValue;
}
}
JSP程序: 我就簡單的列出了兩個文件上傳你可以用腳本程序實現文件輸入框的自動增加,有時間我再給出這方面的代碼。
<html:form action="mutiUpload_act.do" enctype="multipart/form-data">
<html:file property="value(file01)" />
<html:file property="value(file02)" />
<html:submit />
</html:form>
注意:(file02)中的value對應actionform中的setValue(...),是javabean的默認用法。
struts-config.xml:請只參考其中的關鍵部分:
<action path="/mutiUpload_act"
type="com.hong.upload.web.action.MutiUploadAction"
name="mutiUploadForm"
scope="request"
input="input">
<forward name="input" path="/mutiUpload.jsp" />
<forward name="display" path="/display.jsp" />
</action>
Action的程序:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
...{
System.out.println("action.....");
if (form instanceof MutiUploadForm)
...{
// 如果form是MutiUploadForm
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
...{
response.setContentType("text/html;charset=gb2312");
}
MutiUploadForm theForm = (MutiUploadForm) form;
/** *//****************第一個文件***********************/
FormFile file1 = (FormFile) theForm.getValue("file01");// 取得上傳的文件
String size1 = (file1.getFileSize() + " bytes");// 文件大小
String fileName1 = file1.getFileName();// 文件名
try
...{
InputStream stream = file1.getInputStream();// 把文件讀入
//String filePath = request.getPathInfo();// 取當前系統路徑
String filePath = "e:\web\frameDemo";
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "\"
+ file1.getFileName());
// 建立一個上傳文件的輸出流,將上傳文件存入web應用的根目錄。
System.out.println(filePath + "\" + file1.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
...{
bos.write(buffer, 0, bytesRead);// 將文件寫入服務器
}
bos.close();
stream.close();
}
catch (Exception e)
...{
System.err.print(e);
}
request.setAttribute("size1", size1);
request.setAttribute("fileName1", fileName1);
/** *//*******************第二個文件********************/
FormFile file2 = (FormFile) theForm.getValue("file02");// 取得上傳的文件
String size2 = (file2.getFileSize() + " bytes");// 文件大小
String fileName2 = file2.getFileName();// 文件名
try
...{
InputStream stream = file2.getInputStream();// 把文件讀入
//String filePath = request.getPathInfo();// 取當前系統路徑
String filePath = "e:\web\frameDemo";
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "\"
+ file2.getFileName());
// 建立一個上傳文件的輸出流,將上傳文件存入web應用的根目錄。
System.out.println(filePath + "\" + file2.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
...{
bos.write(buffer, 0, bytesRead);// 將文件寫入服務器
}
bos.close();
stream.close();
}
catch (Exception e)
...{
System.err.print(e);
}
request.setAttribute("size1", size1);
request.setAttribute("fileName1", fileName1);
request.setAttribute("size2", size2);
request.setAttribute("fileName2", fileName2);
return mapping.findForward("display");
} else
...{
System.out.println("form is not instanceof MutiUploadForm!");
}
return null;
}
為了簡單,我沒有用循環,在文件多的時候可以根據map的keySet來循環。 上面是程序的關鍵部分,因為時間關系寫的比較粗略,希望對大家有所幫助。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -