java - Jersey 、Spring Boot、上传图像不支持的媒体类型

标签 java rest spring-boot jax-rs

我正在使用 jersey (JAX-RS) 和 spring-boot 以及 jersey 版本 2.25.1,并且我正在尝试发出一个 POST 请求,在其中上传 1 个文件。

我检查了 stackoverflow 上的每个链接,几乎所有链接都告诉我注册 MultipartFeature 并添加 init-params。我已经进行了所有更改,但仍然遇到相同的错误:

@Component
@ApplicationPath("/secure")
    public class JerseyInitialization extends ResourceConfig {
          public JerseyInitialization() {
                this.register(new JacksonJsonProvider(ObjectMapperFactory.create()));
                this.register(MultiPartFeature.class);
                this.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
                this.property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
                this.packages(true, "com.jersey.resources");
            }
    }

我上传图像的帖子请求是:

@Path("/images")
@Consumes(MediaType.APPLICATION_JSON)
@Component
@Transactional
public class ImageResource {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Image uploadFile(@Valid Image image,
                             @FormDataParam("file") InputStream uploadedInputStream,
                             @FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException {

            image.setData(IOUtils.toByteArray(uploadedInputStream));
            image.setFilename(fileDetail.getFileName());

            System.out.println("File uploaded Successfully");

            return imageDao.save(image);
    }

}

我的应用程序类如下:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class).run(args);

    }

    @Bean
    public ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/resources");
        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitialization.class.getName());
        registration.addInitParameter("javax.ws.rs.Application", "com.verico.multipart.app.MultiPartApp");
        return registration;
    }
}

当我使用以下命令运行curl时:

curl -X POST -H "Authorization: Bearer db4719e2-dd76-4977-9dab-e44670213c63" -F "image=@kreditech.jpg" -F "name=kreditech.jpg" localhost:8080/api/secure/images/upload

我收到如下错误:

"status":415,"error":"Unsupported Media Type","message":"Unsupported Media Type"

最佳答案

在课前删除@Consumes(MediaType.APPLICATION_JSON)并引用以下代码上传图片。

@Path("/upload")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@DefaultValue("") @FormDataParam("tags") String tags, 
            @FormDataParam("file") InputStream file,
            @FormDataParam("file") FormDataContentDisposition fileDisposition) {

    String fileName = fileDisposition.getFileName();

    saveFile(file, fileName);

    String fileDetails = "File saved at /Volumes/Drive2/temp/file/" + fileName + " with tags "+ tags;

    System.out.println(fileDetails);

    return Response.ok(fileDetails).build();
}

private void saveFile(InputStream file, String name) {
    try {
        /* Change directory path */
        java.nio.file.Path path = FileSystems.getDefault().getPath("/Volumes/Drive2/temp/file/" + name); 
        /* Save InputStream as file */
        Files.copy(file, path);
    } catch (IOException ie) {
        ie.printStackTrace();
    }
}

关于java - Jersey 、Spring Boot、上传图像不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45017441/

相关文章:

java - 有没有 Javadoc 拼写检查器?

java - 使用 bash 脚本/grep/sed 以用文件名本身替换类名

spring - java.lang.RuntimeException : Driver com. mysql.cj.jdbc.Driver 声称不接受 jdbcUrl、jdbc:oracle:thin:@zzz:1527:yyy

java - 如何将所需格式的值传递给 @RequestParam 中的 Date 对象?

java - C#、Java SQLite 无法读取/写入

java - Spring中如何在DTO中添加 `List<string>`

rest - REST API 如何知道用户是否登录?

Java 循环中的异步休息调用

php - 对应的restful PHP函数有没有标准?

spring - 配置文件特定属性与环境变量