从 API 接收时如何处理 blob 存储 url
How to handle blob store url when receive from API
我有一个 API URL(例如:localhost:8080/api/blobs/download/{item-id})。 API 本身将 return Blob 存储中该项目的 URL(例如,https://myaccount.blob.core.windows.net/mycontainer/myitem)。
现在我有一个任务,要求用户将 API URL 复制并粘贴到浏览器中,并期望它成为 view/download 项目。
问题是 Java 如何在没有前端帮助的情况下支持用户浏览 Blob Store URL?
您希望 java 应用程序从给定的 URL 下载文件吗?
不需要用户交互。
请参阅下面使用 Java NIO 的示例:
The Java NIO package offers the possibility to transfer bytes between
2 Channels without buffering them into the application memory.
To read the file from our URL, we'll create a new ReadableByteChannel
from the URL stream:
ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
The bytes read from the ReadableByteChannel will be transferred to a
FileChannel corresponding to the file that will be downloaded:
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME);
FileChannel fileChannel = fileOutputStream.getChannel();
We'll use the transferFrom() method from the ReadableByteChannel class
to download the bytes from the given URL to our FileChannel:
fileOutputStream.getChannel()
.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
The transferTo() and transferFrom() methods are more efficient than
simply reading from a stream using a buffer. Depending on the
underlying operating system, the data can be transferred directly from
the filesystem cache to our file without copying any bytes into the
application memory.
On Linux and UNIX systems, these methods use the zero-copy technique
that reduces the number of context switches between the kernel mode
and user mode.
您还可以使用其他库,这可能比单独使用 Java NIO 更好。
我有一个 API URL(例如:localhost:8080/api/blobs/download/{item-id})。 API 本身将 return Blob 存储中该项目的 URL(例如,https://myaccount.blob.core.windows.net/mycontainer/myitem)。 现在我有一个任务,要求用户将 API URL 复制并粘贴到浏览器中,并期望它成为 view/download 项目。
问题是 Java 如何在没有前端帮助的情况下支持用户浏览 Blob Store URL?
您希望 java 应用程序从给定的 URL 下载文件吗?
不需要用户交互。
请参阅下面使用 Java NIO 的示例:
The Java NIO package offers the possibility to transfer bytes between 2 Channels without buffering them into the application memory.
To read the file from our URL, we'll create a new ReadableByteChannel from the URL stream:
ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
The bytes read from the ReadableByteChannel will be transferred to a FileChannel corresponding to the file that will be downloaded:
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME);
FileChannel fileChannel = fileOutputStream.getChannel();
We'll use the transferFrom() method from the ReadableByteChannel class to download the bytes from the given URL to our FileChannel:
fileOutputStream.getChannel()
.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
The transferTo() and transferFrom() methods are more efficient than simply reading from a stream using a buffer. Depending on the underlying operating system, the data can be transferred directly from the filesystem cache to our file without copying any bytes into the application memory.
On Linux and UNIX systems, these methods use the zero-copy technique that reduces the number of context switches between the kernel mode and user mode.
您还可以使用其他库,这可能比单独使用 Java NIO 更好。