网络编程模拟客户端上传图片到服务器端出现了个问题找不着,找了挺久都看不出什么错误,然后就重新写了一次成功了 可是这个类里面我到底是什么问题我一点都看不出来
UploadPic.java[code=java]import java.io.*;
import java.net.*;
class UploadPicClient
{
public static void main(String args[])throws Exception
{ if(args.length==0)
{
System.out.println("请选择一个文件");
return;
}
File file=new File(args[0]);
if(!(file.exists()&&file.isFile()&&file.getName().endsWith("jpg")))
{
System.out.println("请选择一个jpg的文件");
return ;
}
if(file.length()>4*1024*1204)
{
System.out.println("请选择一个小于4M的文件");
return ;
}
Socket s=new Socket("192.168.1.102",10001);
FileInputStream fis=new FileInputStream(file);
OutputStream outStream=s.getOutputStream();
int len=0;
byte buf[]=new byte[1024];
while((fis.read(buf))!=-1)
{
outStream.write(buf,0,len);
}
s.shutdownOutput();
InputStream inStream=s.getInputStream();
int num=0;
byte buffin[]=new byte[1024];
num=inStream.read(buffin);
String str=new String(buffin,0,num);
System.out.println(str);
fis.close();
s.close();
}
}
class UploadServiceThread implements Runnable
{ private Socket s;
public UploadServiceThread(Socket s)
{
this.s=s;
}
public void run()
{
try
{
String ip=s.getInetAddress().getHostAddress();
OutputStream outStream =s.getOutputStream();
InputStream inStream =s.getInputStream();
int count=1;
File file=new File(ip+"("+(count++) +")"+".jpg");
while(file.exists())
{
file=new File(ip+"("+(count++) +")"+".jpg");
}
FileOutputStream fos =new FileOutputStream(file);
int len =0;
byte buff[]=new byte[1024];
while((len=inStream.read(buff))!=-1)
{
fos.write(buff,0,len);
}
outStream.write("上传成功".getBytes());
fos.close();
s.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
class UploadPicService
{
public static void main(String args[])throws Exception
{
ServerSocket ss =new ServerSocket(10001);
while(true)
{
Socket s=ss.accept();
new Thread(new UploadServiceThread(s)).start();
}
}
}[/code] |
|