通过套接字发送文件 [英] Sending files through sockets

查看:109
本文介绍了通过套接字发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图在java中使用客户端 - 服务器类发送文件。出于某种原因,当调用发送文件的方法时,套接字关闭。这是代码:

Hello there im trying to send files using client-server classes in java. For some reason when the method that sends the file is called the socket closes. here is the code :

FileInputStream fIn = new FileInputStream(file);
out = new BufferedOutputStream(clientSocket.getOutputStream());
byte fileContent[] = new byte[(int) file.length()];
fIn.read(fileContent);
for (byte b : fileContent) {
    out.write(b);
}

以及来自客户的代码:

FileOutputStream fIn = new FileOutputStream("testing");
BufferedInputStream inAout = new BufferedInputStream(clientSocket.getInputStream());
byte fileContent[] = new byte[1000000];
inAout.read(fileContent);
fIn.write(fileContent);

并收到错误消息:SEVERE:null
java.net.SocketException:Socket关闭

and the error message i get : SEVERE: null java.net.SocketException: Socket closed

我真的没有这方面的经验,所以如果有任何可以帮助它会很棒。

Im not really experienced with this so if any can help it would be great.

推荐答案

InputStream.read(byte []) 方法返回 int 它实际读取的字节数。不能保证读取字节数组中请求的字节数。它通常会返回底层缓冲区的大小,你必须多次调用它。

The InputStream.read(byte[]) method returns an int for the number of bytes it actually read. It's not guaranteed to read as many bytes as you requested from the byte array. It'll often return the size of the underlying buffer and you'll have to call it many times.

你可以通过流式传输字节来提高效率文件的套接字而不是缓冲内存中的整个字节数组。同样在服务器端,你可以做同样的事情来节省内存,并且比一次写一个字节更快。

You can use this to be more efficient by streaming the bytes from the socket to the file instead of buffering the whole byte array in memory. Likewise on the server side you can do the same thing to save memory and be faster than writing a byte at a time.

这是一个服务器和客户端的工作示例一个连接到自身以传输文件:

Here's a working example of a server and client in one that connects to itself to transfer a file:

public class SocketFileExample {
    static void server() throws IOException {
        ServerSocket ss = new ServerSocket(3434);
        Socket socket = ss.accept();
        InputStream in = new FileInputStream("send.jpg");
        OutputStream out = socket.getOutputStream();
        copy(in, out);
        out.close();
        in.close();
    }

    static void client() throws IOException {
        Socket socket = new Socket("localhost", 3434);
        InputStream in = socket.getInputStream();
        OutputStream out = new FileOutputStream("recv.jpg");
        copy(in, out);
        out.close();
        in.close();
    }

    static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[8192];
        int len = 0;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
    }

    public static void main(String[] args) throws IOException {
        new Thread() {
            public void run() {
                try {
                    server();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        client();
    }
}

这篇关于通过套接字发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆