java网络发送图片
来源:百度知道 编辑:UC知道 时间:2024/09/12 22:11:22
FileClient:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class FileClient extends Thread {
private Socket socket;
private InetAddress ip;
private int port;
File file;
public FileClient(File file,InetAddress ip,int port){
this.file = file;
this.ip = ip;
this.port = port;
socket = null;
}
public void run() {
try {
socket = new Socket(ip,port);
FileInputStream fis = new FileInputStream(file);
byte[] buff = new byte[fis.available()];
fis.read(buff);
OutputStream os = socket.getOutputStream();
os.write(buff);
os.flush();
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Fi