0

How can i respond back with a text file containing json data upon request from client .

The request url is:

http://localhost:8082/web/ws/datafileid/json/Sat May 16 12:05:07 IST 2015.txt/

Controller code that handles the request is:

@RequestMapping(value=EmpRestURIConstants.DATAFILE_REQUEST,method=RequestMethod.GET)
@ResponseBody
public String datafileresponse(@PathVariable("filename") String filename, HttpServletResponse response) {
  return cinehomeRestService.checkfilevalid(filename);
}

Service class that handles the request to check the file exists is:

@Override
public String checkfilevalid(String filename) {
  String datafilename=webServiceDao.getdatafilename();
  JSONObject obj = new JSONObject();
  if(datafilename.equals(filename)) {
    return "file";
  }
  else {
    try {
      obj.put("status", "022");
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return obj.toString();
  }
}

Here I need to respond back with the datafile.txt that exists at location resources. How can I perform the task. Can anyone help?

i have tried a method

@RequestMapping(value=EmpRestURIConstants.DATAFILE_REQUEST,method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Response datafileresponse(@PathVariable("filename")String filename) throws IOException{

 JSONObject readdata = new JSONObject();
 String uploadPath = servletContext.getRealPath("");

String fullyqualifiedfilename=uploadPath+filename;

System.out.println("+++++++++"+fullyqualifiedfilename);

          return Response.ok(uploadPath)
              .header("Content-Disposition", "attachment; filename=\"" + fullyqualifiedfilename + "\"" ) //optional
              .build();

}

i GOT THE REPLY AS...

{ "statusType": "OK", "entity": /home/cine/WORKSPACES/study/.metadata/.plugins/org.eclipse.wst.server.‌​core/tmp0/wtpwebapps/
 "entityType": "java.lang.String", "metadata": { "Content-Disposition": [ "attachment; 

 filename="/home/cine/WORKSPACES/study/.metadata/.plugins/org.eclipse.wst.server.‌​core/tmp0/wtpwebapps/Cine Sat May 16 12:05:07 IST 2015.txt"" ] }, "status": 200 }

What this status means. Does the client can fetch teh file using this response.??

shalu
  • 113
  • 2
  • 14
  • possible duplicate of [Downloading a file from spring controllers](http://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers) – Philipp Reichart May 19 '15 at 18:21
  • You're coming from [an answer about downloading a binary file using Jersey JAX-RS](http://stackoverflow.com/a/12251265/180740), but you want to download a txt file using Spring MVC. Take a look at [ResponseEntity](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html) or the "downloading a file" question linked above this comment instead. – Philipp Reichart May 19 '15 at 18:24
  • @philipp i ws not aware about that the code i used was jersey jax-rs. Is this the code section you mentioned. return Response.ok(uploadPath) .header("Content-Disposition", "attachment; filename=\"" + fullyqualifiedfilename + "\"" ) //optional .build(); – shalu May 20 '15 at 11:32
  • Yes, that code doesn't work as-is with Spring MVC. Consider building and returning a [ResponseEntity](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html) instead. – Philipp Reichart May 20 '15 at 21:49

0 Answers0