我正在尝试对以下代码使用 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
提供Files
类似于 java.nio.file.Files 的类。它有一个重载的copy()
方法,如
public static void copy(File from, OutputStream to) throws IOException
那个
Copies all bytes from a file to an output stream.
它不需要任何 Java 7 类,因此可以使用 Java 6 进行编译。
关于jdk 1.6 或更低版本的 java.nio.file.Files 替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18747338/