泽西 2.x Multipart/form-data 服务器 returns 400 错误请求和 MessageBodyWriter NotFoundException

Jersey 2.x Multipart/form-data server returns 400 Bad Request and MessageBodyWriterNotFoundException

当我在 jersey 2.26 中 运行 我的 Multipart/form-data 上传功能时,它 returns 400-Bad request。试了很多方法都没用

上传无文件时,显示响应信息。当我使用客户端时显示“MessageBodyWriterNotFoundException”。

Upload.java

@Path("/upload")
public class UploadFileDemo {
    
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public Response uploadFile(@FormDataParam("file") FileInputStream fileInputStream,
                                @FormDataParam("file") FormDataContentDisposition fileInfo) {
        System.out.println(fileInfo.getFileName()+"-"+fileInfo.getSize());
        String path= "D://"+fileInfo.getFileName();
        int read=0;
        byte[] bytes = new byte[1024];
        try {
            OutputStream out = new FileOutputStream(new File(path));
            while((read = fileInputStream.read(bytes))!=-1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();    
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Response.ok("Data uploaded successfully").build();
    }

}

web.xml

<servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.packages.resource</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.filter.LoggingFilter;
            org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>

仅供参考 - 未显示异常

My logging filter log
Aug 25, 2020 12:53:29 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Server has received a request on thread http-nio-9090-exec-8
1 > POST http://localhost:9090/rest-new-version/webapi/upload/file
1 > accept: */*
1 > accept-encoding: gzip, deflate, br
1 > connection: keep-alive
1 > content-length: 240
1 > content-type: multipart/form-data; boundary=--------------------------247816562830114154909314
1 > host: localhost:9090
1 > postman-token: 91bec8ef-5857-4604-a512-6dc97d5e22c6
1 > user-agent: PostmanRuntime/7.26.3

Aug 25, 2020 12:53:30 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Server responded with a response on thread http-nio-9090-exec-8
1 < 400

请帮助我度过难关:)

FileInputStream 用作参数。所以它返回了 400 Bad request。而不是使用 InputStream

@Path("/upload")
public class UploadFileDemo {
    
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_PLAIN)
    public Response uploadFile(@FormDataParam("file") InputStream fileInputStream,
                                @FormDataParam("file") FormDataContentDisposition fileInfo) {
        System.out.println(fileInfo.getFileName()+"-"+fileInfo.getSize());
        String path= "D://"+fileInfo.getFileName();
        int read=0;
        byte[] bytes = new byte[1024];
        try {
            OutputStream out = new FileOutputStream(new File(path));
            while((read = fileInputStream.read(bytes))!=-1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();    
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Response.ok("Data uploaded successfully").build();
    }

}

这行得通。 快乐编码!!!