将文件从客户端发送到服务器

时间:2014-06-01 14:19:08

标签: java io ftp client-server file-transfer

在下面的代码中,我试图让服务器端从客户端选择文件并发送它,所以基本上所有工作都在服务器端完成。程序通过运行服务器首先等待客户端运行,建立连接,然后服务器将文件的位置发送到客户端outReader.write("B://ghassar/ghassar.txt");客户端读取位置并发送文件。我已经运行了调试,一旦服务器读取此代码String filename = inReader.readLine();它停止并且它像循环一样进入,任何人都可以帮我解决问题

/ Server.java

/*
 * Server waits for a connection to be established by client
 *
 */

import java.io.*;
import java.net.*;

class Server
{
    public static void main(String args[])throws Exception
    {
        System.out.println("Server running...");

        /* Listen on port 5555 */

        ServerSocket server = new ServerSocket(222);

        /* Accept the sk */

        Socket sk = server.accept();

        System.out.println("Server accepted client");
        // to recive from client
        InputStream input = sk.getInputStream();
        //read what is coming
        BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
        //write to client
        BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
            outReader.write("B://ghassar/ghassar.txt");
            outReader.flush();
        /* Read the filename */
        String filename = inReader.readLine();

        if (filename.equals("ghassar.txt") ){

            /* Reply back to client with READY status */

            outReader.write("READY\n");
            outReader.flush();
        }

        /* Create a new file in the libya directory using the filename */
        FileOutputStream wr = new FileOutputStream(new File("B://libya/ "+ filename));

        byte[] buffer = new byte[sk.getReceiveBufferSize()];

        int bytesReceived = 0;

        while((bytesReceived = input.read(buffer))>0)
        {
            /* Write to the file */
           wr.write(buffer,0,bytesReceived);
        }
    }
}


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class Client  {
    public static void main(String args[]){



        Client clientForm = new Client();
        clientForm.action();

    }
    public void action() {


        try{

            /* Try to connect to the server on localhost, port 5555 */

            Socket sk = new Socket("localhost", 222);
            OutputStream output = sk.getOutputStream();

            /* the steam to send the staff*/

            OutputStreamWriter outputStream = new OutputStreamWriter(sk.getOutputStream());

            /* steam to recive staff */

            BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));

               String serverStatus = inReader.readLine(); // Read the first line
               String filename =  serverStatus;//fileDlg.getSelectedFile().getAbsolutePath();
               outputStream.write("ghassar.txt");
               outputStream.flush();

            /* If server is ready, send the file */

            if ( serverStatus.equals("READY") ){

                FileInputStream file = new FileInputStream(filename);

                byte[] buffer = new byte[sk.getSendBufferSize()];

                int bytesRead = 0;

                while((bytesRead = file.read(buffer))>0)
                {
                    output.write(buffer,0,bytesRead);
                }

                output.close();
                file.close();
                sk.close();
               System.out.println("File sent");

            }
        }
        catch (Exception ex){
            /* Catch any errors */
                 System.out.println("not File sent");

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议您使用PrintWriter代替BufferedWriter。在每一行之后无需调用flush,只需使用println()方法以及自动刷新功能来添加新行。

  

与PrintStream类不同,如果启用了自动刷新,则仅在调用printlnprintfformat方法之一时才会执行,而不是每当换行符时碰巧是输出。

     

这些方法使用平台自己的行分隔符概念而不是换行符。

无需在邮件本身附加\n

示例代码:

// here true means auto flush when `println()` method is called
PrintWriter w=new PrintWriter(new OutputStreamWriter(sk.getOutputStream()),true);
w.println(message);

代码中可能的原因:

问题可能出在以下行,其中{04}方法在\n方法中的字符串末尾没有添加write(),而在客户端,方法readLine()用于读取它

BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
outReader.write("B://ghassar/ghassar.txt"); 
outReader.flush();

注意:您可以使用BufferedWriter#newLine()代替编写行分隔符的\n。行分隔符字符串由系统属性line.separator定义,不一定是单个换行符('\n')。