Jump to content

Servlets - download file (eg. upload from server)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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?


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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 -.-