A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 胡刚涛 中级黑马   /  2012-8-11 14:20  /  1688 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.xml.ws.handler.MessageContext.Scope;

public class UploadPic {
/**
  * @param args
  * @throws IOException
  * @throws UnknownHostException
  */
public static void main(String[] args) throws UnknownHostException, IOException {
  // TODO Auto-generated method stub
Socket s=new Socket("27.190.171.122",8881);
  FileInputStream fi=new FileInputStream("d\\Water lilies.jpg");
  OutputStream out=s.getOutputStream();
  byte[] by=new byte[1024];
  int len=0;
  while((len=fi.read(by))!=-1){
   out.write(by, 0, len);
  }
  s.shutdownInput();
  InputStream in=s.getInputStream();
  byte[] by1=new byte[1024];
  int num=in.read(by1);
  System.out.println(new String(by1,0,num));
  fi.close();
  s.close();
  
}
}
/*
* 服务端*/
class PicServer{
public static void main(String[] args) throws IOException{
  ServerSocket ss=new ServerSocket(8881);
  Socket s=ss.accept();
  FileOutputStream fos=new FileOutputStream("d\\Water lilies cope.jpg");
  InputStream in=s.getInputStream();
  byte[] by=new byte[1024];
  int len=0;
while((len=in.read(by))!=-1){
   fos.write(by, 0, len);
  }

  OutputStream out=s.getOutputStream();
  out.write("上传成功".getBytes());
  ss.close();
  s.close();
  fos.close();
  
}
}
红色代码报错
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at UploadPic.main(UploadPic.java:22)

    ip地址是获取的本机地址(附件中是ip地址)

Address.jpg (158.97 KB, 下载次数: 14)

Address.jpg

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

1 个回复

倒序浏览
已经解决
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马