//客户端代码
import java.net.Socket;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.io.IOException;
import java.io.FileNotFoundException;
public class PicClient2
{
public static void main(String[] args) throws UnknownHostException,IOException,FileNotFoundException{
//1. 创建客户端socket。
Socket s = new Socket("192.168.1.110",10012);
//2. 读取客户端要上传的图片文件。
FileInputStream fis = new FileInputStream("1.PNG");
//3. 获取socket输出流,将读到图片数据发送给服务端。
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1){
out.write(buf,0,len);
}
//告诉服务端说:这边的数据发送完毕。让服务端停止读取。
s.shutdownOutput();
//读取服务端发回的内容。
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int lenIn = in.read(buf);
String text = new String(buf,0,lenIn);
System.out.println(text);
fis.close();
s.close();
}
}
//服务端代码
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.OutputStream;
class UploadTask implements Runnable
{
private Socket s;
public UploadTask(Socket s){
this.s = s;
}
public void run(){
int count = 0;
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "......connected");
try{
InputStream in = s.getInputStream();
File dir = new File("c:\\pic");
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(dir,ip + ".bmp");
//如果文件已经存在于服务端。
while(file.exists()){
file = new File(dir,ip + "(" + (++count) + ").bmp");
}
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf)) != -1){
fos.write(buf,0,len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public class PicServer2
{
public static void main(String[] args) throws FileNotFoundException,IOException {
ServerSocket ss = new ServerSocket(10012);
while(true){
Socket s = ss.accept();
new Thread(new UploadTask(s)).start();
}
//ss.close();
}
}
异常提醒:Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at PicClient2.main(PicClient2.java:14)
难道客户端第14行代码有问题?不知道如何解决。求大神指点
|
|