日期:2014-05-20  浏览次数:20770 次

客户端发图片疑问
客户端:
package twenty_four;
import java.io.*;
import java.net.*;
public class PicClient2 {
        public static void main(String[] args)throws Exception {
if(args.length!=1)
{
System.out.println("请选择一个JPG格式端图片");
return;
}
File file=new File(args[0]);
if(!(file.exists()&&file.isFile()))
{
        System.out.println("文件有问题");
}
if(!file.getName().endsWith(".jpg"))
{
        System.out.println("图片格式错误");
        return;
}
if(file.length()>1024*1024*5)
{
                System.out.println("文件过大");
                return;
}
Socket s=new Socket("192.168.106.88",40007);
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();
        }

}
服务端:
package twenty_four;
import java.io.*;
import java.net.*;
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+"...........connection");
        InputStream in=s.getInputStream();
        File file=new File(ip+"("+(count)+")"+".jpg");
        while(file.exists())
                file=new File(ip+"("+(count)+")"+".jsp");
        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+"上传失败");
        }

}
}
public class PicServer2 {
        public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(40007);
while(true)
{
Socket s=ss.accept();
new Thread(new PicThread(s)).start();
}
        }

}

这两段代码运行后,客户端无法输入路径,而且还没输入路径呢就出现   "请选择一个JPG格式端图片"这个警告,这是咋么回事?

------解决方案--------------------
我测试了一下,没有发现什么问题呀,可以得到想要的结果