问题
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