Ravi Teja Ravi Teja - 3 months ago 24
reST (reStructuredText) Question

how to read file sent from http post in jersy

this code i tested for values

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String getE(@FormDataParam("file") InputStream inputStream,@FormDataParam("file") FormDataContentDisposition fileMetaData) {
String out = "";
Map<String, String> l = fileMetaData.getParameters();
Collection<String> s = l.values();
Iterator i = s.iterator();
while(i.hasNext())
{
out += i.next()+" ";
}


return out;
}

i am getting this exception

SEVERE: StandardWrapper.Throwable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public java.lang.String my.Service.getE(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[text/plain], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class my.Service, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@4023c98]}, definitionMethod=public java.lang.String my.Service.getE(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']

Dependencies are

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.16</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.16</version>
</dependency>


my service class

@Path("empdata")


public class Service {

EmpService service = new EmpService();
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String getE(@FormDataParam("file") InputStream inputStream,@FormDataParam("file") FormDataContentDisposition fileMetaData) {
String out = "";
Map<String, String> l = fileMetaData.getParameters();
Collection<String> s = l.values();
Iterator i = s.iterator();
while(i.hasNext())
{
out += i.next()+" ";
}
return out;//
}


}

and i registered my ResourceConfig sub class

this is how i am sending request and file
this is how my request header and body part looks like
Thanks!

Answer

Adding Jersey multipart dependecy

To use multipart features in your Jersey application, you need to add the jersey-media-multipart module to your pom.xml file:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.23.1</version>
</dependency>

If you're not using Maven make sure to have all needed dependencies (see the jersey-media-multipart artifact dependencies) on the classpath.

Registering the multipart feature

Besides adding the dependency, you need to register the MultiPartFeature. See the approaches below:

If you have an Application/ResourceConfig sub-class, do as following:

@ApplicationPath("/api")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(MultipartFeature.class);
        return classes;
    }
}
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        register(MultipartFeature.class);
    }
}

If you don't have an Application/ResourceConfig sub-class, you can register the MultiPartFeature in your web.xml deployment descriptor. The specific resource, provider and feature fully-qualified class names can be provided in a comma-separated value of jersey.config.server.provider.classnames initialization parameter.

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>

Handling multipart requests

Use the @FormDataParam annotation to bind the named body part(s) of a multipart/form-data request entity body to a resource method parameter, as following:

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(@FormDataParam("file") InputStream inputStream,
                       @FormDataParam("file") FormDataContentDisposition fileMetaData) {
    ...
}

For more details, check the Jersey documentation about multipart requests.