通过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();
}
}