import java.io.*;
import java.net.*;
class PtcClient {
public static void main(String[] args) throws Exception{
//1.建立服务
Socket socket=new Socket("192.168.1.8",10006);
FileInputStream filein=new FileInputStream("E:\\360downloads\\wpcache\\abc.jpg");
//读取客户端已有图片
OutputStream output=socket.getOutputStream();
byte[] by1=new byte[1024];
int len=0;
while((len=filein.read(by1))!=-1){
output.write(by1,0,len);
}
//告诉服务端数据已经写完
socket.shutdownOutput();
//读取服务端信息
InputStream input=socket.getInputStream();
byte[] by=new byte[1024];
int line=input.read(by);
System.out.println(new String(by,0,line));
socket.close();
filein.close();
}
}
class PtcThread implements Runnable{
private Socket serversocket;
PtcThread(Socket serversocket){
this.serversocket=serversocket;
}
public void run(){
int count=1;
String ip=serversocket.getInetAddress().getHostAddress();
try{
System.out.println(ip+"connect");
InputStream input=serversocket.getInputStream();
File file=new File(ip+"("+(count)+")"+".jpg");
while(file.exists()){
file=new File(ip+"("+(count++)+")"+".jpg");
}
FileOutputStream output=new FileOutputStream("F:\\"+file);
byte[] by=new byte[1024];
int len=0;
while((len=input.read(by))!=-1){
output.write(by,0,len);
}
OutputStream out=serversocket.getOutputStream();
out.write("你好,上传成功".getBytes());
serversocket.close();
output.close();
}
catch(Exception e){
throw new RuntimeException(ip+"上传失败");
}
}
}
class serversocket{
public static void main(String[] args)throws Exception{
ServerSocket server=new ServerSocket(10006);
while(true){
Socket serversocket=server.accept();
new Thread(new PtcThread(serversocket)).start();
}
}
}
代码运行编译都没有错,可是我想并发上传多张图片,但是只能上传一张,求大神帮忙看看哪里的问题
|
|