Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java:如何直接下载文件?_Java_File_Download - Fatal编程技术网

java:如何直接下载文件?

java:如何直接下载文件?,java,file,download,Java,File,Download,通常我使用以下代码下载文件: 服务器: FileInputStream fin = new FileInputStream(file); long length = file.length(); out.writeLong(length); out.flush(); while (length > 0) { out.writeByte(fin.read()); o

通常我使用以下代码下载文件:

服务器:

        FileInputStream fin = new FileInputStream(file);
        long length = file.length();
        out.writeLong(length);
        out.flush();
        while (length > 0) {
            out.writeByte(fin.read());
            out.flush();
            length--;
        }
客户:

       FileOutputStream fout = new FileOutputStream(downloaded);
       long length = in.readLong();
       while (length > 0) {
            Byte next = (Byte) in.readByte();
            fout.write(next);
            length--;
       }
它可以工作,但我想知道是否有直接的方法从服务器下载文件

编辑: 现在我试着用这个cose,但是我得到了一个ConnectionException

服务器端:

        URI uri = file.toURI();
        uri = setHost(uri, getMyIPAddress());
        System.out.println("URI: " + uri);
        out.writeObject(uri);
客户端:

        File downloaded = new File("downloaded");
        downloaded.createNewFile();
        URI uri = (URI) in.readObject();
        URL url = uri.toURL();
        Files.copy(url.openConnection().getInputStream(),downloaded.toPath(),StandardCopyOption.REPLACE_EXISTING);

可以这样做吗?

您在服务器-客户机通信中使用的是文件,用于您需要的直接访问。您可以始终为此任务包含第三方库,例如。希望我能帮你一点忙。

你说的直接是什么意思?你没有直接的是什么意思?我的意思是不要让服务器读取文件。也许可以将文件的URI发送到客户端?我想澄清一下上下文:我们在机器a上有一个文件,机器B想要下载它。有一种方法可以防止机器a读取要发送给B的文件?我正试图使用文件的URI下载它,但似乎无法从远程访问;也许NAT会阻止访问?我使用的URI如下所示:file://MYIP/C:/Users/user/Desktop/test.jpg . 可以这样做吗?我使用的流已经来自套接字。。。我在想,是否可以直接使用URI访问服务器上的文件,而不使用FileInputStream来读取它。