输入输出流核心代码
所有的文件传输都是靠流,其中文件复制最具代表性.输入流和输出流,从输入流中读取数据写入到输出流中。
InputStream in = 输入源; OutputStream os = 输出目的;
byte[] bytes = new byte[1024]; int len = 0; while((len = in.read(bytes))!=-1){ //读取到文件末尾的话可以读到-1 os.write(bytes,0,len); }
os.close(); in.close();
TCP图片上传客户端
1.Socket套接字连接服务器,指定ip和端口
Socket socket = Socket(String host, int port);
2.通过Socket获取字节输出流,向服务器写图片
OutputStream os = socket.getOutputStream();
3.创建输入流对象,读取图片数据源
FileInputStream fis = new FileInputStream(filePath);
4.读取图片,使用字节输出流,将图片写到服务器,采用字节数组进行缓冲
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1){
os.write(bytes,0,len);
}
socket.shutdownOutput();//文件读完时给服务器写终止序列
5.通过Socket套接字获取字节输入流,读取服务器发回来的上传成功
InputStream inputStream = socket.getInputStream();
len = inputStream.read(bytes);
System.out.println(“服务器返回:”+new String(bytes,0,len));
6.关闭资源
socket.close();
/** * 实现TCP图片上传客户端 * 实现步骤: * 1.Socket套接字连接服务器,指定ip和端口 * Socket socket = Socket(String host, int port); * 2.通过Socket获取字节输出流,写图片 * OutputStream os = socket.getOutputStream(); * 3.使用自己的流对象,读取图片数据源 * FileInputStream fis = new FileInputStream(filePath); * 4.读取图片,使用字节输出流,将图片写到服务器,采用字节数组进行缓冲 * int len = 0; * byte[] bytes = new byte[1024]; * while ((len = fis.read(bytes)) != -1){ * os.write(bytes,0,len); * } * 5.通过Socket套接字获取字节输入流,读取服务器发回来的上传成功 * InputStream inputStream = socket.getInputStream(); * len = inputStream.read(bytes); * System.out.println("服务器返回:"+new String(bytes,0,len)); * 6.关闭资源 * socket.close(); * * Created by Autumn on 2018/2/5. */ public class TCPClient { public static void main(String[] args) throws IOException { String ip = "127.0.0.1"; int port = 8000; File fileFolder = new File("D://Users//Autumn//Pictures"); uploadPic(ip,port,fileFolder+File.separator+"tiger.jpg"); uploadPic(ip,port,fileFolder+File.separator+"water.jpg"); uploadPic(ip,port,fileFolder+File.separator+"sunset.jpg"); } /** * 上传图片 * @param ip 服务器ip地址 * @param port 服务器端口号 * @param filePath 文件路径 * @throws IOException */ public static void uploadPic(String ip,int port,String filePath) throws IOException{ //创建客户端Socket Socket socket = new Socket(ip,port); //根据Socket获取字节输出流,用此流将图片写到服务器 OutputStream os = socket.getOutputStream(); //创建字节输入流,读取本机上的数据源图片 FileInputStream fis = new FileInputStream(filePath); //开始读写字节数组,从输入流中读取到输出流 int len = 0; byte[] bytes = new byte[1024]; while ((len = fis.read(bytes)) != -1){ os.write(bytes,0,len); } //文件读完时给服务器写终止序列 socket.shutdownOutput(); //获取字节输入流,读取服务器的上传成功 InputStream inputStream = socket.getInputStream(); len = inputStream.read(bytes); System.out.println("服务器返回:"+new String(bytes,0,len)); socket.close(); } }
TCP图片上传服务器端
1.ServerSocket套接字对象,监听8000
ServerSocket serverSocket = ServerSocket(int port);
2.方法accept()获取客户端的连接对象
Socket socket = serverSocket.accept();
3.客户端连接对象获取字节输入流,读取客户端发送图片
InputStream in = socket.getInputStream();
4.创建File对象,绑定上传文件夹。判断文件夹存在,不存在则创建文件夹
File upload = new File(“d://upload”);
if (!upload.exists()){
upload.mkdirs();
}
5.创建字节输出流,数据目的File对象所在文件夹
String fileName = “pic”+System.currentTimeMillis()+ new Random().nextInt(9999)+”.jpg”;
FileOutputStream fos = new FileOutputStream(upload+File.separator+fileName);
6.字节流读取图片,字节流将图片写入到目的文件中
byte[] bytes = new byte[1024];
int len = 0;
while((len = in.read(bytes))!=-1){ //读的是客户端发过来图片的字节数组,只有读文件能读到-1,所以这里永远读不到-1,read()会一直阻塞。(需要客户端发送结束标志)
fos.write(bytes,0,len);
}
7.将上传成功回写给客户端
socket.getOutputStream().write((“上传到服务器”+socket.getLocalAddress().toString()+”成功”).getBytes());
8.关闭资源
fos.close();
socket.close();
/** * 多线程接收图片 * Created by Autumn on 2018/2/5. */ public class TCPThreadServer { public static void main(String[] args) throws IOException { //服务端不用指定ip地址,只需指定端口号 ServerSocket serverSocket = new ServerSocket(8000); while(true){ //获得一个客户端Socket对象,开启一个线程 Socket socket = serverSocket.accept(); //线程阻塞 new Thread(new Upload(socket)).start(); } } } /** * TCP图片上传服务器 * 1.ServerSocket套接字对象,监听8000 * ServerSocket serverSocket = ServerSocket(int port); * 2.方法accept()获取客户端的连接对象 * Socket socket = serverSocket.accept(); * 3.客户端连接对象获取字节输入流,读取客户端发送图片 * InputStream in = socket.getInputStream(); * 4.创建File对象,绑定上传文件夹。判断文件夹存在,不存在则创建文件夹 * File upload = new File("d://upload"); * if (!upload.exists()){ * upload.mkdirs(); * } * 5.创建字节输出流,数据目的File对象所在文件夹 * String fileName = "pic"+System.currentTimeMillis()+ new Random().nextInt(9999)+".jpg"; * FileOutputStream fos = new FileOutputStream(upload+File.separator+fileName); * 6.字节流读取图片,字节流将图片写入到目的文件中 * byte[] bytes = new byte[1024]; * int len = 0; * while((len = in.read(bytes))!=-1){ //读的是客户端发过来图片的字节数组,只有读文件能读到1,所以永远读不到-1 * fos.write(bytes,0,len); * } * 7.将上传成功回写给客户端 * socket.getOutputStream().write(("上传到服务器"+socket.getLocalAddress().toString()+"成功").getBytes()); * 8.关闭资源 * fos.close(); * socket.close(); * Created by Autumn on 2018/2/5. */ public class Upload implements Runnable { private Socket socket; public Upload(Socket socket){ this.socket = socket; } @Override public void run() { try { //获取输入流 InputStream in = socket.getInputStream(); //创建文件夹 File upload = new File("d://upload"); if (!upload.exists()){ upload.mkdirs(); } String fileName = "pic"+System.currentTimeMillis()+ new Random().nextInt(9999)+".jpg"; //创建字节输出流,将图片写入到目的文件夹 upload:d://upload FileOutputStream fos = new FileOutputStream(upload+File.separator+fileName); //读写字节数组 byte[] bytes = new byte[1024]; int len = 0; while((len = in.read(bytes))!=-1){ //读的是客户端发过来图片的字节数组,只有读文件能读到1,所以永远读不到-1 fos.write(bytes,0,len); } System.out.println("成功接收来自"+socket.getInetAddress()+socket.getPort()+"的图片!"); //通过客户端连接对象获取字节输出流 //上传成功写会客户端 socket.getOutputStream().write(("上传到服务器"+socket.getLocalAddress().toString()+"成功").getBytes()); fos.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/17825.html