i need upload "1000 files" or "a zip file including files" @ once using struts2. (by 1000 files or zip file, mean need files uploaded on system not matter if user choose 1000 files @ once or zip them , upload single file, looking 1 easier implement , more efficient)
i have read following answers none of them suits purpose.
using following code, when use simple list files; shows name of lists, when use list files not show thing , can not upload files.
upload.jsp
<%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>jsp page</title> </head> <body> <form action="upload" enctype="multipart/form-data" method="post"> <input name="files" type="file" multiple/> <button type="submit"/> </form> </body> </html>
upload.java
@action public class upload implements addresses { private list <file> files = new arraylist <file> (); public string execute(){ return "success"; } public upload() { system.out.println("in upload 1"); for(int i=0;i<files.size();i++) system.out.println(i + ")" + files.get(i)); system.out.println("in upload 2"); } public list <file> getfiles() { return files; } public void setfiles(list <file> files) { this.files = files; for(int i=0;i<files.size();i++) system.out.println(i + ")" + files.get(i)); // file filetocreate = new file("c:\image", files.get(i).tostring()); // fileutils.copyfile(files.get(i), filetocreate); } }
output
in upload 1 in upload 2
i suggest use struts tags instead of plain html tags, , extend actionsupport (returning result constants instead of manually composing result string, "result").
that said, tested , working example.
note: won't work on old versions of ie, since using html5 in own question, bet know , not targeting old ie.
jsp
<%@page contenttype="text/html; charset=utf-8" %> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>multiple file upload example</title> </head> <body> <s:form action="upload" enctype="multipart/form-data" > <s:file name="files" multiple="multiple" /> <s:submit value="upload files" /> </s:form> </body> </html>
note multiple="multiple"
part: if in official documentation, attribute <s:file />
tag not defined, since struts 2.1 allowed because of
dynamic attributes allowed: true
this means drawn on jsp as-is, without interference struts. way struts doens't need update tags each time html5 provides new feature; put foo="bar"
in tag allows dynamic attributes (<s:file />
, <s:textarea />
, etc), , find in html.
action
public class upload extends actionsupport{ private list<file> files; private list<string> filescontenttype; private list<string> filesfilename; /* getters , setters */ public string execute() throws exception{ system.out.print("\n\n---------------------------------------"); int i=0; (file file : files){ system.out.print("\nfile ["+i+"] "); system.out.print("; name:" + filesfilename.get(i)); system.out.print("; contenttype: " + filescontenttype.get(i)); system.out.print("; length: " + file.length()); i++; } system.out.println("\n---------------------------------------\n"); return success; } }
then may want set maximum size of request, , maximum size of each single file, like described here:
struts.xml - max multipart size:
<constant name="struts.multipart.maxsize" value="20000000" />
struts.xml - max size of file (globally package, or locally action)
<interceptor-ref name="fileupload"> <param name="maximumsize">10485760</param> </interceptor-ref>
Comments
Post a Comment