struts 上传任意多个文件转自:http://1zebra.iteye.com/blog/427260?1.jsp页面为 html:file propertyfi
struts 上传任意多个文件
转自:http://1zebra.iteye.com/blog/427260
?
1.jsp页面为
<html:file property="files(0)" />
??????? <html:errors property="files" />
??????? <div id="uploadFile"></div>
??????? <a href=""> <input type="button" value="上传更多"
????????? onclick="addItem()" />
??????? <script>
???????? i=1;
???????? function addItem(){
????????? document.getElementById('uploadFile').innerHTML+='<input type="file" name="files('+i+')"><br/> ';
????????? i++;
???????? }
??????? </script>
2 form表单
//目的是不让struts报错
private List<FormFile> files = new ArrayList<FormFile>();
public List<FormFile> getFiles() {
?? return this.files;
}
3.action为
//获取formfile
?? ContentPublishForm contentPublishForm = (ContentPublishForm) form;
?? MultipartRequestHandler multipartRequestHandler = form
???? .getMultipartRequestHandler();
?? // 取得所有上传文件的对象集合
?? Hashtable elements = multipartRequestHandler.getFileElements();
?? // 循环遍历每一个文件
?? Collection values = elements.values();
?? int k = 0;
?? for (java.util.Iterator i = values.iterator(); i.hasNext();) {
??? FormFile file = (org.apache.struts.upload.FormFile) i.next();// 取得上传的文件
??? if ("" != file.toString()) {
???? FileOutputStream fileOutput;
???? try {
????? String fileUrl = request
??????? .getRealPath("//Image//"
????????? + file.getFileName());
????? fileOutput = new FileOutputStream(fileUrl);
????? fileOutput.write(file.getFileData());
????? fileOutput.flush();
????? fileOutput.close();
???? } catch (FileNotFoundException e) {
????? e.printStackTrace();
???? } catch (IOException e) {
????? e.printStackTrace();
???? }
??? }
?? }