Skip to content

Instantly share code, notes, and snippets.

@vijayakumar-psg587
Created January 17, 2019 16:32
Show Gist options
  • Save vijayakumar-psg587/030b8097c95c39d45bee06181906f7ff to your computer and use it in GitHub Desktop.
Save vijayakumar-psg587/030b8097c95c39d45bee06181906f7ff to your computer and use it in GitHub Desktop.
FileProcessorController
@GetMapping("/reportFile")
@ApiOperation(produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET", response = String.class,
value = "GetAll feed back plans based on planMatch and cost", notes = "Sample note")
public void processFileAndOutput(HttpServletResponse res) {
//Read the file from an FTP server
//For now read it from the resource folder
// ApplicationHome home = new ApplicationHome(this.getClass());
// File dir = home.getDir();
// System.out.println("dfs"+dir);
Path filePath = Paths.get(System.getProperty("user.dir")+"/static/test.txt");
//Path path =Paths.get("/Users/A634885/STS/workspace/FeedBackAPI/static/test.txt");
System.out.println(filePath);
try {
List<FileReadDTO> list1= fileService.readFileFromFTPAndGenerateObjects(filePath);
list1.forEach(item -> System.out.println(item));
String fileName = "reportFile.txt";
FileOutputStream output = new FileOutputStream(System.getProperty("user.dir")+"/static/"+fileName);
list1.forEach(item -> {
System.out.println("IN each iteration");
try {
output.write(item.getPlanNbr().getBytes());
output.write(item.getSsnNumber().getBytes());
output.write(item.getDivision().getBytes());
output.write(item.getRestOfFile().getBytes());
output.write("\n".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
System.out.println("before closing");
output.close();
res.setContentType(MediaType.TEXT_PLAIN_VALUE);
res.setHeader("Content-Disposition", "attachment;filename="+fileName);
//Path to output file
//Path outputFilePath = Paths.get();
//return new ResponseEntity<>("sdfds",HttpStatus.OK);
FileCopyUtils.copy(new FileInputStream(new File(System.getProperty("user.dir")+"/static/"+fileName)), res.getOutputStream());
}catch(IOException e) {
throw new RuntimeException("some err");
}
//get the configinfo from DB
//perform some calculations
//generate report file based on that
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment