通过Java套接字发送文件 [英] Sending a file over java socket

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

问题描述

我一直在搜索Google数小时,试图弄清楚如何通过Java套接字成功发送任何文件.我在网上发现了很多东西,但似乎对我都不起作用,我终于遇到了对象输出/输入流,以便在服务器和客户端之间发送文件,这是迄今为止我发现的唯一可行的方法,但是似乎只能通过与本地主机的连接来工作.例如,如果我从另一个网络上的朋友的计算机连接到服务器并发送文件,则文件失败,套接字关闭并显示读取超时",然后在重新连接后不久,文件就再也不会发送.如果我连接到服务器上的localhost并发送文件,则可以正常运行.那么问题是什么,我该如何解决?

I have been searching Google for hours trying to figure how to successfully send any file over a java socket. 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. So whats the problem and how might I fix it?

下面是更新的代码..移动非常慢

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();
}

客户端和服务器都具有writeFile和readFile.目前,我正在使用它来发送和显示图像.

Both client and server have writeFile and readFile. At the moment I am using this to send and display images.

推荐答案

请勿为此使用对象流.使用 DataInputStream DataOutputStream:

Don't use object streams for this. Use DataInputStream and DataOutputStream:

  1. 使用 writeUTF() readUTF()发送和接收文件名.
  2. 发送和接收数据,如下所示:

  1. Send and receive the filename, with writeUTF() and readUTF().
  2. Send and receive the data, as follows:

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

您可以在两端使用此代码. buffer 的大小可以大于零.不需要是文件的大小,也不会缩放到大文件.

You can use this code at both ends. buffer can be any size greater than zero. It doesn't need to be the size of the file, and that doesn't scale anyway to large files.

关闭插座.

如果您需要发送多个文件,或者需要其他一些使套接字保持打开状态:

If you need to send multiple files, or have some other need to keep the socket open:

  1. 使用 writeLong()发送文件的长度,然后使用 readLong()读取并修改循环,如下所示:

  1. Send the length ahead of the file, with writeLong(), and read it with readLong() and amending the loop, as follows:

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;
}

并且不要关闭插座.请注意,您必须在读取前先测试 total ,并且必须调整读取长度参数,以免超出文件末尾.

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.

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

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