Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code, I am here accept excell file and read it .After than I read of excell file of row values and insert them to database,which to the appropriate columns. At the end, I put the file in the folder that I have determined on the computer. But I am having a problem here,When I want to download the file with the same name a few times in the api, it shows that I only uploaded first time it to the folder at first,but it prints the values to the database. I can't make any logic about it, what do you think I can do here?


Java
@Override
    public ResponseEntity<? extends Response> acceptExcelFileAndSenderName(MultipartFile filePath, String senderName) throws IOException {
        String fileName = filePath.getOriginalFilename();
        if (fileName.substring(fileName.length() - 5).equals(".xlsx")) {
            try (InputStream excelFile = filePath.getInputStream()) {
                String phoneNumber = "";
                String textMessage = "";
                Workbook workbook = new XSSFWorkbook(excelFile);
                Sheet datatypeSheet = workbook.getSheetAt(0);
                Iterator<Row> iterator = datatypeSheet.iterator();
                while (iterator.hasNext()) {
                    Row currentRow = iterator.next();
                    Iterator<Cell> cellIterator = currentRow.iterator();

                    while (cellIterator.hasNext()) {

                        Cell currentCell = cellIterator.next();
                        if (currentCell.getCellType() == CellType.NUMERIC) {
                            phoneNumber = NumberToTextConverter.toText(currentCell.getNumericCellValue());
                        } else if (currentCell.getCellType() == CellType.STRING) {
                            textMessage = String.valueOf(currentCell.getStringCellValue());
                        }

                    }
                    FileDetail fileDetail = new FileDetail();
                    fileDetail.setPhoneNumber(phoneNumber);
                    fileDetail.setTextMessage(textMessage);
                    fileDetail.setSender(senderName);
                    this.fileDetailRepository.save(fileDetail);
                }
                excelFile.close();

                String destination = "C:\\Users\\anar.memmedov\\Desktop\\app\\" + filePath.getOriginalFilename();
                File file1 = new File(destination);
                filePath.transferTo(file1);
                return new ResponseEntity<>(new SuccessResponse(MessageCase.FILE_SUCCESSFULLY_WRITTEN_TO_DATABASE.getMessage(), 200), HttpStatus.OK);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new ResponseEntity<>(new ErrorResponse(MessageCase.FAILED_HAPPEND_WHEN_FILE_WRITTEN_TO_DATABASE.getMessage(), 400), HttpStatus.BAD_REQUEST);
    }


What I have tried:

I can't make any logic about it, what do you think I can do here?
Posted
Comments
Member 15329613 19-Oct-21 7:44am    
I do not understand what you are asking us to do.

Also, fileName.substring(fileName.length() - 5).equals(".xlsx") can also be written as
fileName.endsWith(".xlsx")

It's cleaner and easier in my opinion.

https://beginnersbook.com/2013/12/java-string-endswith-method-example/
[no name] 19-Oct-21 7:44am    
As per my requirement,i need to upload a file into a specified directory,later after some modifications, i need to upload the same file into the same directory,here previous file should not be overridden means files must be saved in the same directory with same names(here i have one assumption,that , for example if my file is abc.txt, after modifications if i upload the modified file it can be saved as abc(1).txt ). how can i resolve my issue? can anybody assist me to come out from this issue.
Richard MacCutchan 19-Oct-21 7:56am    
You cannot have multiple files with the same name in a directory. You need to use unique names for each file. You need to modify your code to create unique names when you download them. A common option is to use the date and time to create a unique suffix to the name.
Nelek 19-Oct-21 9:52am    
The OP answered you but missed to hit "reply" to your message. Just in case you want to continue explaining.
Richard MacCutchan 19-Oct-21 10:02am    
Thanks, it seems to be a common issue in QA.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900