简体   繁体   English

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

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

We are currently using the EasyUpload add-on, and we have specified the criteria for this component: 我们当前正在使用EasyUpload附加组件,并且已经为此组件指定了条件:

a) only CSV files are allowed, with a cap size of 1MB per file. a)只允许CSV文件,每个文件的上限为1MB。 b) only one file can be submitted at a time. b)一次只能提交一个文件。

We just did an uploading test on small-sized CSV files that are below 100Kb. 我们刚刚对100Kb以下的小型CSV文件进行了上传测试。 Usually, the upload process completes successfully. 通常,上传过程会成功完成。 Occasionally, the error of "Could not open file, The system cannot find the file specified" is displayed although the file is already inside the temp folder, and we found that this happens either when: 有时,尽管文件已经在temp文件夹中,但仍显示错误“无法打开文件,系统找不到指定的文件”,并且我们发现在以下情况下会发生这种情况:

a) If the same file is uploaded again after making a small change and within a few seconds after the file has been uploaded successfully. a)如果进行了较小的更改后又在成功上传文件后的几秒钟内再次上传了同一文件。

b) If there are two tabs of the web app, logged under different users were uploading their respective csv files and they also do the same thing of changing values in the csv before uploading them again. b)如果该Web应用程序有两个选项卡,则在不同用户登录下分别上传了各自的csv文件,并且在再次上传它们之前,他们在更改csv中的值方面也做同样的事情。

We tried forcing the file upload through (as another testing method) and noticed after a while that the files sometimes get stuck in the queue although we have imposed a one file at a submission time rule. 我们尝试强制通过文件上传(作为另一种测试方法),过了一会儿,尽管我们在提交时间规则中强加了一个文件,但有时文件有时仍卡在队列中。 It was displayed in a message "There are too many files over the count limit". 它显示在消息“文件数超出限制”中。 We also considered of putting a sleep / wait command of 3-5 seconds after the file submission. 我们还考虑了在文件提交后的3-5秒内放置一个sleep / wait命令。

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");

We are unsure whether if this problem is a known limitation of the component or not. 我们不确定此问题是否是组件的已知限制。

Some of the questions we have discovered along the way are: 我们在此过程中发现的一些问题是:

a) Is there a better way or to control on the file uploading and to avoid the open file / file not found error? a)是否有更好的方法或控制文件上传并避免打开文件/找不到文件错误?

b) Are the values in the setAcceptedFilter method the mime/type values or something else. b)setAcceptedFilter方法中的值是mime / type值还是其他值。 We noticed for images, it's "images/*" but for csv, we had to put in as ".csv" 我们注意到图像是“ images / *”,但是对于csv,我们不得不输入为“ .csv”

Answering to your second question. 回答第二个问题。 The acceptFilter is directly passed to upload inputs "accept" attribute, so both .csv and text/csv should do fine. 直接将acceptFilter传递到上传输入的“ accept”属性,因此.csv和text / csv都应该可以正常工作。 See https://www.w3schools.com/tags/att_input_accept.asp for more instructions. 有关更多说明,请参见https://www.w3schools.com/tags/att_input_accept.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM