*
The moose likes Servlets and the fly likes Sending file with Servlet, Problem if filename contains whitespaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sending file with Servlet, Problem if filename contains whitespaces" Watch "Sending file with Servlet, Problem if filename contains whitespaces" New topic
Author

Sending file with Servlet, Problem if filename contains whitespaces

Mark Mescher
Ranch Hand

Joined: Oct 25, 2004
Posts: 34
Hello,
I'm sending files by a Servlet. I'm setting the filename by using

res.setHeader("Content-Disposition", "attachment; filename=" + filename);

This works fine. But if the filename contains whitespaces, my Firefox cuts the filename before the whitespace. So all chars behind the whitespace (including the extension!) get lost. Are there any solutions for this?

Thx
Mark
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you try with double-quotations too ?


[My Blog]
All roads lead to JavaRanch
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What I would try is this:
  • Create an empty directory on your webserver (call it junk).
  • Put a file with spaces in the name ("space in name.html")
  • Point your browser at that directory.
  • Click on the link for that file.
  • Using the LiveHTTPHeaders plugin for FireFox, view the filename as it's being sent.

  • This will be the default way of packaging files with spaces in the name over HTTP.

    I did this and saw:

    This shows me that servers handle spaces in th names by URL Encoding them and that browsers expect this and already know how to deal with it.
    [ January 30, 2006: Message edited by: Ben Souther ]

    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Michael Remijan
    Author
    Ranch Hand

    Joined: May 29, 2002
    Posts: 123
        
        5



    Java EE Evangelist — Author, EJB 3 in Action 2nd Edition — Java Community Process Member
     
    Don't get me started about those stupid light bulbs.
     
    subject: Sending file with Servlet, Problem if filename contains whitespaces