java Java文件传输文件到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32251895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:53:50  来源:igfitidea点击:

Java file transfer file to server

javatcpfile-transfer

提问by aekramer

I am new to Java networking, and having looked for a resolution for my problem for a while now, I figured why not ask some advice from some more qualified people on this matter?

我是 Java 网络的新手,并且已经为我的问题寻找了一段时间的解决方案,我想为什么不就这个问题向一些更有资格的人寻求一些建议?

I currently have made a small tool which manages a server of mine, and another small client tool. My goal is for the tool to be able to send commands from the client to the server computer. This way I can perform certain actions on the server computer from another machine, including sending a zip archive with updated files.

我目前已经制作了一个管理我的服务器的小工具,以及另一个小客户端工具。我的目标是使该工具能够从客户端向服务器计算机发送命令。通过这种方式,我可以在另一台机器上的服务器计算机上执行某些操作,包括发送带有更新文件的 zip 存档。

I have the basics setup: a TCP connection that sends a command from client to server (server replies with a confirmation) and then I would like the supposed action to take place. My question now is this:

我有基本的设置:一个 TCP 连接,它从客户端向服务器发送命令(服务器回复确认),然后我希望发生假设的操作。我现在的问题是:

When sending a file (.zip) from the client to server, should I send it over TCP or use something like FTP? I would not only like to send the file to the server, but also when it arrived to extract and replace the existing files.

当从客户端向服务器发送文件 (.zip) 时,我应该通过 TCP 发送它还是使用类似 FTP 的东西?我不仅想将文件发送到服务器,而且还想在它到达时提取和替换现有文件。

Kind regards, Alex

亲切的问候,亚历克斯

EDIT: This is what I used for transferring a file from the client to server, however the file doesn't reach the destination in full size.. D:

编辑:这是我用于将文件从客户端传输到服务器的方法,但是该文件没有以完整大小到达目的地.. D:

Server

服务器

package server.control.net.impl;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Update implements Runnable {

    private final static int serverPort = 5555;
    private final static String fileInput = "C:\Users\Alexander\Documents\update.zip";

    public static void main(String args[]) throws IOException{
        ServerSocket servsock = new ServerSocket(serverPort);
        File myFile = new File(fileInput);
        while (true) {
          Socket sock = servsock.accept();
          byte[] mybytearray = new byte[(int) myFile.length()];
          BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
          bis.read(mybytearray, 0, mybytearray.length);
          OutputStream os = sock.getOutputStream();
          os.write(mybytearray, 0, mybytearray.length);
          os.flush();
          sock.close();
        }
    }

    public static void start(){
        Update upd = new Update();  
        Thread tupd = new Thread(upd);  
        tupd.start(); 
    }

    @Override
    public void run() {

    }
}

Client

客户

package server.control.net;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPClient {

    private final static String serverIP = "127.0.0.1";
    private final static int serverPort = 5555;
    private final static String fileOutput = "C:\Users\Alexander\Documents\updateoutput.zip";

    public static void main(String args[]) throws UnknownHostException, IOException {
        Socket sock = new Socket(serverIP, serverPort);
        byte[] mybytearray = new byte[1024];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new FileOutputStream(fileOutput);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int bytesRead = is.read(mybytearray, 0, mybytearray.length);
        bos.write(mybytearray, 0, bytesRead);
        bos.close();
        sock.close();
    }
}

回答by Ravindra babu

You did not get InputStreamfrom the socket after serverSocket.accept(). Open InputStreamon the socket.

之后你没有InputStream从socket中获取serverSocket.accept()InputStream在插座上打开。

    clientSocket = serverSocket.accept();

    InputStream in = clientSocket.getInputStream();

    // Writing the file to disk
    // Instantiating a new output stream object
    OutputStream output = new FileOutputStream("YourFile.zip");

    byte[] buffer = new byte[1024];
    while ((bytesRead = in.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
    // Closing the FileOutputStream handle
    output.close();

Refer to working example at : Write and Read File over Socket

参考工作示例:Write and Read File over Socket