1. Java--网络编程--网络文件上传_xml
    1.   
      package com.model.upload;
      
      import com.sun.xml.internal.ws.util.StreamUtils;
      
      import java.io.*;
      import java.net.ServerSocket;
      import java.net.Socket;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/24 14:49
       * 网络编程实现文件上传
       */
      public class UploadDemo01 {
      //    服务端
          public static void main(String[] args) throws IOException {
              ServerSocket serverSocket = new ServerSocket(8888);
              System.out.println("服务端启动完成监听8888端口号~");
              Socket socket = serverSocket.accept();
      
              BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
              FileOutputStream fileOutputStream = new FileOutputStream("D:\\qq\\IDEA\\IdeaProjects\\mianshi_project\\Java_project\\
        Java_wangluobiancheng\\src\\main\\resources\\file\\b.jpg"); ByteArrayOutputStream bos = new ByteArrayOutputStream();//保存文件的一个流,将文件的所有数据都写到这个流里面,进行操作 byte[] buff=new byte[1024]; int readLine=0; while ((readLine=bufferedInputStream.read(buff))!=-1){ bos.write(buff, 0, readLine); } fileOutputStream.write(bos.toByteArray()); System.out.println("照片接受完成~"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream()); bufferedOutputStream.write("照片成功收到".getBytes()); bufferedOutputStream.flush();//要加入flash将写入的内容进行刷新,否则会爆出 socket is closed错误 socket.shutdownOutput(); bufferedOutputStream.close(); fileOutputStream.close(); bos.close(); bufferedInputStream.close(); socket.close(); serverSocket.close(); } }
    2. package com.model.upload;
      
      import com.sun.xml.internal.ws.util.StreamUtils;
      
      import java.io.*;
      import java.net.InetAddress;
      import java.net.Socket;
      import java.net.UnknownHostException;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/24 14:49
       * 网络编程实现文件上传
       */
      public class UploadDemo02 {
      //    客户端
          public static void main(String[] args) throws IOException {
      
              Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
              System.out.println("连接成功~");
      
              FileInputStream fileInputStream = new FileInputStream("D:\\qq\\IDEA\\IdeaProjects\\mianshi_project\\Java_project\\
      Java_wangluobiancheng\\src\\main\\resources\\file\\a.jpg"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream()); //将文件的整个字节数组放到一个字节数组中 ByteArrayOutputStream bytes=new ByteArrayOutputStream();//创建输出流对象 byte[] buff=new byte[1024]; int readLine=0; while ((readLine=fileInputStream.read(buff))!=-1){ bytes.write(buff,0, readLine); } bufferedOutputStream.write(bytes.toByteArray()); bufferedOutputStream.flush(); socket.shutdownOutput(); BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream()); byte[] buff1=new byte[1024]; int readCount=0; while ((readCount=bufferedInputStream.read(buff1))!=-1){ System.out.println("服务端回复的消息:"+new String(buff1,0,readCount)); } // 注意流的关闭顺序,先用到的后关闭,后用到的先关闭,否则会出现错误 bufferedInputStream.close(); bufferedOutputStream.close(); fileInputStream.close(); socket.close(); } }