我想在客户端浏览器<->我的服务器<->其他服务器之间创建一个管道来下载某些文件。我使用 Apache Tomcat 作为我的服务器。
如何通过我的服务器创建管道?我的服务器上没有太多空间,因此我不想在服务器上保存文件。
由于某些原因,我只想通过我的服务器下载数据。数据应该实时流动。
我可以使用 Java EE 中的流来执行此操作吗?
最佳答案
也许这就是你的意思?
免责声明:我没有尝试编译或运行任何内容
public void doGet(HttpServletRequest request, HttpServletResponse response) {
URL url = new URL("http://your-other-server/the/resource/you/want");
InputStream source = url.openStream();
OutputStream destination = response.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = source.read(buffer)) != -1) {
destination.write(buffer, 0, length);
}
source.close();
}
关于Java EE 输入/输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1306555/