gpt4 book ai didi

jdk 1.6 或更低版本的 java.nio.file.Files 替代品

转载 作者:行者123 更新时间:2023-11-30 09:18:00 25 4
gpt4 key购买 nike

我正在尝试为以下代码使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com