-1

I am receiving the FileOutputStream as String in my servlet, How can I write it to file.

I tried to convert the string to bytes and than assigned it to new FileOutputStream, it generates the file but says the format is corrupted.

I tried various codes but none of them is working.

File file = new File("d:/file1.pdf");
        String content = request.getParameter("TextB1");

        try (FileOutputStream fop = new FileOutputStream(file)) {

            // get the content in bytes
            byte[] contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
IT ppl
  • 2,626
  • 1
  • 39
  • 56
  • What's passed into `TextB1`? Is it actually a PDF file? – shmosel May 18 '17 at 20:33
  • actually I am getting FileOutputStream from third party applet in javascript while doing some document varification using that applet,then I save it to hidden field and submit the form. Now in my servlet I am getting that as a string. and I need to save that as file (in pdf format). – IT ppl May 18 '17 at 20:46
  • my question is not about how to create pdf file, I am getting the fileoutputstream in string and I need to write it to file. – IT ppl May 18 '17 at 20:57
  • To a PDF file... right? – shmosel May 18 '17 at 20:58
  • the fileoutputstream itself is a pdf file, I am able to write it also but the file gets corrupted. – IT ppl May 18 '17 at 20:59
  • It's not "corrupted", it's just not PDF format. You're saving it as plain text. Open it in a text editor and it should display fine. – shmosel May 18 '17 at 21:05
  • it shows the string of fileoutputstream if I open it in text editor – IT ppl May 18 '17 at 21:10
  • Ok... so what's the problem? – shmosel May 18 '17 at 21:10
  • it shows like JVBERi0xLjQNCjUgMCBvYmoNCjw8DQovVHlwZSAvWE9iamVjdA0KL1N1YnR5cGUgL0ltYWdlDQovRmlsdGVyIC9GbGF0ZURlY29kZQ0KL0xlbmd0aCAzMDc5OQ0KL1dpZHRoIDIwMw0KL0hlaWdodCAyMDYNCi9CaXRzUGVyQ29tcG9uZW50IDgN....... actually its not a text content... – IT ppl May 18 '17 at 21:13
  • That's base64 encoded. And it looks like it may indeed be a PDF file. Try `byte[] contentInBytes = Base64.getDecoder().decode(content);`. I've reopened the question. – shmosel May 18 '17 at 21:17
  • let me elaborate for better understanding, I am using third party applet for docuement verification, which takes pdf as input and returns the FileOutputStream of verified document (which is in pdf format), since this is returned in applet, but I need to store that as a file on my server. Hence I am saving that bytes in one hidden field and pass that to another jsp page where I need to store that to a file. – IT ppl May 18 '17 at 21:17
  • thanks a lot, I'll try as suggested. – IT ppl May 18 '17 at 21:18
  • Thanks for your insight, indeed it worked like a charm :) Many thanks – IT ppl May 18 '17 at 21:31
  • Glad I could help! – shmosel May 18 '17 at 21:31

1 Answers1

0

Probably the issue is due the fact that you should need to decode the content if is a PDF file.

something like this should probably work:

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(content.getBytes());
Denis Ismailovski
  • 311
  • 1
  • 7
  • 15
  • You shouldn't use `sun.misc`. They're internal APIs that may be removed at any time (I think they're expected to in Java 9). – shmosel May 18 '17 at 21:46