Can’t upload files with Servlet and HTML5

I use following code to upload file to server. In jsp file, if I use :

 <input type="file" name="file" value="" />

It’s work ok. But if I change to :

 <input type="file" id="fileinput" name="file"  multiple="multiple" />

to upload many file.It just upload the last file. How can I upload many file with this input tag.

Here is server code :

boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
                if (!isMultiPart) {
                    System.out.println("Error , Not upload file");
                } else {
                    FileItemFactory factory = new DiskFileItemFactory();
                    ServletFileUpload upload = new ServletFileUpload(factory);
                    List items = null;
                    try {
                        items = upload.parseRequest(request);
                    } catch (FileUploadException e) {
                        System.out.println("Error : " + e.getMessage());
                    Iterator iter = items.iterator();
                    Map params = new HashMap();
                    StringBuffer fileName;
                    //StringBuffer dir;
                    while (iter.hasNext()) {
                        FileItem item = (FileItem);
                        if (item.isFormField()) {
                            params.put(item.getFieldName(), item.getString("UTF-8"));
                        } else {
                            System.out.println("Not param");
                            fileName = new StringBuffer();
                            File dir = new File(fileName.toString());

                            String extension = item.getName().substring(item.getName().lastIndexOf("."));
                            fileName.append("").append(UUID.randomUUID().toString().replaceAll("-", "")).append(extension);
                            File savedFile = new File(fileName.toString());

Source: jsp

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.