0. Preface
Online most pictures batch upload code old incomplete, then into a sub-text, in order to save Bo friends time
I. Code examples
Front-end code (use HTML5 Multiple property to support multi-image bulk uploads):
<form action= "" Enctype= ' Multipart/form-data ' method= "post" > <div class= "Form-item" > <label > Upload Images </label> <input type= "file" value= "select Picture" Name= "image[]" multiple> </div></ Form>
Backend processing code://access to the foreground of the picture resources (here with the GetFiles () method is roughly expressed, because this is related to the Controller framework, such as SPRINGMVC, struts and so on, may even be the Servlet direct access, specific project use, This is not an example.)
fileitem[] files = GetFiles ("image[]"); list<string> pictureurllist = new arraylist<string> (); for (int index = 0; index < files.length; index++) {try {byte[] bytes = Ioutils.tob Ytearray (Files[index].getinputstream ()); String filename = Files[index].getfilename (); Picture upload path, may be stand-alone picture server, may also be put together with the application String Uploadpath = "http://img.cdn.com/upload/image/"; String extension = filenameutils.getextension (filename). toLowerCase (); Customize a function that generates a non-repeating file name GetFileName () String filename = getfilename () + extension; Upload file Pathtosave = new file (uploadpath, filename); if (!pathtosave.getparentfile (). exists ()) {Pathtosave.getparentfile (). Mkdirs (); } fileoutputstream fos = new FileOutputStream (pathtosave); Ioutils.write (bytes, FOS); } catch (Exception e) {}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Multi-image Batch Upload