Severe error for using “multipart/form-data” for a file upload service - Apache Jersey

会有一股神秘感。 提交于 2019-12-13 12:14:16

问题


I get this error:

SEVERE: Resource methods utilizing @FormParam and consuming "multipart/form-data" are no longer supported. See @FormDataParam

When a client web access is done for a Apache Jersey based Rest web service I am working right now:

@POST
@Path("upload")
@Consumes("multipart/form-data")
@Produces("text/plain")
public String uploadFile(@FormParam("file") File file, @FormParam("file") FormDataContentDisposition fileDetail) {
    String fileLocation = "/files/" + fileDetail.getFileName();
    System.out.println("File location: " + fileLocation);
    // Load image
    try {
        byte[] imageBytes = loadImage(fileLocation);
        MongoConnection conn = MongoUtil.getConnection();
        conn.connect("m1", "avatar"); 
        GridFS fs = new GridFS(conn.getDB());
        GridFSInputFile in = fs.createFile(imageBytes);
        in.save();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "1";
}

I have tried changing from @FormParam to @FormDataParam but it's unresolved.

What could be the fix for this?


回答1:


You will have to download and use jersey-multipart.jar




回答2:


Try this:

@Path("upload")
@Consumes("multipart/form-data")
@POST
public void handleUpload(@FormParam("file") InputStream file) throws Exception {
// do your thing
}

You can also refer this post.

For Client Side:

import java.io.File;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.multipart.FormDataMultiPart;

public class UploadExample {
  public void upload(String url, File f, String formName) {
    FormDataMultiPart form = new FormDataMultiPart().field(formName, f, MediaType.MULTIPART_FORM_DATA_TYPE);
    WebResource webResource = Client.create().resource(url);
    webResource.type(MediaType.MULTIPART_FORM_DATA)
               .accept(MediaType.TEXT_PLAIN)
               .post(form);
    }
}


来源:https://stackoverflow.com/questions/8658251/severe-error-for-using-multipart-form-data-for-a-file-upload-service-apache

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!