Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

tcp - Java file transfer file to server

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?

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.

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:

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.

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:

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You did not get InputStream from the socket after serverSocket.accept(). Open InputStream on the socket.

    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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...