Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read data from MultipartFile which has csv uploaded from browser

Tags:

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.

like image 610
romil gaurav Avatar asked Oct 15 '15 08:10

romil gaurav


1 Answers

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());       
  }
like image 151
cpxondo Avatar answered Sep 16 '22 12:09

cpxondo