如何多次上传一个文件?
How can I upload a file multiple times?
这是我的代码,我在这里接受 excel 文件并读取它。之后我读取了行值的 excel 文件并将它们插入到数据库中,然后插入到适当的列中。最后,我把文件放在我在电脑上确定的文件夹中。但是我在这里遇到了一个问题,当我想在 api 中多次下载同名文件时,它显示我只是第一次将它上传到文件夹中,但它打印了值到数据库。我对此没有任何逻辑,你认为我能在这里做什么?
@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);
}
尝试为每个文件创建一个唯一的文件名,例如:
String destination = "C:\Users\anar.memmedov\Desktop\app\" + System.nanoTime()+"_"+filePath.getOriginalFilename();
我改变了一些东西并开始工作,它看起来像这样
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
String destination = "C:\Users\anar.memmedov\Desktop\app\" + "_" + LocalDateTime.now().format(dateTimeFormatter) + "_" + filePath.getOriginalFilename();
这是我的代码,我在这里接受 excel 文件并读取它。之后我读取了行值的 excel 文件并将它们插入到数据库中,然后插入到适当的列中。最后,我把文件放在我在电脑上确定的文件夹中。但是我在这里遇到了一个问题,当我想在 api 中多次下载同名文件时,它显示我只是第一次将它上传到文件夹中,但它打印了值到数据库。我对此没有任何逻辑,你认为我能在这里做什么?
@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);
}
尝试为每个文件创建一个唯一的文件名,例如:
String destination = "C:\Users\anar.memmedov\Desktop\app\" + System.nanoTime()+"_"+filePath.getOriginalFilename();
我改变了一些东西并开始工作,它看起来像这样
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
String destination = "C:\Users\anar.memmedov\Desktop\app\" + "_" + LocalDateTime.now().format(dateTimeFormatter) + "_" + filePath.getOriginalFilename();