Java: TCP 文件上传

2022/4/3 17:20:40

本文主要是介绍Java: TCP 文件上传,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Server & Client

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Utils.java

package den;


import java.io.*;
import java.nio.charset.StandardCharsets;

public class Utils{
  public static byte[] streamToByteArray(InputStream inputStream) throws IOException{
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int len;
    while((len = inputStream.read(buf)) != -1){
      byteArrayOutputStream.write(buf, 0, len);
    }
    byte[] bytes = byteArrayOutputStream.toByteArray();
    byteArrayOutputStream.close();
    // inputStream.close();  会引起相关socket关闭
    return bytes;
  }

  public static String streamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
    String line;
    StringBuilder stringBuilder = new StringBuilder();
    while((line = bufferedReader.readLine()) != null){
      stringBuilder.append(line).append("\n");
    }
    // inputStream.close();
    return stringBuilder.toString();
  }
}

UploadServer.java

package den;


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

public class UploadServer{
  public static void main(String[] args) throws IOException{
    ServerSocket serverSocket = new ServerSocket(5555, 2048, InetAddress.getLocalHost());
    System.out.println("serverSocket.getLocalSocketAddress() = " + serverSocket.getLocalSocketAddress());

    Socket socket = serverSocket.accept();

    InputStream inputStream = socket.getInputStream();
    byte[] bytes = Utils.streamToByteArray(inputStream);
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("src/p.png"));
    bufferedOutputStream.write(bytes);

    OutputStream outputStream = socket.getOutputStream();
    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
    bufferedWriter.write("server aa\n");
    bufferedWriter.write("server bb\n");
    bufferedWriter.newLine();
    bufferedWriter.flush();

    bufferedOutputStream.close();
    bufferedWriter.close();
    inputStream.close();
    outputStream.close();
    socket.close();
    serverSocket.close();
    // FileInputStream fileInputStream = new FileInputStream("src/img.png");
    // byte[] bytes = Utils.streamToByteArray(fileInputStream);

    // FileInputStream fileInputStream = new FileInputStream("src/b.properties");
    // String s = Utils.streamToString(fileInputStream);
    // System.out.println("s = " + s);


  }
}

UploadClient.java

package den;


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

public class UploadClient{
  public static void main(String[] args) throws IOException{
    Socket socket = new Socket(InetAddress.getLocalHost(), 5555);

    FileInputStream fileInputStream = new FileInputStream("src/img.png");
    byte[] bytes = Utils.streamToByteArray(fileInputStream);
    OutputStream outputStream = socket.getOutputStream();
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
    bufferedOutputStream.write(bytes);
    socket.shutdownOutput();

    InputStream inputStream = socket.getInputStream();
    String s = Utils.streamToString(inputStream);
    System.out.println(s);

    bufferedOutputStream.close();
    socket.close();

  }
}

 



这篇关于Java: TCP 文件上传的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程