使用Java Nio进行远程文件传输

时间:2019-04-21 10:56:20

标签: java nio zero-copy

我正在尝试使用JAVA NIO将文件从主机A传输到客户端B,而不必在本地下载文件,然后为客户端B提供下载文件的链接。

我正在运行一个Spark Apache框架并使用maven项目。

我使用以下命令在Spark中映射了请求http://localhost:8080/download/hello: get(“ / download /:id”,RequestHandler :: downloadHandler);

该函数的内部是从以下位置下载文件的代码: “ https://download.springsource.com/release/STS/3.8.1.RELEASE/dist/e4.6/spring-tool-suite-3.8.1.RELEASE-e4.6-linux-gtk-x86_64.tar.gz

try {
            URL url = new URL("https://download.springsource.com/release/STS/3.8.1.RELEASE/dist/e4.6/spring-tool-suite-3.8.1.RELEASE-e4.6-linux-gtk-x86_64.tar.gz");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            int respCode = +httpURLConnection.getResponseCode();
            System.out.println("response code : "+respCode);
            if (respCode == HttpURLConnection.HTTP_OK){
                String fileName = "";
                String disposition = httpURLConnection.getHeaderField("Content-Disposition");
                String contentType = httpURLConnection.getContentType();
                int contentLength = httpURLConnection.getContentLength();

                if (disposition != null) {
                    // extracts file name from header field
                    int index = disposition.indexOf("filename=");
                    if (index > 0) {
                        fileName = disposition.substring(index + 10,
                                disposition.length() - 1);
                    }
                } else {
                    // extracts file name from URL
                    fileName = url.toString().substring(url.toString().lastIndexOf("/") + 1,
                            url.toString().length());
                }
                System.out.println("Content-Type = " + contentType);
                System.out.println("Content-Disposition = " + disposition);
                System.out.println("Content-Length = " + contentLength);
                System.out.println("fileName = " + fileName);
                httpURLConnection.disconnect();
                System.out.println("other stuff : ");
                System.out.println(url.getHost());
                ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
                FileOutputStream fileOutputStream = new FileOutputStream(fileName);
                FileChannel fileChannel = fileOutputStream.getChannel();
                fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
                fileOutputStream.close();
                readableByteChannel.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

我使用httpURLConnection获取文件名和文件大小,然后进行处理以下载文件。我想做的是,而不是使用fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE)在本地下载文件,而是直接将文件传输到客户端。

我做了一些研究,我认为使用Socketchannels是可能的,但我不了解它应该如何工作。

我也阅读了这篇文章 https://examples.javacodegeeks.com/core-java/nio/java-nio-large-file-transfer-tutorial/ 并试图理解“接收者”课程,但我仍不清楚如何。

我希望您能提供一些指导。谢谢

0 个答案:

没有答案