根据业务需求,需提供一个下载kml格式航线文件的HTTP GET接口
示例代码
@RequestMapping(value = "/getFlyingPathFile/{uavTypeId}/{flyingPathId}.kml", method = RequestMethod.GET, produces = "application/vnd.google-earth.kml+xml")
@ResponseBody
@ApiOperation("kml航线文件下载")
public byte[] getFlyingPathFile(@ApiParam("flyingPathId") @PathVariable Integer flyingPathId, @ApiParam("uavTypeId") @PathVariable Integer uavTypeId, HttpServletResponse response) {
// 给下载的流文件命名为flyingPathId.kml格式
response.addHeader("Content-Disposition", "attachment;filename=" + flyingPathId + ".kml");
// 通过mybatis获取数据库中的航线文件byte[]数据,直接返回
List<UavType_has_FlyingPathKey> pathFiles = uavTypeHasFlyingPathService.getFlyingPathKeysById(flyingPathId);
for (UavType_has_FlyingPathKey pathFile : pathFiles) {
if (pathFile.getUavtypeid().equals(uavTypeId)) {
return pathFile.getOrginalPathFile();
}
}
return null;
}
避坑
注意:@RequestMapping注解中的produces指定返回值的类型和编码。取值如果用常规的流数据文件类型 “application/octet-stream;charset=UTF-8” ,浏览器请求,会报406错误
kml文件比较特殊,需要指定
produces = "application/vnd.google-earth.kml+xml"
InteIlij IDEA AI编程插件推荐
Plugins --> Raccoon (商汤发布的小浣熊),手机号注册登录即可用,挺方便的。
贴一张使用截图: