users@jersey.java.net

ProduceMime Problem

From: KSChan <mr.kschan_at_gmail.com>
Date: Sun, 6 Jan 2008 17:42:20 +0800

Hello~

In my developing application, I would like to allow user to upload binary
objects like jpeg, gif, png, doc, xls, pdf, etc...

I used Jersey together with Apache FileUpload to take the user upload by
following code.

    @ConsumeMime( "multipart/form-data" )
> @ProduceMime( "text/xml" )
> @UriTemplate( "/submitCopy/" )
> @HttpMethod( "POST" )
> public Response submitCopy() {
>
> // Create a factory for disk-based file items
> FileItemFactory factory = new DiskFileItemFactory();
>
> // Create a new file upload handler
> ServletFileUpload upload = new ServletFileUpload( factory );
>
> try {
> // Parse the request
> List items = upload.parseRequest( this._request );
> FileItem item = (FileItem) items.get(0);
>
> Blob blob = new SerialBlob( item.get() );
> String contentType = item.getContentType();
>
> /// SKIPPED: following portion is just simple SQL statements
> }
>

The mime content type (jpeg, gif, blah blah blah etc....) is stored into DB
...


The next thing is to allow my user to retrieve their stuff ...

I used the following code.

    @ConsumeMime( "application/x-www-form-urlencoded" )
> @ProduceMime( "application/*" )
> @UriTemplate( "/obtainCopy/uuid={uuid}" )
> @HttpMethod( "GET" )
> public Response obtainCopy( @UriParam( "uuid" ) String uuid ) {
>
> String sql = "SELECT copy, content_type " +
> "FROM soft_copy " +
> "WHERE uuid = '" + uuid + "'";
> Set< String > keys = new HashSet< String >();
> keys.add( "copy" );
> keys.add( "content_type" );
>
> try {
> List< HashMap< String, Object > > fetch_result =
> this._bridge.fetchForRecord( sql, keys ); // Obtain DB
> data
>
> HashMap< String, Object > item = fetch_result.get(0);
>
> Blob file = (Blob) item.get( "copy" );
> String contentType = (String) item.get( "content_type" );
>
> InputStream stream = file.getBinaryStream();
> byte[] bytes = new byte[ stream.available() ];
> stream.read( bytes );
>
> return Response.Builder
> .ok( bytes )
> .type( contentType )
> .build();
>
>

I get the blob and content type back from DB ... but, how can I
"dynamically" change the produce mime so that the user can "download" with
xxx.jpg or yyy.png or zzz.pdf instead of xxx or yyy or zzz without
extension ... ??

Thx for any advice ^^.

ks.

-- 
I, have a dream. I want to make my dream come true.