2

I am trying to use java.nio.file.Files functionality for the following code

@RequestMapping(value = "/view", method = RequestMethod.GET)
public HttpServletResponse viewReport(Map model, HttpServletRequest req,HttpServletResponse rep){

     try {

         ReportGenerationService reportGenerationService = new ReportGenerationService();
         ViewReportParameters viewReportParameters = reportGenerationService.getReportViewParameters();


         String quote="\"";
         String inlineFileName = "inline; fileName="+quote+viewReportParameters.getFileName()+".pdf"+quote;

         File file = new File(filePath);

         rep.setHeader("Content-Type", "application/pdf");
         rep.setHeader("Content-Length", String.valueOf(file.length()));
         rep.setHeader("Content-Disposition", inlineFileName);


            Files.copy(file.toPath(), rep.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    return rep;
}

But since the linux box jdk version is older(Java 6) , I cannot be using this. Is there any alternative to do similar operation in Java 6? Thanks in advance

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
AdityaHandadi
  • 149
  • 1
  • 2
  • 13
  • You might check out [FileChannel](http://docs.oracle.com/javase/6/docs/api/java/nio/channels/FileChannel.html) and [this question](http://stackoverflow.com/questions/2520305/java-io-to-copy-one-file-to-another). – sgbj Sep 11 '13 at 17:30

1 Answers1

2

Guava provides a Files class similar to java.nio.file.Files. It has an overloaded copy() method, such as

public static void copy(File from,  OutputStream to) throws IOException

that

Copies all bytes from a file to an output stream.

It doesn't need any Java 7 classes and so compiles with Java 6.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724