• 文件上传案例及多线程版本


    文件上传案例

    代码演示:

     
     1 public class TCPServer {
     2     public static void main(String[] args) throws IOException {
     3         ServerSocket server=new ServerSocket(5555);
     4         Socket socket=server.accept();
     5         //明确数据源
     6         InputStream in=socket.getInputStream();
     7         //明确目的地
     8         File file=new File("x:\upload");
     9         if(!file.exists()){
    10             file.mkdirs();
    11         }
    12         //域名+毫秒值
    13         String filename="oracle"+System.currentTimeMillis()+".jpg";
    14         FileOutputStream fos=new FileOutputStream(file+File.separator+filename);
    15         //复制
    16         int len=0;
    17         byte[] bytes=new  byte[1024];
    18         while((len=in.read(bytes))!=-1){
    19             fos.write(bytes,0,len);
    20         }
    21 
    22         //回复客户端
    23         OutputStream out=socket.getOutputStream();
    24         out.write("上传成功!".getBytes());
    25         //释放资源
    26         server.close();
    27         fos.close();
    28     }
    29 }
     
     
     1 public class TCPClinet {
     2     public static void main(String[] args) throws IOException {
     3         Socket socket=new Socket("192.168.1.171",7000);
     4         OutputStream out=socket.getOutputStream();
     5         //明确数据源
     6         FileInputStream fis=new FileInputStream("x:\test\img1.jpg");
     7         int len=0;
     8         byte[] bytes=new byte[1024];
     9         //文件复制 
    10         while((len=fis.read(bytes))!=-1){
    11             out.write(bytes,0,len);
    12         }
    13         //告诉服务器端不要在读了到末尾了
    14         socket.shutdownOutput();
    15         //服务器端回复
    16         InputStream in=socket.getInputStream();
    17         len=in.read(bytes);
    18         System.out.println(new String(bytes, 0, len));
    19         //释放资源
    20         fis.close();
    21         socket.close();
    22     }
    23 
    24 }
     

      文件上传案例多线程版本

    代码演示:

     
    1 public class Demo {
    2     public static void main(String[] args) throws IOException {
    3         ServerSocket server=new ServerSocket(6000);
    4         while(true){
    5             Socket socket=server.accept();
    6             new Thread(new Upload(socket)).start();
    7         }    
    8     }
    9 }
     
     
     1 public class Upload implements Runnable{
     2     private Socket socket;
     3     public Upload(Socket socket){
     4         this.socket=socket;
     5     }
     6     public void run() {
     7         //明确数据源
     8         FileOutputStream fos=null;
     9         try {
    10             InputStream in= socket.getInputStream();
    11             //明确目的地
    12             File file=new File("x:\upload");
    13             if(!file.exists()){
    14                 file.mkdirs();
    15             }
    16             //域名+毫秒值
    17             String filename="oracle"+System.currentTimeMillis()+".jpg";
    18             fos=new FileOutputStream(file+File.separator+filename);
    19             //复制
    20             int len=0;
    21             byte[] bytes=new  byte[1024];
    22             while((len=in.read(bytes))!=-1){
    23                 fos.write(bytes,0,len);
    24             }
    25             //回复客户端
    26             OutputStream out=socket.getOutputStream();
    27             out.write("上传成功!".getBytes());
    28         } catch (IOException e) {
    29             e.printStackTrace();
    30         }finally{
    31             //释放资源
    32             try {
    33                 fos.close();
    34             } catch (IOException e) {
    35                 e.printStackTrace();
    36             }
    37         }
    38     }
    39 }
  • 相关阅读:
    算法与数据结构 (四) 排序 一 交换类排序
    算法与数据结构 (三) 二叉树的简单应用 二叉查找树,二叉堆排序
    算法与数据结构 (二) 二叉树的简单实现,非递归的前序遍历 中序遍历 后序遍历
    算法与数据结构 (一) 链表,栈,队列的简单实现
    服务器端的redis和MySQL的远程连接的简单解决方案
    记一次自定义监听器使用spring 管理的bean的问题
    基于java开发的RBAC模型权限管理系统
    2019 本科java开发春招面经(实习)
    记一次Bootstrap框架下 使用Ajax失效的问题
    [转]在static代码块或static变量的初始化过程中使用ServiceManager提供的api的陷阱
  • 原文地址:https://www.cnblogs.com/qq1312583369/p/10223217.html
Copyright © 2020-2023  润新知