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地址)
|
|