本帖最后由 忄雾飞扬 于 2013-10-30 13:00 编辑
总是发生这个异常;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(SocketOutputStream.java:113)
at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
at JpgClient.main(JpgThread.java:82)
我用System.out.println("java").的方法来排除,只找到可能 是存储地址错误,找不到解决办法,求解!!
、、
import java.io.*;
import java.net.*;
class PicCopy implements Runnable
{
private Socket s;
PicCopy(Socket s){
this.s = s;
}
public void run(){
int count = 0;
String ip = s.getInetAddress().getHostAddress();
try{
InputStream in = s.getInputStream();
File dir = new File("f:\\myjava\day23");
File file = new File(dir,ip+"("+(count)+")"+".jpg");
while(file.exists())
file = new File(dir,ip+"("+(count++)+")"+".jpg");
FileOutputStream fis = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while((len = in.read(b))!=-1){
fis.write(b,0,len);
}
OutputStream out = s.getOutputStream();
out.write("图片上传成功".getBytes());
fis.close();
s.close();
}
catch(Exception e){
throw new RuntimeException("图片上传失败");
}
finally{try
{
s.close();
}
catch (Exception ee)
{
throw new RuntimeException("jay");
}}
}
}
class JpgClient
{
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("该文件有问题");
return ;
}
if(!file.getName().endsWith(".jpg"))
{
System.out.println("图片格式错误,请重新选择");
return ;
}
if(file.length()>1024*1024*5)
{
System.out.println("文件过大");
return ;
}
Socket s = new Socket("169.254.141.190",10008);
FileInputStream fis = new FileInputStream(file);
OutputStream out = s.getOutputStream();
System.out.println("java");
byte[] b = new byte[fis.available()];
int len = 0;
while((len = fis.read(b))!= -1){
out.write(b,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] bu= new byte[1024];
int i = 0;
while((i= in.read(bu))!=-1)
System.out.println(new String(bu,0,i));
fis.close();
s.close();
}
}
class JpgServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(10008);
while(true){
Socket s = ss.accept();
new Thread(new PicCopy(s)).start();
}
}
}
|