jdk 1.6 或更低版本的 java.nio.file.Files 替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18747338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.nio.file.Files alternative for jdk 1.6 or less
提问by AdityaHandadi
I am trying to use java.nio.file.Files functionality for the following code
我正在尝试对以下代码使用 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;
}
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
但是由于 linux box jdk 版本较旧(Java 6),我不能使用它。在 Java 6 中有没有其他方法可以做类似的操作?提前致谢
采纳答案by Sotirios Delimanolis
Guava
provides a Files
class similar to java.nio.file.Files
. It has an overloaded copy()
method, such as
Guava
提供一个Files
类似于java.nio.file.Files
. 它有一个重载的copy()
方法,比如
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.
它不需要任何 Java 7 类,因此可以使用 Java 6 进行编译。