Skip to main content

need help, restful web service (via Jersey) with file upload

6 replies [Last post]
mr_kschan
Offline
Joined: 2007-09-01

i'm facing the problem to handle a file upload http request with the Jersey web service.

i can get the http header (string) and the file uploaded (string with binary "text").

can anyone tell me, is there any way to get the binary out from the string (using some library instead of manually parsing the string)????

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jyothsna1
Offline
Joined: 2008-03-26

HI Paul or somebody please,

I see MimeMultipart in the postmethod- submitcopy() in your reply above.

may I know which package has this MimeMultipart.

I see 3 packages in netbeans which has MimeMultipart. I dont know which one to use as this area is completely new to me.

(javax.mail.internet)
(com.​sun.​xml.​internal.​messaging.​saaj.​packaging.​mime.​internet)
com.​sun.​xml.​messaging.​saaj.​packaging.​mime.​internet

please throw some light.

thanks,

Jyothsna

mr_kschan
Offline
Joined: 2007-09-01

paul,

i have tried byte[] as the parameter and InputStream as parameter.

but, they both failed

@ConsumeMime( "multipart/form-data" )
@ProduceMime( "text/plain" )
@UriTemplate( "/submitCopy/" )
@HttpMethod( "POST" )
public Response submitCopy(
@HttpContext HttpHeaders request, InputStream content ) {

String r = "";

try {
byte[] bytes = new byte[ content.available() ];

content.read( bytes );

r = Arrays.toString( bytes );
} catch ( Exception ex ) {}

return Response.Builder.ok( r ).build();
}

the multipart delimitor is still in the input... how can i filter those thing?

sandoz
Offline
Joined: 2003-06-20

Ah! i see, you want to process the request entity as a multipart MIME data stream.

In that case you can use JavaMail and the MimeMultipart type as follows:

@ConsumeMime( "multipart/form-data" )
@ProduceMime( "text/plain" )
@UriTemplate( "/submitCopy/" )
@HttpMethod( "POST" )
public void submitCopy(MimeMultipart formData) {
// Void is being returned but you can return something else
// like a URI to a resource or just some content or
// do redirection to a resource
}

You need to make sure you have the mail.jar and activation.jar in your classpath.

Paul.

mr_kschan
Offline
Joined: 2007-09-01

paul,

i get a better solution with Jersey 0.3 =D

use apache fileupload and jersey 0.3 (the HttpServletRequest) works perfectly. ^^

excellent Jersey ^^

sandoz
Offline
Joined: 2003-06-20

Glad to hear it works!

Are you using the ServletRequest.getParameter functionality for processing content of the type "application/x-www-form-urlencoded" ? If so i think i misunderstood your original intent.

You can use the following Java type for such purposes:

com.sun.ws.rest.api.representation.FormURLEncodedProperties

See the SimpleConsole example in the distribution for further details (look at the Form.class)

Paul.

sandoz
Offline
Joined: 2003-06-20

I am not sure from your description what you exact problem is so i am guessing.

You can use byte[] or InputStream instead of String for HTTP methods, for example

@HttpMethod
void put(byte[] content) {
}

@HttpMethod
void put(InputStream content) {
}

Does that help?

If not can you send some example code and the version of Jersey you are using so i can better help you?

Paul.