我正在尝试将java.nio.file.Files功能用于以下代码

@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;
}


但是由于linux box jdk版本较旧(Java 6),所以我不能使用它。在Java 6中是否可以进行类似的操作?提前致谢

最佳答案

Guava提供类似于Filesjava.nio.file.Files类。它具有重载的copy()方法,例如

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





  将所有字节从文件复制到输出流。


它不需要任何Java 7类,因此可以使用Java 6进行编译。

关于java - jdk 1.6或更低版本的java.nio.file.Files替代,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18747338/

10-09 07:35
查看更多