简体   繁体   English

通过Java中的Socket进行文件传输

[英]File transfer through Socket in java

I'm making a Network File Transfer System for transfering any kind of file over a network in java. 我正在制作一个网络文件传输系统,用于在Java中通过网络传输任何类型的文件。 The size also could be of any kind. 大小也可以是任何种类。 Therefore I've used UTF-8 protocol for the task. 因此,我已将UTF-8协议用于该任务。

I'm providing the codes which I've made but the problem is some times the file gets transfered as it is, with no problem at all. 我提供的是我编写的代码,但是问题是文件有时按原样传输,完全没有问题。 But sometimes few kb's of data is just skipped at the receiving end, which actually restricts the mp3/video/image file to be opened correctly. 但是有时在接收端只是跳过了几kb的数据,实际上限制了mp3 / video / image文件的正确打开。 I think the problem is with BUFFER. 我认为问题出在BUFFER。 I'm not creating any buffer which, right now, I think may be of some use to me. 我没有创建任何缓冲区,目前,我认为这可能对我有用。

I would really appreciate if anyone could provide any help regarding the problem, so that the file gets transferred fully. 如果有人可以提供有关此问题的任何帮助,以使文件完全传输,我将不胜感激。

Client side : --->> File Sender 客户端:--- >>文件发送方

Socket clientsocket = new Socket(host,6789);                        // host contains the ip address of the remote server
DataOutputStream outtoserver = new DataOutputStream(clientsocket.getOutputStream());
try
{
    int r=0;
    FileInputStream fromFile1 = new FileInputStream(path);                  // "path" is the of the file being sent.
    while(r!=-1)
    {   
         r = fromFile1.read();
         outtoserver.writeUTF(r+"");
    }
}
catch(Exception e)
{
    System.out.println(e.toString());
}
clientsocket.close();

Server side: --->> File Receiver 服务器端:--- >>文件接收器

ServerSocket welcome = new ServerSocket(6789);
Socket conn = welcome.accept();
try
{
    String r1 = new String();
    int r=0;
    FileOutputStream toFile1 = new FileOutputStream(path);                  // "path" is the of the file being received.
    BufferedOutputStream toFile= new BufferedOutputStream(toFile1);
    DataInputStream recv = new DataInputStream(conn.getInputStream());
    while(r!=-1)
    {   
        r1 = recv.readUTF();
        r = Integer.parseInt(r1);
        toFile.write(r);
    }
}
catch(Exception e)
{
    System.out.println(e.toString());
}

I don't understand why you are encoding binary data as text. 我不明白您为什么将二进制数据编码为文本。

Plain sockets can send and receive streams of bytes without any problems. 普通套接字可以发送和接收字节流,而不会出现任何问题。 So, just read the file as bytes using a FileInputStream and write the bytes to the socket as-is. 因此,只需使用FileInputStream将文件读取为字节,然后将字节原样写入套接字即可。

(For the record, what you are doing is probably sending 3 to 5 bytes for each byte of the input file. And you are reading the input file one byte at a type without any buffering. These mistakes and others you have made are likely to have a significant impact on file transfer speed. The way to get performance is to simply read and write arrays of bytes using a buffer size of at least 1K bytes.) (记录下来,您可能正在为输入文件的每个字节发送3到5个字节。并且您正在以一种类型读取一个字节的输入文件而没有任何缓冲。这些错误以及您所犯的其他错误很可能会对文件传输速度有重大影响。获得性能的方法是使用至少1K字节的缓冲区大小简单地读取和写入字节数组。)


I'm not sure of this, but I suspect that the reason that you are losing some data is that you are not flushing or closing outtoserver before you close the socket on the sending end. 我不确定,但是我怀疑丢失某些数据的原因是,在关闭发送端的套接字之前,您没有刷新或关闭outtoserver

FOLLOW UP 跟进

I also noticed that you are not flushing / closing toFile on the receiver end, and that could result in you losing data at the end of the file. 我还注意到您没有在接收器端刷新/关闭toFile ,这可能会导致您在文件末尾丢失数据。

The first problem I see is that you're using DataInputStream and DataOutputStream . 我看到的第一个问题是您正在使用DataInputStreamDataOutputStream These are for reading/writing primitive Java types ( int , long etc), you don't need them for just binary data. 这些用于读取/写入原始Java类型( intlong等),而对于二进制数据则不需要它们。

Another problem is that you're not flushing your file output stream - this could be causing the lost bytes. 另一个问题是您没有刷新文件输出流-这可能会导致丢失字节。

显式刷新可能有助于解决问题。

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

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