黑马程序员技术交流社区
标题:
求助!大神
[打印本页]
作者:
liu100chao
时间:
2015-6-12 19:32
标题:
求助!大神
/*
需求:多客户并发上传图片。
*/
import java.io.*;
import java.net.*;
class picture1
{
public static void main(String[] args) throws Exception
{
Socket ss = new Socket("192.168.1.104",10043);
FileInputStream read = new FileInputStream("C:\\Users\\liuchao\\Desktop\\1.PNG");
OutputStream write = ss.getOutputStream();
byte[] data = new byte[1024];
int len = 0;
while((len=read.read(data))!=-1)
{
write.write(data,0,len);
}
ss.shutdownOutput();
InputStream wri = ss.getInputStream();
byte[] bufin = new byte[1024];
int shuzi = wri.read(bufin);
System.out.println(new String(bufin,0,shuzi));
read.close();
ss.close();
}
}
class pic implements Runnable
{
private Socket s ;
pic(Socket s)
{
this.s=s;
}
public void run()
{
String ip =s.getInetAddress().getHostAddress();
int count=1;
try
{
System.out.println(ip);
InputStream read = s.getInputStream();
File dir = new File("C:\\Users\\liuchao\\Desktop\\1_copy.PNG");
File file = new File(dir,ip+"("+count+")"+".PNG");
if(file.exists())
file = new File(dir,ip+"("+(count++)+")"+".PNG");
FileOutputStream write = new FileOutputStream(file);
int len = 0;
byte[] data = new byte[1024];
while((len=read.read(data))!=-1)
{
write.write(data,0,len);
}
OutputStream out=s.getOutputStream();
out.write("上传成功".getBytes());
write.close();
s.close();
}
catch (Exception e)
{
System.out.println("上传失败");
}
}
}
class picture2
{
public static void main(String[] args) throws Exception
{
ServerSocket ss= new ServerSocket(10043);
while(true)
{
Socket s = ss.accept();
new Thread(new pic(s)).start();
}
}
}
今天瞧这个代码不知为什么,只要去掉多线程的代码就没问题,已加入多线程,每次运行都显示“上传失败”,请问各位大神们这是怎麽回事??会不会是我电脑的问题???
作者:
liu100chao
时间:
2015-6-12 20:04
只加入线程,运行就不通过!弄了半天,都没弄明白!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2