package com.itheima.scoketdemo8;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10000);
while (true) {
Socket accept = ss.accept();
BufferedInputStream bis=new BufferedInputStream(accept.getInputStream());
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(".\\"+ UUID.randomUUID().toString() +".png"));
int b;
while ((b= bis.read())!=-1){
bos.write(b);
}
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
bw.write("上传成功");
bw.newLine();
bw.flush();
bos.close();
accept.close();
}
//ss.close();
}
}
package com.itheima.scoketdemo8;
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket=new Socket("127.0.0.1",10000);
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(".\\1.png"));
OutputStream os = socket.getOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(os);
int b;
while ((b=bis.read())!=-1){
bos.write(b);//通过网络写道服务器
}
//给服务器一个结束标记,告诉服务器文件已经传输完毕
socket.shutdownOutput();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
bis.close();
socket.close();
}
}
package com.itheima.scoketdemo8;
import java.util.UUID;
public class UUIDDemo {
public static void main(String[] args) {
UUID uuid=UUID.randomUUID();
String s=uuid.toString().replace("-","");
System.out.println(s);
}
}
|