Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Servlets - download file (eg. upload from server)

servlet

  • Please log in to reply
1 reply to this topic

#1 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 26 September 2010 - 06:30 AM

I'm using the following code from the book "Head first Servlets & JSP"
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
   throws ServletException, IOException {
        resp.setContentType("application/pdf");
        ServletContext ctx = getServletContext();
        InputStream is = ctx.getResourceAsStream("/download.pdf");

        int read=0;
        byte[] bytes = new byte[1024];

        OutputStream os = resp.getOutputStream();
        while((read = is.read(bytes)) != -1){
            os.write(bytes, 0, read);
        }
    }
I've got 2 questions here:
  • Why 1024, 10 bits seems like an odd number
  • Why does my file download with a filename "download.do.pdf". How do I change this?

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 27 September 2010 - 04:07 AM

Okay for question 2. 'download.do' is the name of the .java file i'm accessing. And it appearantly just adds .pdf to it and sends that as filename -.-
  • 0





Also tagged with one or more of these keywords: servlet

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download