OutputStream out=s.getOutputStream();
byte[]buf=new byte[1024];
int len=0;
while((len=bufr.read(buf))!=-1)
{
out.write(buf,0,len);
}
//告诉服务端数据已写完
s.shutdownOutput();
InputStream in=s.getInputStream();
byte[] bufIn=new byte[1024];
int num= in.read(bufIn) ;
System.out.println(new String(bufIn,0,num));
bufr.close();
s.close();
}
}
[java]
package net2;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
* 服务端
*
*/
public class PicServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(10029);
while(true)
{
Socket s=ss.accept();
new Thread(new PicThread(s)).start();
}
}
}
package net2;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
* 服务端
*
*/
public class PicServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(10029);
while(true)
{
Socket s=ss.accept();
new Thread(new PicThread(s)).start();
}
}
}
[java]
package net2;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class PicThread implements Runnable {
private Socket s;
PicThread(Socket s)
{
this.s=s;
}
@Override
public void run() {
int count=1;
String ip = s.getInetAddress().getHostAddress();
try
{
System.out.println("ip" + ip + "connect...");
InputStream in= s.getInputStream();
File file=new File(ip+"("+count+")"+".jpg");
while(file.exists())
{
file=new File(ip+"("+(count++)+")"+".jpg");
}
FileOutputStream fos=new FileOutputStream(file);
byte[]buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1)
{
fos.write(buf,0,len);
}
OutputStream out=s.getOutputStream() ;
out.write("上传成功".getBytes());
fos.close();
s.close();
}
catch(Exception e)
{
throw new RuntimeException(ip+"上传失败");
}
}
}
package net2;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class PicThread implements Runnable {
private Socket s;
PicThread(Socket s)
{
this.s=s;
}
@Override
public void run() {
int count=1;
String ip = s.getInetAddress().getHostAddress();
try
{
System.out.println("ip" + ip + "connect...");
InputStream in= s.getInputStream();
File file=new File(ip+"("+count+")"+".jpg");
while(file.exists())
{
file=new File(ip+"("+(count++)+")"+".jpg");
}
FileOutputStream fos=new FileOutp