简体   繁体   English

通过Java套接字发送文件

[英]Sending a file over java socket

I have been searching Google for hours trying to figure how to successfully send any file over a java socket. 我一直在搜索Google数小时,试图弄清楚如何通过Java套接字成功发送任何文件。 I have found many things online but none of them seem to work for me, I finally came across Object Output/Input streams to send files between the server and client, it is so far the only thing I have found that has worked, however it only seems to work over a connection to the localhost. 我在网上发现了很多东西,但似乎对我都不起作用,我终于遇到了对象输出/输入流,以便在服务器和客户端之间发送文件,这是迄今为止我发现的唯一可行的方法,但是似乎只能通过与本地主机的连接来工作。 For instance if I connect to the server from a friend's computer on a different network and send a file it fails, socket closes saying "read timeout" and then shortly after reconnects, file never gets sent. 例如,如果我从另一个网络上的朋友的计算机连接到服务器并发送文件,则文件失败,套接字关闭并显示“读取超时”,然后在重新连接后不久,文件就再也不会发送。 If I connect to localhost on the server and send the file it works perfectly. 如果我连接到服务器上的localhost并发送文件,则可以正常运行。 So whats the problem and how might I fix it? 那么问题是什么,我该如何解决?

Edit: below is the updated code.. moves extremely slow 编辑:下面是更新的代码。

public synchronized File readFile() throws Exception {
    String fileName = readLine();
    long length = dis.readLong();
    File temp = File.createTempFile("TEMP", fileName);
    byte[] buffer= new byte[1000];
    int count=0;
    FileOutputStream out = new FileOutputStream(temp);
    long total = 0;
    while (total < length && (count = dis.read(buffer, 0, (int)Math.min(length-total, buffer.length))) > 0)
    {
        System.out.println(total+" "+length+" "+temp.length()); //Just trying to keep track of where its at...
        out.write(buffer, 0, count);
        total += count;
    }
    out.close();
    return temp;
}


public synchronized void writeFile(File file) throws Exception {
    writeString(file.getName());
    long length = file.length();
    dos.writeLong(length);
    byte[] buffer= new byte[1000];
    int count=0;
    FileInputStream in = new FileInputStream(file);
    long total = 0;
    while (total < length && (count = dis.read(buffer, 0, (int)Math.min(length-total, buffer.length))) > 0)
    {
        System.out.println(total+" "+length); //Just trying to keep track of where its at...
        dos.write(buffer, 0, count);
        total += count;
    }
    in.close();
}

Both client and server have writeFile and readFile. 客户端和服务器都具有writeFile和readFile。 At the moment I am using this to send and display images. 目前,我正在使用它来发送和显示图像。

Don't use object streams for this. 不要为此使用对象流。 Use DataInputStream and DataOutputStream: 使用DataInputStreamDataOutputStream:

  1. Send and receive the filename, with writeUTF() and readUTF() . 使用writeUTF()readUTF()发送和接收文件名。
  2. Send and receive the data, as follows: 发送和接收数据,如下所示:

     while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } 

    You can use this code at both ends. 您可以在两端使用此代码。 buffer can be any size greater than zero. buffer大小可以大于零。 It doesn't need to be the size of the file, and that doesn't scale anyway to large files. 它不必是文件的大小,并且也不会缩放到大文件。

  3. Close the socket. 关闭插座。

If you need to send multiple files, or have some other need to keep the socket open: 如果您需要发送多个文件,或者需要保持套接字打开状态:

  1. Send the length ahead of the file, with writeLong() , and read it with readLong() and amending the loop, as follows: 使用writeLong()发送文件前面的长度,并使用readLong()读取它并修改循环,如下所示:

     long length = in.readLong(); long total = 0; while (total < length && (count = in.read(buffer, 0, length-total > buffer.length ? buffer.length : (int)(length-total))) > 0) { out.write(buffer, 0, count); total += count; } 

    and don't close the socket. 并且不要关闭插座。 Note that you have to test total before reading, and that you have to adjust the read length parameter so as not to overrun the end of the file. 请注意,您必须读取测试total ,并且必须调整读取长度参数,以免超出文件末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM