#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    199
    Rep Power
    11

    Java, servlets, and downloading files from database question.


    So

    I have this servlet that connects to my database, loads a blob into a byte array and then i want to create a download link with the byte array....


    first i created a file with the data and then create a download link with that created file. but that is a sub-optimal idea because my folder gets full with unnecesary files for years and years.

    i have heard there is a way to pass the byte array to a buffer in html so that the data of the file is sent to the html buffer and downloaded at the moment instead of having to create a file with the data. so... is there a way to do this??

    any help is completely appreciated, thanks!!
  2. #2
  3. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5053
    Set the mime type of the response (setContentType), get the servlet's output stream and write the data directly to the response.

    Example: Returning an Image in a Servlet

    Hope this helps!

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    199
    Rep Power
    11
    thanks a lot that is what i was looking for, it worked but...

    how do i set the name of the file i am downloading, because when i send the bytes i can't send the name of the file the user is about to download. so... any help there?
  6. #4
  7. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5053
    any help there?
    You can suggest a filename by setting the Content-Disposition header with "attachment; filename=whatever".

    ftp://ftp.rfc-editor.org/in-notes/rfc2183.txt

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    199
    Rep Power
    11
    Originally Posted by Yawmark
    You can suggest a filename by setting the Content-Disposition header with "attachment; filename=whatever".

    ftp://ftp.rfc-editor.org/in-notes/rfc2183.txt

    ~

    thanks a lot but i am completely new to this, could you please be a bit more specific?

    what would be like a 2 line java code example to set the filename?

    thanks!!
  10. #6
  11. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5053
    what would be like a 2 line java code example to set the filename?
    Code:
    response.setHeader("Content-Disposition", "attachment; filename=" + filename);
    New or not, you should brush up on your research skills. It's bad form to ask for solutions without at least trying something first, or at the very least, explaining why you weren't able to find any resources. A quick Google for "java set content disposition" (keywords from my previous post) would have given you numerous examples.

    ~
    Last edited by Yawmark; July 13th, 2006 at 06:28 PM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

IMN logo majestic logo threadwatch logo seochat tools logo