0
在AngularJS一个原始文件我有以下要求, Java服务器将通过读取服务器上的文件发送以下信息(也可以是PDF,DOC或图像)下载字节数组作为从服务器
的Java代码:
public static byte[] downloadFileAsStream(String fileName) throws IOException, MalformedURLException
File file = new File(fileName);
if (!file.exists()) {
// File not found
return null;
}
try {
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
return readFileAsByte(file.getAbsolutePath());
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
return null;
}
private static byte[] readFileAsByte(String filePath) {
try {
Path path = Paths.get(filePath);
return Files.readAllBytes(path);
} catch (IOException ex) {
ex.printStackTrace();
}
return new byte[0];
}
服务器将返回该文件作为字节数组,那么这需要被转换为原始文件AngularJS和下载