java - Jax-RS /cxf 随机空响应
问题描述
我有一个资源 JAX-RS,它应该从 url 返回图片。它运作良好,但有时对于具有 http 200 代码的相同 url 的响应是空的。
我该如何解决这个问题,谢谢
httpd 日志:
100.xx.xx.xx - - [11/Jun/2020:16:27:46 +0200] [GET /ressources/1000/1 HTTP/1.1] 200 - [http://r-dev.fr/] [Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0]
100.xx.xx.xx - - [11/Jun/2020:16:27:46 +0200] [GET /ressources/1000/1 HTTP/1.1] 200 - [http://r-dev.fr/] [Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0]
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m10</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.7.0</version>
</dependency>
public Response getPicture(final String url) {
WebClient client = WebClient.create(url);
try {
Response r = client.get();
if (r.getStatusInfo().getStatusCode() == Response.Status.OK.getStatusCode()) {
String contentDispo = r.getStringHeaders().getFirst("Content-Disposition");
if (!StringUtils.isEmpty(contentDispo)) {
ContentDisposition cd = new ContentDisposition(contentDispo);
String fileName = cd.getParameter("filename");
String mimeType = defineMimeType(fileName);
Object entity = r.getEntity();
return Response.ok(entity, mimeType).header("Connection", "Keep-Alive").header("Keep-Alive", "timeout=60, max=100").build();
} else {
return Response.serverError().build();
}
} else {
return r;
}
} catch (WebApplicationException | ClientException ex) {
throw new WebApplicationException();
}
}
Api 使用方法从 url 获取图像:
public Response getRessource(String idRessource) { File fileToReturn = ....
return Response.status(Response.Status.OK).entity(new FileInputStream(fileToReturn)).type("application/octet-stream")
.header("Content-Disposition", "attachment; filename=" + fileToReturn.getName()).build();
}
catch (FileNotFoundException e) {
LOGGER.error("Erreur : le fichier d'id {} est introuvable", idRessource, e);
return Response.status(Response.Status.NOT_FOUND).build();
}
}
解决方案
推荐阅读
- javascript - 向单个按钮添加第二个功能
- javascript - 为什么 AWS EB 在部署时抛出错误?
- azure-logic-apps - CosmosDB 连接器/删除分区集合中的文档/设计器使用的错误标头属性
- android - React-native undefined 不是函数(评估 'firebase.auth()')
- mongodb - 什么是二进制 json (BSON) 中的二进制?
- java - 使用 Thymeleaf 的 Sprint MVC 应用程序无法找到具有标准 href 的 CSS?
- android - 运行程序后,Android Studio 在我的设备中安装了 2 个应用程序
- tensorflow - 多标签数字分类 TensorFlow
- algorithm - 算法问题,求解字典中的变量
- php - 如何通过夹具将虚拟数据插入到外部列 symfony