6.基于BIO形式的文件上传
2022/8/11 6:25:02
本文主要是介绍6.基于BIO形式的文件上传,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
如何再BIO下实现任意类型的文件传输呢?
1.服务端代码: public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); System.out.println("服务端启动,等待客户端连接...."); while (true) { Socket socket = ss.accept(); //重点1:一个上传任务启动一个线程处理 new ServerReaderThread(socket).start(); } } catch (IOException e) { e.printStackTrace(); } } } 2.服务端上传线程处理类: public class ServerReaderThread extends Thread { private Socket socket; public ServerReaderThread(Socket socket) { this.socket = socket; } @Override public void run() { try { System.out.println("服务端读取线程启动....."); //1.获取一个数据输入流读取客户端发送来的数据 DataInputStream dis=new DataInputStream(socket.getInputStream()); //2.读取客户端发来的文件类型 String suffix=dis.readUTF(); System.out.println("服务端读取到客户端发送来的文件类型:"+suffix); //3.定义一个字节输出管道负责将客户端发送来的数据写出去 OutputStream os=new FileOutputStream("C:\\Users\\24459\\Desktop\\server\\"+ UUID.randomUUID().toString()+suffix); byte[] buffer=new byte[1024]; int len; //读取次数 int i = 0; while ((len=dis.read(buffer))>0){ System.out.println("第"+(i++)+"次读取,读取长度:"+len); os.write(buffer,0,len); } os.close(); System.out.println("服务端读取结束!"); }catch (Exception e){ e.printStackTrace(); } } } 3.客户端 /** * 目标:实现客户端上传任意类型的文件数据到服务端保存起来 */ public class Client { public static void main(String[] args) { try ( InputStream is = new FileInputStream("C:\\Users\\24459\\Desktop\\client\\20200724194309755.png"); ) { System.out.println("客户端启动!"); Socket socket = new Socket("127.0.0.1", 8888); //2.将字节输出流包装称一个数据输出流 DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); //3.先发送一个字符串给服务端:这里可以先发送文件的后缀 dos.writeUTF(".png"); //4.用一个字节一个字节进行发送 byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) > 0) { Thread.sleep(2); dos.write(buffer, 0, len); } dos.flush(); System.out.println("客户端发送完毕!"); } catch (Exception e) { e.printStackTrace(); } } }
问题:
当客户端上传执行结束时,服务端抛出异常,并且上传的图片并不能打开!
这是什么原因呢??
如何改进呢?
/** * 目标:实现客户端上传任意类型的文件数据到服务端保存起来 */ public class Client { public static void main(String[] args) { try ( InputStream is = new FileInputStream("C:\\Users\\24459\\Desktop\\client\\20200724194309755.png"); ) { System.out.println("客户端启动!"); Socket socket = new Socket("127.0.0.1", 8888); //2.将字节输出流包装称一个数据输出流 DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); //3.先发送一个字符串给服务端:这里可以先发送文件的后缀 dos.writeUTF(".png"); //4.用一个字节一个字节进行发送 byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) > 0) { Thread.sleep(2); dos.write(buffer, 0, len); } dos.flush(); //重点:只需要在客户端发送完毕后,停止输出流! socket.shutdownOutput(); System.out.println("客户端发送完毕!"); } catch (Exception e) { e.printStackTrace(); } } } 这样服务端的判断: public void run() { try { .... 读取到的会是-1,从而读取完数据后,退出循环 while ((len=dis.read(buffer))>0){ System.out.println("第"+(i++)+"次读取,读取长度:"+len); os.write(buffer,0,len); } .... }catch (Exception e){ e.printStackTrace(); } }
这篇关于6.基于BIO形式的文件上传的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行