打开图片显示:这个程序运行后图片无法打开,因为另一程序正在编辑此图片。
求大虾帮忙找一下错误之处,谢谢!程序如下:
import java.net.*;
import java.io.*;
class PicClient
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.167.12.98",10006);
File file=new File(args[0]);
FileInputStream fis=new FileInputStream(file);
OutputStream out=s.getOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=fis.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));
fis.close();
s.close();
}
}
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+"..contect..");
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+"上传失败");
}
}
}
class PicServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(10006);
while(true)
{
Socket s=ss.accept();
new Thread(new PicThread(s)).start();
}
}
} |
|