RESTful webservice using Spring mvc respond back w

2019-09-19 03:31发布

问题:

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.??