How can I reply back with a text file containing json data as requested by the client.
Request URL:
http://localhost:8082/web/ws/datafileid/json/Sat May 16 12:05:07 IST 2015.txt/
Controller code processing the request:
@RequestMapping(value=EmpRestURIConstants.DATAFILE_REQUEST,method=RequestMethod.GET) @ResponseBody public String datafileresponse(@PathVariable("filename") String filename, HttpServletResponse response) { return cinehomeRestService.checkfilevalid(filename); }
The service class that processes the file check request exists:
@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 answer with datafile.txt that exists in the resources location. How to complete the task. Can anyone help?
I tried the 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 + "\"" )
i CONTACT HOW TO ...
{ "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 does this status mean. Can the client extract this file with this answer.
rest file spring-mvc web-services response
shalu May 18 '15 at 6:23 2015-05-18 06:23
source share