class PicThread implements Runnable
{
private Socket s;
PicThread(Socket s)
{
this.s = s;
}
public void run()
{
int count = 1;
String ip = s.getInetAddress().getHostAddress();
try
{
System.out.println(ip+"connected.....");
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 [] bufin = new byte[1024];
int len = 0;
while((len = in.read(bufin)) != -1)
{
fos.write(bufin,0,len);
}
s.shutdownOutput();
// 建立Socket的输出流输出反馈给客户端的信息
OutputStream os = s.getOutputStream();
os.write("上传成功".getBytes());
// 关闭资源
fos.close();
s.close();
}
catch(Exception e)
{
throw new RuntimeException(ip+":上传失败");
}
}
}
运行结果:
Exception in thread "Thread-0" java.lang.RuntimeException: 192.168.1.100:上传失败
at PicThread.run(PicUpload.java:42)
at java.lang.Thread.run(Thread.java:745)
|