各自努力,最高处见!加油!
socket TCP 网络上传图片
服务器端代码
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class FileUploadingServer {
public static void main(String[] args) throws IOException {
byte[] buf=new byte[1024];
int readLen=0;
String filePath="D:\\Java_code\\InternetLearn\\src\\library.jpg";
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
ServerSocket serverSocket=new ServerSocket(8888);
System.out.println("服务端,在8888端口监听,等待连接。。。。");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
while ((readLen=inputStream.read(buf))!=-1)
{
fileOutputStream.write(buf,0,readLen);
}
System.out.println("服务器接收到图片!发送已接受指令给客户端!");
OutputStream outputStream = socket.getOutputStream();
outputStream.write("收到图片".getBytes());
inputStream.close();
fileOutputStream.close();
outputStream.close();
socket.close();
}
}
客户端代码
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class uploadingClient {
public static void main(String[] args) throws Exception {
//客户端读取本地的文件,传入输入流中
String filePath="E:\\library.jpg";
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));//为什么要这一步?
byte[] bytes = StreamUtils.streamToByteArray(bufferedInputStream);//调用题目所给的库,将输入流中的数据转换成字节数组。
Socket socket = new Socket(InetAddress.getLocalHost(),8888);//连接到本机的8888端口
OutputStream outputStream = socket.getOutputStream();//获取到一个字节流
outputStream.write(bytes);
socket.shutdownOutput();
System.out.println("客户端文件发送完毕,准备接收服务器端反馈!");
byte[] readBytes=new byte[1024];
InputStream inputStream = socket.getInputStream();
String s = StreamUtils.streamToString(inputStream);
System.out.println("服务器端回复:"+s);
bufferedInputStream.close();
outputStream.close();
inputStream.close();
socket.close();
}
}
工具类文件代码
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 此类用于演示关于流的读写方法
*
*/
public class StreamUtils {
/**
* 功能:将输入流转换成byte[]
* @param is
* @return
* @throws Exception
*/
public static byte[] streamToByteArray(InputStream is) throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象
byte[] b = new byte[1024];
int len;
while((len=is.read(b))!=-1){
bos.write(b, 0, len);
}
byte[] array = bos.toByteArray();
bos.close();
return array;
}
/**
* 功能:将InputStream转换成String
* @param is
* @return
* @throws Exception
*/
public static String streamToString(InputStream is) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder= new StringBuilder();
String line;
while((line=reader.readLine())!=null){ //当读取到 null时,就表示结束
builder.append(line+"\r\n");
}
return builder.toString();
}
}
netstat指令
- netstat-an可以查看当前主机网络情况,包括端口监听情况和网络连接情况
- netstat-an | more可以分页显示
- 要求在dos控制台下运行(win键+R快捷键,输入cmd命令行)
外部地址:连接该端口的外部程序ip地址。
状态:LISTENING监听中,ESTABLISHED(established)已连接
Ctrl+C退出