java - 无法在 Spring Boot 中使用 jax rs api 发布照片
问题描述
下面是控制器类的代码,它是控制器类的一个方法,它有 2 个用于发布照片和获取照片的方法。无法使用此方法进行映射,它使用 ajax API 调用我猜问题是无法使用此方法映射的 ajax api,我不确定这里有什么问题
@Path("/{id}/photo")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response updatePhoto(@PathParam("id") String id, @FormDataParam("photo") InputStream is) throws RecordNotFoundException {
boolean updated = sanService.updatePhoto(id, is);
if (updated)
return Response.status(Status.OK).entity("").build();
else
return Response.status(Status.NOT_MODIFIED).entity("").build();
}
@GET
@Path("/{id}/photo")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response getPhoto(@PathParam("id") String id) {
String result = sanService.getPhoto(id);
if (result == null)
return Response.status(Status.NOT_FOUND).entity("").build();
else {
File file = new File(result);
ResponseBuilder response = Response.status(Status.OK).entity((Object) file);
response.header("Content-Disposition", "attachment; filename=" + id + ".png");
return response.build();
}
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFileLocation = "C://"
+ fileDetail.getFileName();
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(
uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
以下是当我点击发布照片的路径时在邮递员中收到的错误
{
"timestamp": "2020-04-03T17:10:10.105+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/5cedvcb6rfsss5ff300eb/photo"
}
解决方案
推荐阅读
- laravel - Laravel Backpack enableAjaxTable 不存在
- java - 多层感知器的置信矩阵
- user-interface - 我如何在centos7中安装cassandra GUI
- angular - (Angular,Nebular)使用 JWT 和 SMS 的两因素身份验证
- nginx - 重写一个url参数并保留其他
- python - AttributeError:“列表”对象没有属性“iloc”
- c++ - 无法使用网络摄像头运行 c++ 项目,但它实际上可以使用图像
- javascript - 如何从 POST 路由中的 GET 路由获取 req.params
- python-3.x - 如何编码此字符串以将其导出为 PDF
- amazon-web-services - 将 AWS AppSync API 版本控制到 Github