1

I am trying to generate a pdf in my application.

I have used the following in a previous project to copy the byte array to the outputstream.

FileCopyUtils.copy(fileContent, response.getOutputStream());

In my current project we are not using spring framework. So I cannot use FileCopyUtils here. Is there any other way to do the similar thing in simple basic java.

Philip John
  • 4,776
  • 9
  • 39
  • 64

3 Answers3

2

Class java.nio.file.Files contains several utility methods to copy files (new since Java 7).

For example:

Path path = Paths.get("C:\\Somewhere\\Somefile.txt");

Files.copy(path, response.getOutputStream());
Jesper
  • 195,030
  • 44
  • 313
  • 345
  • does this actually do the trick? I just need to copy the converted pdf byte array. It is not a file loaded from any location. – Philip John Aug 27 '15 at 10:13
2

To write a byte array to an OutputStream, call its write method:

response.getOutputStream().write(fileContent);

No helper methods required.

user253751
  • 50,383
  • 6
  • 45
  • 81
0

If you really must do it yourself (I would strongly recommend you do not) then something like this should work.

public static void copy(Reader in, Writer out) throws IOException {
  final char[] buffer = new char[4096];
  int read;
  do {
    read = in.read(buffer, 0, buffer.length);
    if (read > 0) {
      out.write(buffer, 0, read);
    }
  } while (read >= 0);
}
OldCurmudgeon
  • 62,806
  • 15
  • 115
  • 208