java中的网络信息传输方式基于TCP协议和UD协议P的,socket是基于TCP协议的实现代码如下:
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ClientTcpSend {
public static void main(String[] args) {
int length = 0;
byte[] sendBytes = null;
Socket socket = null;
DataOutputStream dos = null;
FileInputStream fis = null;
try {
try {
socket = new Socket();
socket.connect(new InetSocketAddress("127.0.0.1", 33456),
dos = new DataOutputStream(socket.getOutputStream());
File file = new File("/root/6674541037_c3a9c8b64c_b.jpg");
Ffis = new FileInputStream(file);
sendBytes = new byte[1024];
while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
dos.write(sendBytes, 0, length);
dos.flush();
}
} finally {
if (dos != null)
dos.close();
if (fis != null)
fis.close();
if (socket != null)
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
转载于:https://www.csdn.net/tags/OtTakg1sOTcwMTUtYmxvZwO0O0OO0O0O.html