使用Spring mvc的RESTful webservice以数据文件作为响应进行响应

时间:2015-05-18 06:23:40

标签: web-services file rest spring-mvc response

如何根据客户的要求回复包含json数据的文本文件。

请求网址为:

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

处理请求的控制器代码是:

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

处理检查文件的请求的服务类是:

@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();
  }
}

在此,我需要回复位置datafile.txt中存在的resources。我该如何执行任务。有人可以帮忙吗?

我尝试了一种方法

@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();

}

我把答复作为......

{ "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 }

这种状态意味着什么。客户端是否可以使用此响应来获取文件。??

0 个答案:

没有答案