首页 > 解决方案 > 无法在 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"
}

标签: javaspringspring-bootspring-datajax-rs

解决方案


推荐阅读