Java心得34
2015-09-16 23:27
495 查看
今天跟大家分享一下:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
private static boolean isRun = true;
public MyServer(){
try {
ServerSocket server = new ServerSocket(8080);
while(isRun){
Socket socket = server.accept();
new MyThread(socket);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MyServer m = new MyServer();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MyThread implements Runnable{
private FilePress fp = new FilePress();
private Socket socket;
public MyThread(Socket socket){
this.socket = socket;
Thread t = new Thread(this);
t.start();
}
@Override
public void run() {
InputStream in = null;
OutputStream out = null;
try {
in = socket.getInputStream();
out = socket.getOutputStream();
byte[]b = new byte[1024];
in.read(b);
String str = new String(b).trim();
fp.writeFile(str);
out.write("文件已保存".getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class FilePress {
private static Properties pro = new Properties();
public void writeFile(String str){
String info = str.substring(str.indexOf("?")+1, str.lastIndexOf(" "));
String[]x = info.split("&");
String fileName = x[0].substring(x[0].indexOf("=")+1);
for(int i = 1;i<x.length;i++){
String temp[] = x[i].split("=");
pro.setProperty(temp[0], temp[1]);
}
try {
pro.store(new FileWriter(fileName+".properties"), null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JOptionPane;
public class MyClient {
public MyClient(){
String str = JOptionPane.showInputDialog(null,"请输入消息");
try {
Socket socket = new Socket("127.0.0.1",8080);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write(str.getBytes());
out.flush();
byte[]b = new byte[1024];
in.read(b);
String info = new String(b).trim();
JOptionPane.showMessageDialog(null, "服务器回复:"+info);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MyClient m = new MyClient();
}
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
private static boolean isRun = true;
public MyServer(){
try {
ServerSocket server = new ServerSocket(8080);
while(isRun){
Socket socket = server.accept();
new MyThread(socket);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MyServer m = new MyServer();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MyThread implements Runnable{
private FilePress fp = new FilePress();
private Socket socket;
public MyThread(Socket socket){
this.socket = socket;
Thread t = new Thread(this);
t.start();
}
@Override
public void run() {
InputStream in = null;
OutputStream out = null;
try {
in = socket.getInputStream();
out = socket.getOutputStream();
byte[]b = new byte[1024];
in.read(b);
String str = new String(b).trim();
fp.writeFile(str);
out.write("文件已保存".getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class FilePress {
private static Properties pro = new Properties();
public void writeFile(String str){
String info = str.substring(str.indexOf("?")+1, str.lastIndexOf(" "));
String[]x = info.split("&");
String fileName = x[0].substring(x[0].indexOf("=")+1);
for(int i = 1;i<x.length;i++){
String temp[] = x[i].split("=");
pro.setProperty(temp[0], temp[1]);
}
try {
pro.store(new FileWriter(fileName+".properties"), null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JOptionPane;
public class MyClient {
public MyClient(){
String str = JOptionPane.showInputDialog(null,"请输入消息");
try {
Socket socket = new Socket("127.0.0.1",8080);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write(str.getBytes());
out.flush();
byte[]b = new byte[1024];
in.read(b);
String info = new String(b).trim();
JOptionPane.showMessageDialog(null, "服务器回复:"+info);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MyClient m = new MyClient();
}
}
相关文章推荐
- Java心得33
- 继承、接口小例子
- Spring的事务管理阅读笔记与感悟(一)
- emmet —— Eclipse html 插件
- Java心得32
- Java中数组中的clone()方法
- java基础-set集合
- 认识Java Spring 框架
- java设计模式第11弹--建造者模式
- 栈的应用之括号的匹配Java语言描述
- 基于maven搭建ssh项目
- Java解析XML重复Id
- JVM系列文章(五):Javac编译与JIT编译
- webService客户端传输xml中文乱码
- Java学习笔记(七)函数
- java: web应用中不经意的内存泄露
- JVM系列文章(一):Java内存区域分析
- java__IO_02
- java 编程思想 阅读笔记(2)
- 看别人写的Java中String的一些方法