Vaadin EasyUpload 加载项有时无法打开或找不到指定的文件

Vaadin EasyUpload add-on sometimes could not open or find file specified

我们目前正在使用 EasyUpload 插件,我们已经为该组件指定了条件:

a) 只允许使用 CSV 文件,每个文件的大小上限为 1MB。 b) 一次只能提交一个文件。

我们刚刚对小于 100Kb 的小型 CSV 文件进行了上传测试。通常,上传过程会成功完成。虽然文件已经在临时文件夹中,但偶尔会显示 "Could not open file, The system cannot find the file specified" 的错误,我们发现在以下情况下会发生这种情况:

a) 如果同一文件在文件上传成功后的几秒内稍作修改后再次上传。

b) 如果 web 应用程序有两个选项卡,则在不同用户下登录正在上传各自的 csv 文件,并且他们也会做同样的事情,在再次上传之前更改 csv 中的值。

我们尝试强制通过文件上传(作为另一种测试方法)并在一段时间后注意到文件有时会卡在队列中,尽管我们在提交时间规则中强加了一个文件。它显示在消息 "There are too many files over the count limit" 中。我们还考虑在文件提交后放置 3-5 秒的睡眠/等待命令。

MultiFileUpload multiFileUpload = new MultiFileUpload() {
            @Override
            protected void handleFile(File tmpFile, String fileName, String mimeType, long length) {
                String[] header = {"EOD_NUM","OUTLET_NAME","POSM_NAME","EOD_DATE","TOTAL_SALES","GROSS_SALES",
                "TRAN_COUNT","VOID_COUNT","SERVICE_CHARGE","DISCOUNT_AMT","VAT_TAX_AMT","SVC_TAX_AMT","ROUNDING_ADJ"};

                uploadLogger.debug("File: " + tmpFile.getAbsolutePath());
                uploadLogger.debug("FileName: " + fileName);
                uploadLogger.debug("MimeType: " + mimeType);
                uploadLogger.debug("File Length: " + length);
                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");
                LocalDateTime now = LocalDateTime.now();
                File f2 = null;
                f2 = new File(busId+"_"+dtf.format(now)+".csv");
                tmpFile.renameTo(f2);
                try {
                    ///var/lib/tomcat8/ in linux
                    ///D:\home\site\wwwroot\ in Windows
                    uploadLogger.debug("f2 absolutepath: " + f2.getAbsolutePath());
                    uploadLogger.debug("f2 canonical path: " + f2.getCanonicalPath());
                    CloudBlockBlob blob = container.getBlockBlobReference(f2.getName());
                    if(f2.length() > 0){
                        blob.uploadFromFile(f2.getAbsolutePath());
                        Notification.show("File upload completed.",Notification.Type.TRAY_NOTIFICATION);
                    }
                    CSVReader reader = new CSVReader(new FileReader(f2.getAbsolutePath()), ',' , '"' , 0);
                    //read header name
                    //String[] myheader = reader.readNext();

                    //NOTE :: Store all row and column from csv info List of String Array
                    myEntries = reader.readAll(); 
                    if (myEntries != null && !myEntries.isEmpty()) {
                        boolean success = uploadDAO.insertUploaderEntry(myEntries,busId, userId,"");
                        uploadLogger.debug("SUCCESSS??? " + success);
                        if(success){
                            Notification successNotify = new Notification("Record has been created successfully.","Upload Successful!");
                            successNotify.setDelayMsec(3000);
                            successNotify.setStyleName(ValoTheme.NOTIFICATION_SUCCESS);
                            successNotify.setPosition(Position.MIDDLE_CENTER);
                            successNotify.show(Page.getCurrent());
                        }else {
                            Notification.show("Error in submitting uploaded record.","Upload failed!"
                                    , Notification.Type.ERROR_MESSAGE).setDelayMsec(3000);
                        }
                        Thread.sleep(3000); //added to see if the delay solves the problem or not.
                    }
                } catch (URISyntaxException | StorageException | IOException ex) {
                    new Notification("Could not open file",ex.getMessage(),Notification.Type.ERROR_MESSAGE).show(Page.getCurrent());
                    uploadLogger.debug(ex);
                } catch (InterruptedException ix) {
                    uploadLogger.debug("Interrupted Exception found: " + ix.getMessage());
                } 
            }

            @Override
            protected boolean supportsFileDrops() {
                return false;
            }   
        };
        multiFileUpload.setMaxFileCount(1);
        multiFileUpload.setUploadButtonCaption("Upload CSV file here");
        multiFileUpload.setMaxFileSize(fileSizeLimit); // 2MB
        multiFileUpload.setAcceptFilter(".csv");

我们不确定这个问题是否是组件的已知限制。

我们在此过程中发现的一些问题是:

a) 是否有更好的方法或控制文件上传并避免打开文件/文件未找到错误?

b) setAcceptedFilter 方法中的值是 mime/type 值还是其他值。我们注意到对于图像,它是 "images/*" 但对于 csv,我们必须输入为“.csv”

回答你的第二个问题。 acceptFilter 直接传递给上传输入 "accept" 属性,因此 .csv 和 text/csv 应该都可以。有关更多说明,请参阅 https://www.w3schools.com/tags/att_input_accept.asp