• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing a char array to a file

 
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a function called decode(sPDFImage);
Here is part of the function:
<%!public static char[] decode(String text) {

return decode(text.toCharArray());

}
%>

The sPDFImage is the actual pdf base64 which needs to be converted to binary.
Basically, I have the pdf displaying properly in the browser but now I need to write the PDF to a file. I have the code doing this, but it won't open because it is not properly decoded.
This call:
out.print(decode(sPDFImage));
out.flush(); //added flush to ensure buffer is flushed for subsquent requests
out.close();
This works when it opens in the browser but I need to write it to a file then open it.
Here is my code which I am using:
DataOutputStream output_stream;
output_stream = new DataOutputStream(new FileOutputStream("c:\\" + sSessionID + ".pdf"));
char[] ray = new char[decode(sPDFImage).length];
int x = 0;
ray = decode(sPDFImage); //put the return array into ray[]
for(x = 0; x < ray.length; ++x){
System.out.print(ray[x]);
output_stream.writeUTF(ray[x]);
}
output_stream.close();
I do get stuff in the file but it is garbage, I need what is displayed in the browser as a PDF.
My machine starts beeping when I run this code
Anyways is there another way I can get the char array written to a pdf file that I can open?
Thanks Ray
[ December 03, 2002: Message edited by: Ray Smilgius ]
[ December 03, 2002: Message edited by: Ray Smilgius ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why make things hard on yourself?
Writer.write(char[])
 
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Dave,
PrintWriter pw = new PrintWriter(new FileOutputStream("c:\\" + sSessionID + ".pdf"));
pw.write(decode(sPDFImage));
pw.close();
I used this but and I try to open it opens correctly but it is a blank pdf no contents but I know the contents are there becasue it works in the browser.
Thanks Ray
Any Suggestions?
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Writer out = new FileWriter(filename);" is a bit more direct.
As for for blank .pdf problem, one probable problem leaps to mind: Readers/Writers and the char datatype in general are not suitable for binary content. It might solve your problem to have decode() return a byte[] and call FileOutputStream.write(byte[]).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!