read data from MultipartFile which has csv uploaded from browser
Asked Answered
J

4

20

May be I am doing it worng by using MultipartFile upload feature.

I have to read data from csv file which will be chosen by the client through the browser. I used MultipartFile to upload file. The file is coming to the controller but now I am unable to read csv data from it. Please guide the best way to do it or help me read csv data from MultipartFile. The jsp has

    <form method="POST" action="uploadFile" enctype="multipart/form-data">
            File to upload: <input type="file" name="file"> <input
                type="submit" value="Upload"> Press here to upload the
            file!
        </form>

The controller has

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFileHandler(@RequestParam("file") MultipartFile file) {

Thanks.

Joletta answered 15/10, 2015 at 8:40 Comment(1)
Are you getting errors? Or are you trying to process the MultipartFile with a class that is looking for a File object? You need to provide more information about what you are trying to do or the results you are seeing.Fechner
J
12

I figured out a workaround. I converted the file to bytes and then converted the bytes to String. From String I applied string.split() to get what I wanted out of the file.

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFileHandler(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
            String completeData = new String(bytes);
            String[] rows = completeData.split("#");
            String[] columns = rows[0].split(",");
Joletta answered 16/10, 2015 at 10:39 Comment(0)
C
36

I used a buffer to read line by line and get from multipart the inputstream. Maybe is more code, but I find helpful read text file by lines.

BufferedReader br;
List<String> result = new ArrayList<>();
try {

     String line;
     InputStream is = multipart.getInputStream();
     br = new BufferedReader(new InputStreamReader(is));
     while ((line = br.readLine()) != null) {
          result.add(line);
     }

  } catch (IOException e) {
    System.err.println(e.getMessage());       
  }
Chiou answered 26/9, 2017 at 10:43 Comment(0)
J
12

I figured out a workaround. I converted the file to bytes and then converted the bytes to String. From String I applied string.split() to get what I wanted out of the file.

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFileHandler(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
            String completeData = new String(bytes);
            String[] rows = completeData.split("#");
            String[] columns = rows[0].split(",");
Joletta answered 16/10, 2015 at 10:39 Comment(0)
C
4

The best solution that I found was

@PostMapping(value="/csv")
public ResponseEntity<Void> processUpload(@RequestParam MultipartFile file) {

BufferedReader fileReader = new BufferedReader(new 
InputStreamReader(file.getInputStream(), "UTF-8"));
CSVParser csvParser = new CSVParser(fileReader, CSVFormat.DEFAULT);

Iterable<CSVRecord> csvRecords = csvParser.getRecords();

for (CSVRecord csvRecord : csvRecords) {
    System.out.println(csvRecord);
}
...

This is an adaptative solution from https://bezkoder.com/spring-boot-upload-csv-file/

Campney answered 18/12, 2020 at 1:55 Comment(0)
L
0

Probably you can try a generic Utility to convert CSV to POJO and extend for other Data types

            import java.io.BufferedReader;
            import java.io.File;
            import java.io.FileInputStream;
            import java.io.InputStreamReader;
            import java.io.Reader;
            import java.lang.reflect.Field;
            import java.lang.reflect.Method;
            import java.util.ArrayList;
            import java.util.Arrays;
            import java.util.Date;
            import java.util.List;

            import org.apache.commons.collections4.CollectionUtils;
            import org.apache.commons.csv.CSVFormat;
            import org.apache.commons.csv.CSVParser;
            import org.apache.commons.csv.CSVRecord;
            import org.springframework.util.ResourceUtils;
            import org.springframework.util.StringUtils;
            import org.springframework.web.multipart.MultipartFile;

            import com.opencsv.bean.CsvToBeanBuilder;

            import stocktales.durations.UtilDurations;

            public class CSV2POJOUtility
            {
                @SuppressWarnings(value =
                { "rawtypes", "unchecked" })
                public static <T> List<T> getPOJOS4mCsv(String filePath, Class<T> pojoClass) throws Exception
                {
                    List<T> pojos = null;
                    if (StringUtils.hasText(filePath))
                    {
                        if (pojoClass != null)
                        {
                            File file = new File(filePath);
                            if (file != null)
                            {
                                FileInputStream fileStream;

                                try
                                {
                                    fileStream = new FileInputStream(file);
                                    Reader reader = new InputStreamReader(fileStream);
                                    if (reader != null)
                                    {
                                        pojos = (List<T>) new CsvToBeanBuilder(reader).withSkipLines(1)
                                                .withType(pojoClass.getClass()).build().parse();
                                    }

                                }

                                catch (Exception e)
                                {
                                    //Try from Class Path instead
                                    file = ResourceUtils.getFile("classpath:" + filePath);
                                    if (file != null)
                                    {
                                        try
                                        {
                                            fileStream = new FileInputStream(file);
                                            Reader reader = new InputStreamReader(fileStream);
                                            if (reader != null)
                                            {
                                                pojos = (List<T>) new CsvToBeanBuilder(reader).withSkipLines(1).withType(pojoClass)
                                                        .build().parse();

                                            }
                                        }

                                        catch (Exception e1)
                                        {
                                            throw e1;
                                        }
                                    }

                                }

                            }

                        }
                    }

                    return pojos;
                }

                public static <T> List<T> getPOJOS4mCsv(MultipartFile file, Class<T> pojoClass, boolean skipFirstLine)
                        throws Exception
                {
                    List<T> pojos = null;
                    if (!file.isEmpty())
                    {
                        if (pojoClass != null)
                        {

                            if (file.getInputStream() != null)
                            {

                                BufferedReader fileReader = new BufferedReader(
                                        new InputStreamReader(file.getInputStream(), "UTF-8"));

                                CSVParser csvParser = new CSVParser(fileReader, CSVFormat.DEFAULT);

                                Iterable<CSVRecord> csvRecords = csvParser.getRecords();
                                if (csvRecords.iterator() != null)
                                {
                                    long cnt = 0;
                                    pojos = new ArrayList<T>();
                                    // Get the fields of the class object
                                    Field[] fields = pojoClass.getDeclaredFields();
                                    List<Method> methods = getSetters(pojoClass);
                                    List<Field> fieldsList = Arrays.asList(fields);
                                    if (CollectionUtils.isNotEmpty(methods) && CollectionUtils.isNotEmpty(fieldsList))
                                    {
                                        for (CSVRecord csvRecord : csvRecords)
                                        {
                                            cnt += 1;
                                            if (skipFirstLine)
                                            {
                                                if (cnt > 1)
                                                {
                                                    setFlds(pojoClass, pojos, methods, fieldsList, csvRecord);
                                                }
                                            }
                                            else
                                            {
                                                setFlds(pojoClass, pojos, methods, fieldsList, csvRecord);
                                            }

                                        }

                                    }

                                }
                                csvParser.close();

                            }

                        }
                    }

                    return pojos;
                }

                private static <T> void setFlds(Class<T> pojoClass, List<T> pojos, List<Method> methods, List<Field> fieldsList,
                        CSVRecord csvRecord) throws Exception
                {

                    Object classObject = (T) pojoClass.newInstance();

                    int fldCnt = 0;
                    //Number of Columns must equal Setter Methods
                    if (csvRecord.size() == methods.size())
                    {
                        for (Field field : fieldsList)
                        {
                            Method method = methods.stream().filter(m -> m.getName().toLowerCase().contains(field.getName()))
                                    .findFirst().get();
                            if (method != null)
                            {
                                Object val = csvRecord.get(fldCnt);
                                if (field.getType().equals(Date.class))
                                {

                                    Date valDate = UtilDurations.getDateFromString(val.toString());
                                    method.invoke(classObject, valDate);

                                }
                                else
                                {
                                    method.invoke(classObject, val);
                                }

                                fldCnt++;
                            }
                        }
                    }

                    pojos.add((T) classObject);
                }

                public static List<Method> getSetters(Class<?> c)
                {
                    List<Method> setters = new ArrayList<>();
                    for (Method method : c.getDeclaredMethods())
                    {
                        if (method.getName().startsWith("set") && method.getParameterCount() == 1)
                        {
                            setters.add(method);
                        }
                    }
                    return setters;
                }

            }
Leaved answered 3/1 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.