Jersey truncating the slashes from the uploaded file name

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Jersey truncating the slashes from the uploaded file name

ManiKanta G

Hi,

While uploading files from IE, Jersey resource is getting the file path without any slashes (as IE' behavior for file uploads is different from the other browsers in that it sends the complete file path of the user' system as  the file name). Is there any way  to stop this default behavior of Jersey of truncating the slashes from path. 

The code I m using is no different from the one the documentation suggest:

@Path("upload")
@POST
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") FormDataContentDisposition disposition , 
        @FormDataParam("file") FormDataBodyPart part) {
    String fileName = disposition.getFileName();

    // some code to save the file with the user uploaded file name
}


Or in general, is there any place I can hook some code in place or before the actual parameters were set in the resource class. I m actually interested to know where all the resource class annotation processing will be done.

Thanks,
ManiKanta G
twitter.com/ManiKantaG
Reply | Threaded
Open this post in threaded view
|

Re: Jersey truncating the slashes from the uploaded file name

Paul Sandoz-2
Hi,

I am not precisely sure what "file path" you are referring to, is it the value of the file name obtained from the content disposition?

Could you send an example e.g. from logging or from snooping on the HTTP port?

Paul.

On Feb 2, 2011, at 10:03 AM, ManiKanta G wrote:


Hi,

While uploading files from IE, Jersey resource is getting the file path without any slashes (as IE' behavior for file uploads is different from the other browsers in that it sends the complete file path of the user' system as  the file name). Is there any way  to stop this default behavior of Jersey of truncating the slashes from path. 

The code I m using is no different from the one the documentation suggest:

@Path("upload")
@POST
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") FormDataContentDisposition disposition , 
        @FormDataParam("file") FormDataBodyPart part) {
    String fileName = disposition.getFileName();

    // some code to save the file with the user uploaded file name
}


Or in general, is there any place I can hook some code in place or before the actual parameters were set in the resource class. I m actually interested to know where all the resource class annotation processing will be done.

Thanks,
ManiKanta G
twitter.com/ManiKantaG

Reply | Threaded
Open this post in threaded view
|

Re: Jersey truncating the slashes from the uploaded file name

ManiKanta G

Hi,

Below is the Fiddler's log for the upload request: 

Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)
Content-Type: multipart/form-data; boundary=---------------------------7db3c8365097e
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-Length: 522
Host: localhost:9998
Pragma: no-cache

-----------------------------7db3c8365097e
Content-Disposition: form-data; name="file"; filename="C:\Documents and Settings\User1\Desktop\TODO.txt"
Content-Type: text/plain

<File content goes here...>
-----------------------------7db3c8365097e--

But printing the disposition.getFileName() in resource is giving: 

C:Documents and SettingsUser1DesktopTODO.txt

I've tested in both IE8 & IE7 and the same behavior is observed.

Thanks,
ManiKanta G
twitter.com/ManiKantaG


On Wed, Feb 2, 2011 at 2:50 PM, Paul Sandoz <[hidden email]> wrote:
Hi,

I am not precisely sure what "file path" you are referring to, is it the value of the file name obtained from the content disposition?

Could you send an example e.g. from logging or from snooping on the HTTP port?

Paul.

On Feb 2, 2011, at 10:03 AM, ManiKanta G wrote:


Hi,

While uploading files from IE, Jersey resource is getting the file path without any slashes (as IE' behavior for file uploads is different from the other browsers in that it sends the complete file path of the user' system as  the file name). Is there any way  to stop this default behavior of Jersey of truncating the slashes from path. 

The code I m using is no different from the one the documentation suggest:

@Path("upload")
@POST
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") FormDataContentDisposition disposition , 
        @FormDataParam("file") FormDataBodyPart part) {
    String fileName = disposition.getFileName();

    // some code to save the file with the user uploaded file name
}


Or in general, is there any place I can hook some code in place or before the actual parameters were set in the resource class. I m actually interested to know where all the resource class annotation processing will be done.

Thanks,
ManiKanta G
twitter.com/ManiKantaG


Reply | Threaded
Open this post in threaded view
|

Re: Jersey truncating the slashes from the uploaded file name

Paul Sandoz-2

On Feb 3, 2011, at 12:51 PM, ManiKanta G wrote:


Hi,

Below is the Fiddler's log for the upload request: 

Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)
Content-Type: multipart/form-data; boundary=---------------------------7db3c8365097e
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-Length: 522
Host: localhost:9998
Pragma: no-cache

-----------------------------7db3c8365097e
Content-Disposition: form-data; name="file"; filename="C:\Documents and Settings\User1\Desktop\TODO.txt"
Content-Type: text/plain

<File content goes here...>
-----------------------------7db3c8365097e--

But printing the disposition.getFileName() in resource is giving: 

C:Documents and SettingsUser1DesktopTODO.txt

I've tested in both IE8 & IE7 and the same behavior is observed.


Thanks, that is much clearer.

I am reusing the HTTP header parsing code to parse content-disposition headers. The '\' character has special meaning:


      A string of text is parsed as a single word if it is quoted using double-quote marks.

    quoted-string  = ( <"> *(qdtext | quoted-pair ) <"> )
    qdtext         = <any TEXT except <">>

The backslash character ("\") MAY be used as a single-character quoting mechanism only within quoted-string and comment constructs.

    quoted-pair    = "\" CHAR

and that meaning is the same for rfc822:


3.4.4. DELIMITING AND QUOTING CHARACTERS
        The quote character (backslash) and  characters  that  delimit
        syntactic  units  are not, generally, to be taken as data that
        are part of the delimited or quoted unit(s).   In  particular,
        the   quotation-marks   that   define   a  quoted-string,  the
        parentheses that define  a  comment  and  the  backslash  that
        quotes  a  following  character  are  NOT  part of the quoted-
        string, comment or quoted character.  A quotation-mark that is
        to  be  part  of  a quoted-string, a parenthesis that is to be
        part of a comment and a backslash that is to be part of either
        must  each be preceded by the quote-character backslash ("\").
        Note that the syntax allows any character to be quoted  within
        a  quoted-string  or  comment; however only certain characters
        MUST be quoted to be included as data.  These  characters  are
        the  ones that are not part of the alternate text group (i.e.,
        ctext or qtext).

This means IE7 and IE8 are not conforming to the encoding rules of rfc822. The encoding of the content-disposition header should look like this:

  Content-Disposition: form-data; name="file"; filename="C:\\Documents and Settings\\User1\\Desktop\\TODO.txt"

Paul.

Reply | Threaded
Open this post in threaded view
|

Re: Jersey truncating the slashes from the uploaded file name

captainbob
I have exactly the same problem.
On IE, FormDataContentDisposition.getFileName returns the file full path with no separators, thus making it impossible to get the real filename.

It's a huge issue since it prevents jersey multipart to be used by IE users, which means a LOT of people ...
I understand its IE's fault, but is there any workaround? Or am I missing something?

Thx.
Reply | Threaded
Open this post in threaded view
|

Re: Jersey truncating the slashes from the uploaded file name

Pavel Bucek-2
Hello,

which Jersey version are you using? I think this has been already fixed
some time ago - can you retry with Jersey 1.6?

Pavel

On 4/19/11 5:05 PM, captainbob wrote:

> I have exactly the same problem.
> On IE, FormDataContentDisposition.getFileName returns the file full path
> with no separators, thus making it impossible to get the real filename.
>
> It's a huge issue since it prevents jersey multipart to be used by IE users,
> which means a LOT of people ...
> I understand its IE's fault, but is there any workaround? Or am I missing
> something?
>
> Thx.
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Jersey-truncating-the-slashes-from-the-uploaded-file-name-tp5984041p6287606.html
> Sent from the Jersey mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: Jersey truncating the slashes from the uploaded file name

captainbob
Hi,
 our project was running with Jersey multipart 1.3, I tried to upgrade to
1.6 or 1.7 but we still got the same problem.


--
View this message in context: http://jersey.576304.n2.nabble.com/Jersey-truncating-the-slashes-from-the-uploaded-file-name-tp5984041p6290729.html
Sent from the Jersey mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: Jersey truncating the slashes from the uploaded file name

jkoett
This post has NOT been accepted by the mailing list yet.
In reply to this post by ManiKanta G
A good solution to this issue is posted here:
http://stackoverflow.com/questions/19164001/how-to-get-only-the-filename-with-jersey-file-upload

For those interested, the solution posted listens to changes on the file selector, then parses the filename using javascript and sends it along with the file post in a hidden input.