开发者

How do I correctly set the HTTP Content-Disposition for large file names in Java?

I'm working on some 开发者_Go百科requirements that will lead to arbitrary PDF files being downloaded from a J2EE web server. The names may look like this:

Xxxxxxxxxxxxxxxxxx - Yyyyyyyyyy - Aaaaaaaaaaa - Bbbbbbbb ccc Dddddddddddddd - abc1234560 - 2009-03-26 – 235959.pdf

Now I've read a couple of sections in RFC2183:

http://www.ietf.org/rfc/rfc2183.txt

For instance

A short (length <= 78 characters) parameter value containing only non-tspecials' characters SHOULD be represented as a single token'. A short parameter value containing only ASCII characters, but including tspecials' characters, SHOULD be represented as quoted-string'. Parameter values longer than 78 characters, or which contain non-ASCII characters, MUST be encoded as specified in [RFC 2184].

Etc etc. Now there are millions of things that can go wrong, if I don't read through all of those RFC's... Or I choose a library which handles such RFC specifications. Is there any such thing for Java? Or am I paranoid, and actually it's sufficient to just write this header to the out stream:

String filename = "\"" + filename.replace("\"", "\\\"") + "\"";
addHeader("Content-Disposition", "attachment; filename=" + filename);


I had similar problem in past and found the following solution.

The first URL looks like http://myhost.com/file/1234 where 1234 is the file ID. Let's say that the file name should be my-very-long-file-name.pdf. So, instead of setting HTTP header redirect the call to URL like

http://myhost.com/download/1234/my-very-long-file-name.pdf

The sevlet mapped to /download/ will take ID from URL and print the file to its output stream. But browser will extract the file name from URL and offer you to download and save the file because its name is into the URL. I hope this will work for you also for long file names.


RFC 2183 isn't relevant, RFC 6266 is.

Also, the 78 character limit only applies to email, not http, so you don't have to worry about that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜