Issue
I am new to file handling. I want to read the uploaded text or CSV file. The controller loooks like this:
@PostMapping("/{testid}/consumers")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ResponseStatus(HttpStatus.OK)
public Subscription createConsumer(@PathVariable String cid, @FormParam("file") InputStream inputStream)
And I am reading this in service like this:
public Subscription createSubscription(String campaingid, InputStream file) {
BufferedReader br = new BufferedReader(new InputStreamReader(file));
log.debug("Write File--------------------------- ");
String line = null;
try {
while ((line=br.readLine()) != null) {
log.debug("Line: " + line);
} catch (IOException ioe) {
log.error("Exception while reading input " + ioe);
}
}
But I am getting null value in 'br' object. What is wrong I am doing here? I am not using jersey framework. I am using spring boot rest.
Solution
I got solution for this ! I modify it as follows:
@PostMapping("/{testid}/consumers")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ResponseStatus(HttpStatus.OK)
public Subscription createConsumer(@PathVariable String cid, @RequestParam("file") MultipartFile file)
And I am reading it as follows:
InputStream inputStream = file.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
list = br.lines().collect(Collectors.toList());
Answered By - pan1490
Answer Checked By - Marie Seifert (JavaFixing Admin)