简体   繁体   English

Java:将文件从服务器传输到客户端以及从客户端传输到服务器

[英]Java: Transfer a file from server to client and from client to server

I am a newbie and I want to accomplish file transfer from server to client "do something with it" and then send the file back to the server. 我是新手,我想完成从服务器到客户端的文件传输,“然后用它做一些事情”,然后将文件发送回服务器。 The most basic code I am using is here: 我正在使用的最基本的代码在这里:

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 Main {
  public static void main(String[] args) throws IOException {
    ServerSocket servsock = new ServerSocket(123456);
    File myFile = new File("s.pdf");
    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();
    }
  }
}

The client module

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

public class Main {
  public static void main(String[] argv) throws Exception {
    Socket sock = new Socket("127.0.0.1", 123456);
    byte[] mybytearray = new byte[1024];
    InputStream is = sock.getInputStream();
    FileOutputStream fos = new FileOutputStream("s.pdf");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    int bytesRead = is.read(mybytearray, 0, mybytearray.length);
    bos.write(mybytearray, 0, bytesRead);
    bos.close();
    sock.close();
  }
} 

Got it from this website: http://www.java2s.com/Code/Java/Network-Protocol/TransferafileviaSocket.htm 从以下网站获得它: http : //www.java2s.com/Code/Java/Network-Protocol/TransferafileviaSocket.htm

I understand how this works but I don't know how to send a file back to the server. 我了解这是如何工作的,但我不知道如何将文件发送回服务器。 Please help. 请帮忙。

I've written a file transfer class in the past, you can use it both in your client and server (by making an instance) and use the methods to send and receive files as much as you want. 过去我已经编写了一个文件传输类,您可以在客户端和服务器中都使用它(通过创建一个实例),并使用所需的方法来发送和接收文件。

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

public class FileTransferProcessor {
    Socket socket;
    InputStream is;
    FileOutputStream fos;
    BufferedOutputStream bos;
    int bufferSize;


    FileTransferProcessor(Socket client) {
        socket = client;
        is = null;
        fos = null;
        bos = null;
        bufferSize = 0;

    }

    void receiveFile(String fileName) {
        try {
            is = socket.getInputStream();
            bufferSize = socket.getReceiveBufferSize();
            System.out.println("Buffer size: " + bufferSize);
            fos = new FileOutputStream(fileName);
            bos = new BufferedOutputStream(fos);
            byte[] bytes = new byte[bufferSize];
            int count;
            while ((count = is.read(bytes)) >= 0) {
                bos.write(bytes, 0, count);
            }
            bos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void sendFile(File file) {

        FileInputStream fis;
        BufferedInputStream bis;
        BufferedOutputStream out;
        byte[] buffer = new byte[8192];
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            out = new BufferedOutputStream(socket.getOutputStream());
            int count;
            while ((count = bis.read(buffer)) > 0) {
                out.write(buffer, 0, count);

            }
            out.close();
            fis.close();
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

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