首次成功后,文件传输功能将冻结

时间:2018-07-06 15:22:04

标签: java file-transfer

我试图编写一个程序,该程序只是将文件从客户端发送到服务器。该程序有一个gui,您可以在其中输入文件路径以及希望新文件名在服务器上显示的内容。该程序在第一次发送时工作正常,但在第二次发送时冻结,并且第二个文件未写入服务器。任何帮助将不胜感激,因为我真的非常感谢你们。

客户代码

ServerSocket serverSocket;
    public void sendFile(String filePath, String fileFieldName)throws IOException{

        //Parameters filePath is a full direct path of file to be sent
        //Example of filePath "C:\Users\Someone\Desktop\Capture3333333.PNG"
        //FileFieldName is the desired name and extension of file being sent

        // i removed my server ip and just put in a string for demonstration
    Socket socket = new Socket("IP GOES HERE", 5557); 
    System.out.println("Is sending?");
    BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
    DataOutputStream dos = new DataOutputStream(bos);
    File files = new File(filePath);
    long length = files.length();
    dos.writeLong(length);

    dos.writeUTF("\\"+ fileFieldName);

    FileInputStream fis = new FileInputStream(filePath);
    BufferedInputStream bis = new BufferedInputStream(fis);

    int theByte = 0;
    while((theByte = bis.read()) != -1) bos.write(theByte);

    bis.close();
    bos.flush();

    dos.close();
    } 

服务器代码

     ServerSocket serverSocket;

    public void fileListenThread () throws IOException {

        while(true){//This is so the socket keeps listening
         serverSocket = new ServerSocket(5557);
    Socket socket = serverSocket.accept();

    if(socket.isConnected()){
    BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
    DataInputStream dis = new DataInputStream(bis);
    File files ;

    long fileLength = dis.readLong();//This reads file length from client
    String fileName = dis.readUTF();//This reads filename from client
    files = new File( System.getProperty("user.home") + "\\Desktop\\Top-Brand\\Images\\" + fileName );//File received from client is written to this path
    FileOutputStream fos = new FileOutputStream(files);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    for(int j = 0; j < fileLength; j++)
        bos.write(bis.read());
    bos.flush();
    bos.close();
    dis.close();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

移动行

serverSocket = new ServerSocket(5557);

在while循环之外。它应该在循环之前,而不是在循环内部。如果将其放入循环中,则在每次文件传输后,您将创建另一个服务器套接字,该套接字将无法绑定,因为旧的套接字仍然存在。

答案 1 :(得分:0)

已解决-我的服务器套接字在循环内,导致网络绑定异常,这是服务器的工作代码,客户端工作正常

     ServerSocket serverSocket;

    public void fileListenThread () throws IOException {
      serverSocket = new ServerSocket(5557);
        while(true){//This is so the socket keeps listening

    Socket socket = serverSocket.accept();

    if(socket.isConnected()){
    BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
    DataInputStream dis = new DataInputStream(bis);
    File files ;

    long fileLength = dis.readLong();//This reads file length from client
    String fileName = dis.readUTF();//This reads filename from client
    files = new File( System.getProperty("user.home") + "\\Desktop\\Top-Brand\\Images\\" + fileName );//File received from client is written to this path
    FileOutputStream fos = new FileOutputStream(files);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    for(int j = 0; j < fileLength; j++)
        bos.write(bis.read());
    bos.flush();
    bos.close();
    dis.close();
        }
    }
}