简体   繁体   English

使用JERSEY通过REST API上传文件

[英]Uploading file through REST API with JERSEY

Hi have seen a lot of posts about this, but i cannot find my issue. 嗨,您已经看到很多关于此的帖子,但是我找不到我的问题。

I keep geeting an error 400 我不断遇到错误400

Here is my code 这是我的代码

POST
@Path("/upload")
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadFile(  @FormDataParam("file") InputStream fileInputStream,
                                @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
{
    System.out.println(fileMetaData.getFileName());

    return Response.ok("File caught successfully !!").build();
}

Here are my Jersey dependencies 这是我对泽西岛的依赖

       <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.25</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.25</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.18</version>
        </dependency>

And I've added this to the AppConfiguration 并将其添加到AppConfiguration中

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> resources = new java.util.HashSet<>();
    addRestResourceClasses(resources);
    resources.add(MultiPartFeature.class);
    return resources;
}

In all fairness, it could be my actual HTTP request, so here is my POSTman 公平地说,这可能是我的实际HTTP请求,所以这是我的POSTman

邮递员的身体 邮递员头

//could you please use this code to upload multiples files //您能否使用此代码上传多个文件

    @Path("/files")
        @POST
        @Consumes(MediaType.MULTIPART_FORM_DATA)
        public Response uploadFiles2(@DefaultValue("") @FormDataParam("tags") String tags,
                @FormDataParam("files") List<FormDataBodyPart> bodyParts,
                @FormDataParam("files") FormDataContentDisposition fileDispositions,
                @FormDataParam("file2") InputStream file2,
                @FormDataParam("file2") FormDataContentDisposition fileDisposition2) {

            StringBuffer fileDetails = new StringBuffer("");

            /* Save multiple files */

            for (int i = 0; i < bodyParts.size(); i++) {
                /*
                 * Casting FormDataBodyPart to BodyPartEntity, which can give us
                 * InputStream for uploaded file
                 */
                BodyPartEntity bodyPartEntity = (BodyPartEntity) bodyParts.get(i).getEntity();
                String fileName = bodyParts.get(i).getContentDisposition().getFileName();

                saveFile(bodyPartEntity.getInputStream(), fileName);

                fileDetails.append(" File saved at /Volumes/Drive2/temp/file/" + fileName);
            }

            /* Save File 2 */

            String file2Name = fileDisposition2.getFileName();

            saveFile(file2, file2Name);
            fileDetails.append(" File saved at /Volumes/Drive2/temp/file/" + file2Name);
            fileDetails.append(" Tag Details : " + tags);

            System.out.println(fileDetails);

HTML code is below HTML代码如下

<form action="/upload/files" enctype="multipart/form-data" method="post">
        <label>Select multiple files</label><input type= "file" name="files" multiple /> <br/><br/>
        <label>Select File</label><input type= "file" name="file2" /> <br/><br/>
        <label>Tags</label> <input name="tags" maxlength="10"/> <br/><br/>
        <input type="submit" title="Save"/>
    </form>

one quick way. 一种快速的方法。 remove @Consumes from your method and also remove content-type from the postman. 从方法中删除@Consumes ,并从邮递员中删除内容类型。 it works for me. 这个对我有用。

don't worry Jersey will consider it as multipart form-data 别担心,Jersey会将其视为multipart form-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM