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.
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With