java.nio.file.Files alternative for jdk 1.6 or less

12,365

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.

Share:
12,365
AdityaHandadi
Author by

AdityaHandadi

Java Programmer(Web service)

Updated on June 16, 2022

Comments

  • AdityaHandadi
    AdityaHandadi almost 2 years

    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