https://github.com/andresp08/springboot-file-upload
This Spring Boot application is designed to facilitate generic CSV file uploads with a focus on flexibility and ease of use. The primary goal of this project is to allow users to upload CSV files.
https://github.com/andresp08/springboot-file-upload
file-upload java maven opencsv postgresql springboot swagger-ui
Last synced: 8 months ago
JSON representation
This Spring Boot application is designed to facilitate generic CSV file uploads with a focus on flexibility and ease of use. The primary goal of this project is to allow users to upload CSV files.
- Host: GitHub
- URL: https://github.com/andresp08/springboot-file-upload
- Owner: Andresp08
- Created: 2023-11-13T16:48:02.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-13T18:46:26.000Z (about 2 years ago)
- Last Synced: 2025-02-28T12:07:40.715Z (12 months ago)
- Topics: file-upload, java, maven, opencsv, postgresql, springboot, swagger-ui
- Language: Java
- Homepage:
- Size: 70.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SpringBoot File Upload
`Swagger Documentation`

## Upload files with springboot using opencsv
## If you want to use this generic file upload without the anotation @CsvBindByName, you can replace the GenericCsvParseToEntity methods for:
public Set parseCsvToEntity(MultipartFile file, Class entityClass) throws IOException,
CsvValidationException {
Set entities = new HashSet<>();
try (CSVReader reader = new CSVReader(new InputStreamReader(file.getInputStream()))) {
String[] header = reader.readNext();
String[] line;
while ((line = reader.readNext()) != null) {
Entity entity = buildEntity(header, line, entityClass);
entities.add(entity);
}
}
return entities;
}
private Entity buildEntity(String[] header, String[] line, Class entityClass) {
try {
Entity entity = entityClass.getDeclaredConstructor().newInstance();
for (int i = 0; i < header.length; i++) {
String columnName = header[i];
String cellValue = line[i];
Field field = entityClass.getDeclaredField(columnName);
field.setAccessible(true);
if (field.getType() == String.class) {
field.set(entity, cellValue);
} else if (field.getType() == int.class) {
field.set(entity, Integer.parseInt(cellValue));
}
field.setAccessible(false);
}
return entity;
} catch (Exception e) {
throw new RuntimeException("Error al construir entidad", e);
}
}