简体   繁体   中英

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:

a) only CSV files are allowed, with a cap size of 1MB per file. b) only one file can be submitted at a time.

We just did an uploading test on small-sized CSV files that are below 100Kb. 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:

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.

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.

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.

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?

b) Are the values in the setAcceptedFilter method the mime/type values or something else. We noticed for images, it's "images/*" but for csv, we had to put in as ".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. See https://www.w3schools.com/tags/att_input_accept.asp for more instructions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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