通过socket读取异地文件

通过socket读取异地文件,其实原理很简单,就是通过一个readfile和writefile命令去调用服务器端的程序,然后服务器返回文件内容。
废话少说,把代码贴出来,不得不说里面的有些内容是剽窃的。呵呵
Client端代码
Java代码

package org.playgod1984.swing.servertest;  

import java.io.*;  

import java.net.*;  

public class Client  

{  

    Socket socket;  

    BufferedReader in;  

    PrintWriter out;  

    public Client()  

    {  

        try  

        {  

            System.out.println("Try to Connect to 127.0.0.1:10000");  

            socket = new Socket("127.0.0.1", 10000);  

            System.out.println("The Server Connected!");  

            System.out.println("Please enter some Character:");  

            BufferedReader line = new BufferedReader(new  

            InputStreamReader(System.in));  

            out = new PrintWriter(socket.getOutputStream(), true);  

            out.println(line.readLine());  
            out.println("END");  
            in = new BufferedReader(new InputStreamReader(socket  
                    .getInputStream()));  
            String rtnValue = "";  
            while (!"END".equals((rtnValue = in.readLine()))) {  
                System.out.println(rtnValue);  
            }  
            out.close();  

            in.close();  

            socket.close();  

        } catch (IOException e)  

        {  

            out.println("Wrong");  

        }  

    }  

    public static void main(String[] args){  
        new Client();  
    }  

}  

Server端
Java代码

package org.playgod1984.swing.servertest;  

import java.net.*;  

import java.io.*;  

public class Server  

{  

    private ServerSocket ss;  

    private Socket socket;  

    private BufferedReader in;  

    private PrintWriter out;  

    public Server()  

    {  

        try  

        {  

            ss = new ServerSocket(10000);  

            while (true){  
                socket = ss.accept();  
                in = new BufferedReader(new InputStreamReader(socket  
                        .getInputStream()));  
                String line = in.readLine();  
                String rtnValue = "";  
                out = new PrintWriter(socket.getOutputStream(), true);  
                if (line.startsWith("readfile")) {  
                    readFile(line.substring(line.indexOf(" ")));  
                } else if (line.startsWith("writefile")) {  
                    String value = "";  
                    StringBuffer writeValue = new StringBuffer("");  
                    while (!"END".equals((value = in.readLine()))) {  
                        writeValue.append(value).append("\r\n");  
                    }  
                    writeFile(line.split(" ")[1], line.split(" ")[2] + "\r\n"  
                            + writeValue);  
                } else {  
                    // do other job;  
                }  
                out.println(rtnValue);  
                out.println("END");  
                out.close();  
                in.close();  
                socket.close();  
            }  

        } catch (IOException e){  
            out.println("wrong");  
        }  

    }  

    public void readFile(String path) {  
        StringBuffer rtnValue = new StringBuffer("");  
        File file = new File(path.trim());  
        BufferedReader reader = null;  
        try {  
            reader = new BufferedReader(new FileReader(file));  
            String tempString = null;  
            // 一次读入一行,直到读入null为文件结束  
            while ((tempString = reader.readLine()) != null) {  
                out.println(tempString);  
            }  
            reader.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (reader != null) {  
                try {  
                    reader.close();  
                } catch (IOException e1) {  
                }  
            }  
        }  
    }  

    public void writeFile(String path, String value) {  

        BufferedOutputStream buff = null;  
        try {  
            buff = new BufferedOutputStream(  
                    new FileOutputStream(new File(path)));  
            buff.write(value.getBytes());  
            buff.flush();  
            buff.close();  
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
            out.println("END");  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
            out.println("END");  
        }  
        out.println("END");  
    }  

    public static void main(String[] args) {  
        new Server();  
    }  

}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我要修改昵称

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值