代码如下:
//并发图片上传
import java.io.*;
import java.net.*;
class PicClient2
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket(InetAddress.getByName("localhost"),11111);
BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("E:\\nz.jpg"));
byte[] data = new byte[1024*1024*5];
int len = bufis.read(data);
OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream();
out.write(data,0,len);
out.flush();
len = in.read(data);
String show = new String(data,0,len);
System.out.println(show);
bufis.close();
s.close();
}
}
class PicServer2
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(11111);
Socket s = ss.accept();
PrintStream ps = new PrintStream(new FileOutputStream("D:\\nzz.jpg"),true);
InputStream in = s.getInputStream();
byte[] data = new byte[1024*1024*5];
int len = in.read(data);
ps.write(data,0,len);
ps.flush();
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
out.flush();
ps.close();
s.close();
ss.close();
}
}
发生异常:
Exception in thread "main" java.net.SocketException: Software caused connection
abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at PicClient2.main(并发图片上传.java:19)