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和下載