Spring boot 如何使用SpringBoot Rest API Apache CXF实现下载大型zip文件(1-2GB)?
如何使用SpringBoot Rest API Apache CXF实现下载大型zip文件(1-2GB) 我尝试过使用输出流,但没有成功Spring boot 如何使用SpringBoot Rest API Apache CXF实现下载大型zip文件(1-2GB)?,spring-boot,Spring Boot,如何使用SpringBoot Rest API Apache CXF实现下载大型zip文件(1-2GB) 我尝试过使用输出流,但没有成功 InputStream inputStream = new FileInputStream(new File(file)); return new StreamingResponseBody() { @Override public void writeTo(OutputStream
InputStream inputStream = new FileInputStream(new File(file));
return new StreamingResponseBody() {
@Override
public void writeTo(OutputStream outputStream)
throws IOException {
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
System.out.println("Writing some bytes..");
outputStream.write(data, 0, nRead);
}
inputStream.close();
}
};
我尝试了下面的代码,它可以下载500MB的文件,但它也可以用于1-2GB的文件:
try {
String link = "https://s.basketbuild.com/uploads/devs/dianlujitao/oneplus3/cm13/cm-13.0-20160621-UNOFFICIAL-oneplus3.zip";
URL url = new URL(link);
System.out.println("Started reading the zip");
File dir = new File("C:\\Softwares\\testdownload");
if (!dir.exists())
dir.mkdirs();
String fileBaseName = "TestDownload";
String fileExtension = "zip";
System.out.println("Name: " + fileBaseName + '.' + fileExtension);
File outputFile = new File(dir, fileBaseName + '.' + fileExtension);
if (!outputFile.exists()) {
outputFile.createNewFile();
}
InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream byteArrayOutputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
int n = 0;
byte[] buf = new byte[1024];
long duration = System.currentTimeMillis();
while ((n = inputStream.read(buf)) != -1) {
byteArrayOutputStream.write(buf, 0, n);
}
duration = System.currentTimeMillis() - duration;
System.out.println("Finish in " + duration + "ms");
inputStream.close();
// release outputstream
byteArrayOutputStream.close();
System.out.println("Your download has been finished");
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("Something unexpected has happened!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Something unexpected has happened!");
}
在您的示例中,您使用了URL,但我尝试从服务器目录或文件系统中读取。如何根据需要进行修改?