简体   繁体   English

套接字,java发送文件服务器客户端

[英]sockets, java sending files server client

I want to send a file through a socket. 我想通过套接字发送文件。 The "server" is sitting on another computer than mine and the client in another computer as well. “服务器”位于我的另一台计算机上,客户端也位于另一台计算机上。 The file can go to and fro server and client but only in their current directory. 该文件只能在当前目录中往返服务器和客户端。 The approach i have taken up to now is by using a file input stream and writing the file on a file output stream neverthelees this doesnt work as far as i understand.. Is there another way to send files through sockets? 我到目前为止所采用的方法是使用文件输入流并将文件写入文件输出流,尽管如此,据我所知这是行不通的。是否有另一种通过套接字发送文件的方法?

Here is my code what could be wrong here? 这是我的代码,这里可能出什么问题了?

public class Copy {

    private ListDirectory dir;
    private Socket socket;

    public Copy(Socket socket, ListDirectory dir) {
        this.dir = dir;
        this.socket = socket;
    }

    public String getCopyPath(String file) throws Exception {
        String path = dir.getCurrentPath();
        path += "\\" + file;
        return path;

    }

    public void copyFileToClient(String file, String destinationPath) 
    throws Exception {

        byte[] receivedData = new byte[8192];
        BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(getCopyPath(file)));
        String findDot = file;
        String extension = "";

        for (int i = 0; i < findDot.length(); i++) {
            String dot = findDot.substring(i, i + 1);
            if (dot.equals(".")) {
                extension = findDot.substring(i + 1);
            }
        }
        if (extension.equals("")) {
            extension = "txt";
        }
        BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(destinationPath + "\\"
                + "THECOPY" + "." + extension)));

        int len;

        while ((len = bis.read(receivedData)) > 0) {

            bos.write(receivedData, 0, len);
        }
        //      in.close();
        bis.close();
        //      output.close();
        bos.close();

    }

    //  public static void main(String args[]) throws Exception {
    //      Copy copy = new Copy();
    //      System.out.print(copy.getCopyPath("a"));
    //  }

}

And some client code : 和一些客户端代码:

...

DataOutputStream outToServer = new DataOutputStream(
        clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(
        new InputStreamReader(clientSocket.getInputStream()));
boolean exit = false;

else if (sentence.length() > 3 && sentence.substring(0, 3).equals("get")) {
        String currPath = dir.getCurrentPath();
        outToServer.writeBytes(sentence + "_" + currPath + "\n");

} else {

...

Your copyFileToClient method uses a FileInputStream and FileOutputStream directly, ie it does not transfer anything to/from the client at all, only from one local file to another. 您的copyFileToClient方法直接使用FileInputStream和FileOutputStream,即它根本不向客户端传输任何内容,仅从一个本地文件向另一个本地文件传输任何内容。 This is fine if you want to manage remotely files on the server, but does not help for sending data between different computers. 如果要远程管理服务器上的文件,这很好,但是对于不同计算机之间的数据发送无济于事。

You have to somehow send the data through the OutputStream/InputStream of the Socket - ie use the FileInputStream at the sending side and the FileOutputStream at the receiving one. 您必须以某种方式通过Socket的OutputStream / InputStream发送数据-即在发送端使用FileInputStream,在接收端使用FileOutputStream。

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

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