• java 上传文件(文本文件)


    /**
     *  java file upload demo
     *  @author:luowen
     *  @time:2013-11-03
     */
    
    import java.net.*;
    import java.io.*;
    
    
    class UploadClient
    {
        public static void main(String[] args)throws Exception
        {
            //建立客户端socket对象
            Socket s = new Socket("127.0.0.1",10000);
            
            BufferedReader bur = new BufferedReader(new FileReader("demo.txt"));
    
            PrintStream ps = new PrintStream(s.getOutputStream(),true);
    
            String line;
    
            while((line = bur.readLine()) != null)
            {
                ps.println(line);
            }
    
            s.shutdownOutput();
    
            BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
    
            while((line = bufr.readLine()) != null)
            {
               System.out.println(line);
            }
    
            bur.close();
            bufr.close();
            ps.close();
            s.close();
            
        }
    }
    
    class UploadServer
    {
        public static void main(String[] args)throws Exception
        {
           ServerSocket ss = new ServerSocket(10000);
    
           Socket s = ss.accept();
    
           String ip = s.getInetAddress().getHostName();
    
           System.out.println(ip+"........connected!");
    
           BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
    
           //BufferedWriter bufw = new BufferedWriter(new FileWriter("ServerDemo.txt"));
           PrintWriter bufw = new PrintWriter(new FileWriter("ServerDemo.txt"));
    
           String line;
    
           while((line = bufr.readLine()) != null)
           {
                //bufw.write(line,0,line.length());
                //bufw.flush();
                bufw.println(line);
           }
    
           OutputStream os = s.getOutputStream();
    
           os.write("上传成功!".getBytes());
    
           os.close();
           bufr.close();
           bufw.close();
           ss.close();
        }
    }
    

      

  • 相关阅读:
    CentOS安装rz\sz命令
    间隔线美化
    linuxバッチの環境変数の設定
    Base64编码
    百分号编码
    java四月份总结
    win10窗口修改 #强制窗口修改 #任意修改 #窗口
    spacedesk #踩坑 #displayoff
    pip安装出错 #代理网络问题 #anocanda
    Fdroid 开源foss市场 里面很多开源应用程序
  • 原文地址:https://www.cnblogs.com/luowen/p/3404937.html
Copyright © 2020-2023  润新知