JAVA 基础 网络编程 简单实现文件上传

论坛 期权论坛 脚本     
匿名技术用户   2021-1-5 22:09   427   0
  • 1 单线程上传


1)SERVER

package fileUpLoad;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class Server {
 public static void main(String[] args) throws IOException {
  //创建对象绑定端口
  ServerSocket ss = new ServerSocket(9999);
  //获得socket对象
  Socket s = ss.accept();
  InetAddress ipObj = s.getInetAddress();
  System.out.println(ipObj.getHostAddress()+"登录服务器。");
  //获得输入流
  InputStream in = s.getInputStream();
  Random ran = new Random();
  BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream("/home/alex/images/" + ran.nextInt(9999) + ".jpg"));
  //将socket流中的数据写入fileout流中
  byte[] buf = new byte[1024];
  int len = -1;
  while( (len = in.read(buf)) != -1 ) {
   fileOut.write(buf, 0, len);
  }
  //反馈信息
  OutputStream out = s.getOutputStream();
  String str = ipObj.getHostAddress()+ "图片复制成功。";
  out.write(str.getBytes());
  //关闭资源
  in.close();
  out.close();
  fileOut.close();
  s.close();
 }
}

2)Client

package fileUpLoad;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
 
 public static void main(String[] args) throws IOException {
  //创建客户端,链接服务器
  Socket socket = new Socket("127.0.0.1", 9999);
  //获得输出流,用来写文件到服务器
  OutputStream out = socket.getOutputStream();
  //获得图片的输入流
  BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream("/home/alex/images/11.jpeg"));
  //将图片流写入到服务器
  byte[] buf = new byte[1024];
  int len = -1;
  while( (len = fileIn.read(buf)) != -1 ) {
   out.write(buf, 0, len);  
  }
  //客户端发送数据完毕,结束Socket输出流的写入操作,告知服务器端
  socket.shutdownOutput();
  //获得反馈信息
  InputStream in = socket.getInputStream();
  int length = in.read(buf);
  System.out.println(new String(buf, 0, length));
  //关闭资源
  socket.close();
  in.close();
  out.close();
  fileIn.close();
 }
 
}
  • 2 多线程文件上传

多线程文件上传只涉及修改服务器端,客户端不用修改用之前的就行。

1)Server

package fileUpLoad;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MultiThreadServer {

 public static void main(String[] args) throws IOException {
  // 创建对象绑定端口
  ServerSocket ss = new ServerSocket(9999);
  while (true) {
   // 客户端套接字
   Socket clientSocket = ss.accept();
   //匿名内部类实现多线程
   new Thread() {
    public void run() {
     try {
      String clientIP = clientSocket.getInetAddress().getHostAddress();
      System.out.println(clientIP + "登录服务器。");
      // 获得文件输出流

      File path = new File("/home/alex/images/" + clientIP);
      if(!path.exists()) {
       path.mkdir();
      }
      FileOutputStream file;
      BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream(path.getAbsolutePath()+ "/" +System.currentTimeMillis() +".jpg"));
      // 获得输入流
      InputStream clientIn = clientSocket.getInputStream();
      byte[] buf = new byte[1024];
      int len = -1;
      while ((len = clientIn.read(buf)) != -1) {
       fileOut.write(buf);
      }
      // 反馈信息
      // 获得客户端输出流
      OutputStream out = clientSocket.getOutputStream();
      out.write("文件上传完毕.".getBytes());
      // 关闭资源
      clientIn.close();
      clientSocket.close();
      fileOut.close();
      out.close();
     }catch(IOException e) {
      e.printStackTrace();
     }
    }
   }.start();
  }
 }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP