我想创建一个API,生成与Google Earth / Maps API兼容的KML文件以供下载。
目前我面临406不可接受的错误。
代码如下:
@RequestMapping(
value = "/kml",
method = RequestMethod.GET)
public @ResponseBody HttpEntity<InputStream> kml() throws IOException
{
InputStream is = geocodingManager.generateKml(geocodingProviderId);
HttpHeaders headers = new HttpHeaders();
headers.add("content-disposition", "attachment; filename=Samo.kml");
headers.add("content-type", "application/vnd.google-earth.kml+xml");
return new HttpEntity<InputStream>(is, headers);
}
如果我使用浏览器或RESTClient插件调用API(http://localhost/context/path-to/kml
)(即使明确设置了Accept: *.*
或Accept: */*
标头),我总是会收到错误406 Not Acceptable
。< / p>
我首先尝试将produces
属性设置为注释,然后决定将其作为尝试删除,但这并没有解决。
如何将KML下载到我的浏览器?
答案 0 :(得分:0)
其中一种方式
@RequestMapping(value = "/kml")
public void kml(HttpServletRequest request,HttpServletResponse response) throws IOException
{
InputStream is = request.getServletContext().getResourceAsStream("/WEB-INF/1.0_week_age.kml");;
IOUtils.copy(is, response.getOutputStream());
}
或
@RequestMapping(value = "/kml")
public ResponseEntity<byte[]> kml(HttpServletRequest request) throws IOException
{ //get your input stream here
InputStream is = request.getServletContext().getResourceAsStream("/WEB-INF/1.0_week_age.kml");
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(IOUtils.toByteArray(is), headers, HttpStatus.CREATED);
}
或者甚至可以使用默认的ByteArrayMessageConverter
@RequestMapping(value = "/kml")
public @ResponseBody byte[] kml(HttpServletRequest request,HttpServletResponse response) throws IOException
{
InputStream is = request.getServletContext().getResourceAsStream("/WEB-INF/1.0_week_age.kml");
return IOUtils.toByteArray(is);
}
如果正在访问某些休息服务然后返回kml,您也可以考虑使用RestTemplate