多个文件上传 [英] Multiple file Upload

查看:71
本文介绍了多个文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在任何位置上传多个文件。我的问题是我选择了多个文件但是当我点击上传按钮时,只有最后一个文件上传了重命名名称,重命名名称是所有文件名附加逗号,如此(file1,file2, flie3)

How to upload multiple file in any location. My Problem is that i am selecting multiple files but when i click on the upload button only last one file is uploaded with rename name and the rename name is all file name append with comma like this (file1,file2,flie3)

这是代码

File saveFile = null;
String tempPath = System.getProperty("java.io.tmpdir");
saveFile = new File(tempPath + File.separator + fileUploadFileName);
FileUtils.copyFile(fileUpload, saveFile);


推荐答案

通过使用Apache commons fileupload FileItem,示例代码将是这样的

By using Apache commons fileupload FileItem, the sample code will be like this

try {
            // parses the request's content to extract file data
            List formItems = upload.parseRequest(request);
            Iterator iter = formItems.iterator();

            // iterates over form's fields
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                // processes only fields that are not form fields
                if (!item.isFormField()) {
                    String fileName = new File(item.getName()).getName();
                    String filePath = uploadPath + File.separator + fileName;
                    File storeFile = new File(filePath);

                    // saves the file on disk
                    item.write(storeFile);
                }
            }
            request.setAttribute("message", "Upload has been done successfully!");
        } catch (Exception ex) {
            request.setAttribute("message", "There was an error: " + ex.getMessage());
            ex.printStackTrace();
        }

多文件上传。请参阅此上传了解更多详情:

Download MultipleFilesUpload.zip from Multi File Upload. Refer to this Upload for more details :

这篇关于多个文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆