Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

InputStream to Character Array Char[] in servlets in Java  RSS feed

 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a Rest web service call and get InputStream.

Now i want to Write Input Stream to PrintWriter of servlet.

So that it can be downloaded.

I am able to write String and file can be downloaded using following code, i want it to work for Input stream

Following is code:

response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=\"" + name + ".pdf\"");
response.getWriter().write(is);

Write can take following:
write(String)
write(char[])
write(int)
write(String, int len, int off)
write(char[], int len, int off)

I think char[] will not harm PDF file which is going to download in it
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think char[] will not harm PDF file which is going to download in it


Very very very wrong. A PDF file must be treated as a stream of bytes.

For best results, you must include a Content-length header so the client will know how many bytes to expect. See the ServletResponse method setContentLength() to do this.

Bill
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have resolved the issue using following:




______

William: Content length varies and for showing content length of inputstream i have to make temporary file(?). Then filename.length will work?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!