Web services 使用SpringMVC的RESTfulWebService以数据文件作为响应进行响应
如何在客户端请求时使用包含json数据的文本文件进行响应 请求url为:Web services 使用SpringMVC的RESTfulWebService以数据文件作为响应进行响应,web-services,file,rest,spring-mvc,response,Web Services,File,Rest,Spring Mvc,Response,如何在客户端请求时使用包含json数据的文本文件进行响应 请求url为: 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 datafil
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 }
这个状态意味着什么。客户端是否可以使用此响应获取该文件?可能与您的源文件重复,但您希望使用Spring MVC下载一个txt文件。请看一看或改为在该注释上方链接的“下载文件”问题。@philipp我不知道我使用的代码是jersey jax-rs。这是您提到的代码部分吗。返回Response.ok(uploadPath).header(“内容处置”、“附件;文件名=\”“+fullyqualifiedfilename+”\”“)//可选的.build();是的,该代码与SpringMVC不一样。考虑建立和返回一个替代。