Java文件传输文件到服务器 [英] Java file transfer file to server

查看:219
本文介绍了Java文件传输文件到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java网络新手,已经找了一段时间解决我的问题了,我想为什么不在这个问题上向一些更有资格的人提出一些建议呢?

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?

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

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.

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

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:

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

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.

亲切的问候,Alex

Kind regards, Alex

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

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:

服务器

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() {

    }
}

客户

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();
    }
}


推荐答案

你在 serverSocket.accept()之后,没有从套接字获取 InputStream 。在套接字上打开 InputStream

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

这篇关于Java文件传输文件到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆