输入输出流、字符字节流、NIO

news2024/9/20 10:52:41

1、对输入输出流、字符字节流的学习,以之前做的批量下载功能为例

批量下载指的是,将多个文件打包到zip文件中,然后下载该zip文件。

1.1下载网络上的文件

代码参考如下:

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MultiDownloadByUrlTest {
    public static void main(String[] args) throws IOException {
        //如果zip文件不存在则创建zip
        String localZipFile = "D:/temp/testUrl.zip" ;
        File file = new File(localZipFile);
        if(!file.exists()){
            file.createNewFile();
        }
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(localZipFile));
        byte[] buffer = new byte[1024];
        //要批量下载的文件数组
        String[] urls = new String[] {"http://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png",
                "https://img-home.csdnimg.cn/images/20201124032511.png"};
        //依次获取批量下载的文件
        for(int i =0; i<urls.length;i++){
            //从数据库中获取文件的路径和文件名,并放入zip文件中
            String urlFile = urls[i];
            out.putNextEntry(new ZipEntry(i+".png"));
            int len;
            URL url = new URL(urlFile);
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            //读入需要下载的文件的内容,打包到zip文件
            while ((len = inStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            inStream.close();
        }
        out.close();
    }

}

1.2、下载磁盘中的多个文件到zip文件中

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MultiDownloadTest {
    public static void main(String[] args) throws IOException {
        //如果zip文件不存在则创建zip
        String localZipFile = "D:/temp/test.zip" ;
        File file = new File(localZipFile);
        if(!file.exists()){
            file.createNewFile();
        }
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(localZipFile));
        //要批量下载的文件数组
        String[] ids = new String[] {"11.docx","22.xlsx"};
        byte[] buffer = new byte[1024];
        //依次获取批量下载的文件
        for(int i =0; i<ids.length;i++){
            String fileName = ids[i];
            out.putNextEntry(new ZipEntry(fileName));
            int len;
            FileInputStream inStream = new FileInputStream(new File("D:/temp/"+fileName));
            //读入需要下载的文件的内容,打包到zip文件
            while ((len = inStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            inStream.close();
        }
        out.close();
    }

}

1.3编写接口,下载该zip文件

Controller类代码如下:

package com.hmblogs.backend.controller;

import com.hmblogs.backend.util.MultiDownloadTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@Slf4j
public class FileController {

    /**
     * get方式下载zip文件
     * @return
     */
    @GetMapping(value = "/downloadZipByGet")
    public void downloadZipByGet(HttpServletRequest request,
                                 HttpServletResponse response) throws IOException {
        log.info("downloadZipByGet prepare begin.");
        MultiDownloadTest.preMultiDownload();
        log.info("downloadZipByGet prepare end.");
        log.info("downloadZipByGet begin.");
        String zipFileName = "D:/temp/test.zip";
        InputStream is = new FileInputStream(new File(zipFileName));
        // 设置response参数,可以打开下载页面
        response.reset();
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setHeader("Access-Control-Expose-Headers", "content-disposition");
        response.setHeader("Content-Disposition", "attachment;filename=test.zip");
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
            log.info("downloadZipByGet end.");
        } catch (final IOException e) {
            log.error("downloadZipByGet errors, reason:{}", e.getMessage());
        } finally {
            if (bis != null){
                bis.close();
            }
            if (is != null){
                is.close();
            }
            if (bos != null){
                bos.close();
            }
            if (out != null){
                out.close();
            }
        }
        // 用后删除临时用途的zip文件
        File fileTempZip = new File(zipFileName);
        if(fileTempZip.exists()){
            fileTempZip.delete();
        }
    }

    /**
     * post方式下载zip文件
     * @return
     */
    @RequestMapping(value = "/downloadZipByPost",method = RequestMethod.POST)
    public void downloadZipByPost(){

    }

}

工具类MultiDownloadTest.java代码如下:

package com.hmblogs.backend.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MultiDownloadTest {
    public static void preMultiDownload() throws IOException {
        //如果zip文件不存在则创建zip
        String localZipFile = "D:/temp/test.zip" ;
        File file = new File(localZipFile);
        if(!file.exists()){
            file.createNewFile();
        }
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(localZipFile));
        //要批量下载的文件数组
        String[] ids = new String[] {"11.docx","22.xlsx"};
        byte[] buffer = new byte[1024];
        //依次获取批量下载的文件
        for(int i =0; i<ids.length;i++){
            String fileName = ids[i];
            out.putNextEntry(new ZipEntry(fileName));
            int len;
            FileInputStream inStream = new FileInputStream(new File("D:/temp/"+fileName));
            //读入需要下载的文件的内容,打包到zip文件
            while ((len = inStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            inStream.close();
        }
        out.close();
    }

}

1.4访问接口

http://localhost:8080/backend/downloadZipByGet

下载了文件,能正常打开,且文件都是正常的。

二、学习NIO

参考Java之NIO基本简介_java_脚本之家

2.1开发一个简单的服务端接收客户端发过来的消息的功能

服务端Server.java代码如下:


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
public class Server {
    public static void main(String[] args) {
        try {
            //1.获取管道
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            //2.设置非阻塞模式
            serverSocketChannel.configureBlocking(false);
            //3.绑定端口
            serverSocketChannel.bind(new InetSocketAddress(8888));
            //4.获取选择器
            Selector selector = Selector.open();
            //5.将通道注册到选择器上,并且开始指定监听的接收事件
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            //6.轮询已经就绪的事件
            while (selector.select() > 0){
                System.out.println("开启事件处理");
                //7.获取选择器中所有注册的通道中已准备好的事件
                Iterator<SelectionKey> it = selector.selectedKeys().iterator();
                //8.开始遍历事件
                while (it.hasNext()){
                    SelectionKey selectionKey = it.next();

                    //9.判断这个事件具体是啥
                    if (selectionKey.isAcceptable()){
                        System.out.println("isAcceptable--->"+selectionKey);
                        //10.获取当前接入事件的客户端通道
                        SocketChannel socketChannel = serverSocketChannel.accept();
                        //11.切换成非阻塞模式
                        socketChannel.configureBlocking(false);
                        //12.将本客户端注册到选择器
                        socketChannel.register(selector,SelectionKey.OP_READ);
                    }else if (selectionKey.isReadable()){
                        System.out.println("isReadable--->"+selectionKey);
                        //13.获取当前选择器上的读
                        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                        //14.读取
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
                        int len;
                        while ((len = socketChannel.read(buffer)) > 0){
                            buffer.flip();
                            System.out.println(new String(buffer.array(),0,len));
                            //清除之前的数据(覆盖写入)
                            buffer.clear();
                        }
                    }
                    //15.处理完毕后,移除当前事件
                    it.remove();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端Client.java代码如下:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
public class Client {
    public static void main(String[] args) {
        try {
            SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",8888));
            socketChannel.configureBlocking(false);
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.print("请输入:");
                String msg = scanner.nextLine();
                buffer.put(msg.getBytes());
                buffer.flip();
                socketChannel.write(buffer);
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行Server

然后运行Client

在Client输入wwww

然后看到Server打印如下内容:

开启事件处理
isAcceptable--->sun.nio.ch.SelectionKeyImpl@42110406
开启事件处理
isReadable--->sun.nio.ch.SelectionKeyImpl@531d72ca
wwww

可以看到, 这时先开启了isAcceptable事件处理,然后开启了isReadable事件处理。

然后再在Client输入qqqq

然后看到Server打印如下内容:

开启事件处理
isReadable--->sun.nio.ch.SelectionKeyImpl@531d72ca
qqqq

这时只开启了isReadable这样的事件处理,不用再开启isAcceptable事件处理

server控制台截图如下

client的控制台如下: 

2.2开发网络编程应用实例-群聊系统

需求:进一步理解 NIO 非阻塞网络编程机制,实现多人群聊

 编写一个 NIO 群聊系统,实现客户端与客户端的通信需求(非阻塞)

服务器端:可以监测用户上线,离线,并实现消息转发功能

客户端:通过 channel 可以无阻塞发送消息给其它所有客户端用户,同时可以接受其它客户端用户通过服务端转发来的消息

Server.java代码如下:


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
/**
 *
 */
public class Server {
    //定义属性
    private Selector selector;
    private ServerSocketChannel ssChannel;
    private static final int PORT = 9999;
    //构造器
    //初始化工作
    public Server() {
        try {
            // 1、获取通道
            ssChannel = ServerSocketChannel.open();
            // 2、切换为非阻塞模式
            ssChannel.configureBlocking(false);
            // 3、绑定连接的端口
            ssChannel.bind(new InetSocketAddress(PORT));
            // 4、获取选择器Selector
            selector = Selector.open();
            // 5、将通道都注册到选择器上去,并且开始指定监听接收事件
            ssChannel.register(selector , SelectionKey.OP_ACCEPT);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    //监听
    public void listen() {
        System.out.println("监听线程:" + Thread.currentThread().getName());
        try {
            while (selector.select() > 0){
                // 7、获取选择器中的所有注册的通道中已经就绪好的事件
                Iterator<SelectionKey> it = selector.selectedKeys().iterator();
                // 8、开始遍历这些准备好的事件
                while (it.hasNext()){
                    // 提取当前这个事件
                    SelectionKey sk = it.next();
                    // 9、判断这个事件具体是什么
                    if(sk.isAcceptable()){
                        // 10、直接获取当前接入的客户端通道
                        SocketChannel schannel = ssChannel.accept();
                        // 11 、切换成非阻塞模式
                        schannel.configureBlocking(false);
                        // 12、将本客户端通道注册到选择器
                        System.out.println(schannel.getRemoteAddress() + " 上线 ");
                        schannel.register(selector , SelectionKey.OP_READ);
                        //提示
                    }else if(sk.isReadable()){
                        //处理读 (专门写方法..)
                        readData(sk);
                    }
                    it.remove(); // 处理完毕之后需要移除当前事件
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            //发生异常处理....
        }
    }
    //读取客户端消息
    private void readData(SelectionKey key) {
        //获取关联的channel
        SocketChannel channel = null;
        try {
            //得到channel
            channel = (SocketChannel) key.channel();
            //创建buffer
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            int count = channel.read(buffer);
            //根据count的值做处理
            if(count > 0) {
                //把缓存区的数据转成字符串
                String msg = new String(buffer.array());
                //输出该消息
                System.out.println("来自客户端---> " + msg);
                //向其它的客户端转发消息(去掉自己), 专门写一个方法来处理
                sendInfoToOtherClients(msg, channel);
            }
        }catch (IOException e) {
            try {
                System.out.println(channel.getRemoteAddress() + " 离线了..");
                e.printStackTrace();
                //取消注册
                key.cancel();
                //关闭通道
                channel.close();
            }catch (IOException e2) {
                e2.printStackTrace();;
            }
        }
    }
    //转发消息给其它客户(通道)
    private void sendInfoToOtherClients(String msg, SocketChannel self ) throws  IOException{
        System.out.println("服务器转发消息中...");
        System.out.println("服务器转发数据给客户端线程: " + Thread.currentThread().getName());
        //遍历 所有注册到selector 上的 SocketChannel,并排除 self
        for(SelectionKey key: selector.keys()) {
            //通过 key  取出对应的 SocketChannel
            Channel targetChannel = key.channel();
            //排除自己
            if(targetChannel instanceof  SocketChannel && targetChannel != self) {
                //转型
                SocketChannel dest = (SocketChannel)targetChannel;
                //将msg 存储到buffer
                ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
                //将buffer 的数据写入 通道
                dest.write(buffer);
            }
        }
    }
    public static void main(String[] args) {
        //创建服务器对象
        Server groupChatServer = new Server();
        groupChatServer.listen();
    }
}

Client.java代码如下:


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
public class Client {
    //定义相关的属性
    private final String HOST = "127.0.0.1"; // 服务器的ip
    private final int PORT = 9999; //服务器端口
    private Selector selector;
    private SocketChannel socketChannel;
    private String username;
    //构造器, 完成初始化工作
    public Client() throws IOException {
        selector = Selector.open();
        //连接服务器
        socketChannel = socketChannel.open(new InetSocketAddress("127.0.0.1", PORT));
        //设置非阻塞
        socketChannel.configureBlocking(false);
        //将channel 注册到selector
        socketChannel.register(selector, SelectionKey.OP_READ);
        //得到username
        username = socketChannel.getLocalAddress().toString().substring(1);
        System.out.println(username + " is ok...");
    }
    //向服务器发送消息
    public void sendInfo(String info) {
        info = username + " 说:" + info;
        try {
            socketChannel.write(ByteBuffer.wrap(info.getBytes()));
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    //读取从服务器端回复的消息
    public void readInfo() {
        try {
            int readChannels = selector.select();
            if(readChannels > 0) {//有可以用的通道
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    if(key.isReadable()) {
                        //得到相关的通道
                        SocketChannel sc = (SocketChannel) key.channel();
                        //得到一个Buffer
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
                        //读取
                        sc.read(buffer);
                        //把读到的缓冲区的数据转成字符串
                        String msg = new String(buffer.array());
                        System.out.println(msg.trim());
                    }
                }
                iterator.remove(); //删除当前的selectionKey, 防止重复操作
            } else {
                //System.out.println("没有可以用的通道...");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        //启动我们客户端
        Client chatClient = new Client();
        //启动一个线程, 每个3秒,读取从服务器发送数据
        new Thread() {
            public void run() {
                while (true) {
                    chatClient.readInfo();
                    try {
                        Thread.currentThread().sleep(3000);
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        //发送数据给服务器端
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            System.out.print("请输入:");
            String s = scanner.nextLine();
            chatClient.sendInfo(s);
        }
    }
}

Client22.java代码如下:就是Client.java的代码复制一份,改下文件名而已。


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

public class Client22 {
    //定义相关的属性
    private final String HOST = "127.0.0.1"; // 服务器的ip
    private final int PORT = 9999; //服务器端口
    private Selector selector;
    private SocketChannel socketChannel;
    private String username;
    //构造器, 完成初始化工作
    public Client22() throws IOException {
        selector = Selector.open();
        //连接服务器
        socketChannel = socketChannel.open(new InetSocketAddress("127.0.0.1", PORT));
        //设置非阻塞
        socketChannel.configureBlocking(false);
        //将channel 注册到selector
        socketChannel.register(selector, SelectionKey.OP_READ);
        //得到username
        username = socketChannel.getLocalAddress().toString().substring(1);
        System.out.println(username + " is ok...");
    }
    //向服务器发送消息
    public void sendInfo(String info) {
        info = username + " 说:" + info;
        try {
            socketChannel.write(ByteBuffer.wrap(info.getBytes()));
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    //读取从服务器端回复的消息
    public void readInfo() {
        try {
            int readChannels = selector.select();
            if(readChannels > 0) {//有可以用的通道
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    if(key.isReadable()) {
                        //得到相关的通道
                        SocketChannel sc = (SocketChannel) key.channel();
                        //得到一个Buffer
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
                        //读取
                        sc.read(buffer);
                        //把读到的缓冲区的数据转成字符串
                        String msg = new String(buffer.array());
                        System.out.println(msg.trim());
                    }
                }
                iterator.remove(); //删除当前的selectionKey, 防止重复操作
            } else {
                //System.out.println("没有可以用的通道...");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        //启动我们客户端
        Client22 chatClient = new Client22();
        //启动一个线程, 每个3秒,读取从服务器发送数据
        new Thread() {
            public void run() {
                while (true) {
                    chatClient.readInfo();
                    try {
                        Thread.currentThread().sleep(3000);
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        //发送数据给服务器端
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            System.out.print("请输入:");
            String s = scanner.nextLine();
            chatClient.sendInfo(s);
        }
    }
}

启动Server、Client、Client22后,在Client输入内容、在Client22输入内容后,对3个控制台截图如下

Server控制台截图如下:

Client控制台如下:

Client22控制台如下:

 

简单的需求目的达到了。 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1366337.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

TS 36.211 V12.0.0-下行(8)-调制和上变频

本文的内容主要涉及TS 36.211&#xff0c;版本是C00&#xff0c;也就是V12.0.0。

2023 年精选:每个 DevOps 团队都应该了解的 5 种微服务设计模式

微服务彻底改变了应用程序开发世界&#xff0c;将大型整体系统分解为更小、更易于管理的组件。这种架构风格的特点是独立、松散耦合的服务&#xff0c;带来了从可扩展性、模块化到更高的灵活性等众多优势。 DevOps 团队如何最好地利用这种方法来实现最高效率&#xff1f;答案在…

小游戏实战丨基于PyGame的贪吃蛇小游戏

文章目录 写在前面PyGame贪吃蛇注意事项系列文章写在后面 写在前面 本期内容&#xff1a;基于pygame的贪吃蛇小游戏 下载地址&#xff1a;https://download.csdn.net/download/m0_68111267/88700188 实验环境 python3.11及以上pycharmpygame 安装pygame的命令&#xff1a;…

【Spring Cloud 】进阶之Config配置中心

目录 config大致的一个思路&#xff1a; 二&#xff0c;前期准备 2.1导入依赖 2.2编写bootstrop.yml&#xff1a; 三&#xff0c;编写Controller类 3.1获取单个配置类信息 3.2获取多个配置类信息 &#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f3…

【开源GPT项目 - 在问】让知识无界,智能触手可及

Chatanywhere: chatAnywhere 在问 | 让知识无界&#xff0c;智能触手可及 项目简介 这是一个免费的在线聊天工具&#xff0c;旨在让用户更方便地享受科技带来的便利。用户可以使用我们的工具来获取答案、寻求建议、进行翻译和计算等等。这是由一位个人开发者创建的&#xff…

MyBatisPlus学习一:快速入门

前言 前面快速学习了Mybatis&#xff0c;现在开始快速学习MyBatisPlus 学习教程&#xff1a; 黑马mybatis教程全套视频教程&#xff0c;2天Mybatis框架从入门到精通 黑马程序员最新MybatisPlus全套视频教程&#xff0c;4小时快速精通mybatis-plus框架 简介 MyBatisPlus 是…

python对常见的激活函数绘图操作(详细代码讲解)

写论文的时候需要做一些激活函数的图像&#xff0c;为此将常见的激活函数进行整理汇总了一下&#xff0c;方便后续的复习 激活函数的作用是为让模型处理非线性问题&#xff0c;故次激活函数都是非线性的 生活中&#xff0c;非线性问题占大多数&#xff0c;而模型的训练通常都是…

Fiddler工具 — 9.命令行和状态栏

1、命令行 命令行在Fiddler的左下方的黑色窗口&#xff0c;也叫QuickExec&#xff0c;可以调用 Fiddler的内置命令。 这一系列内置的函数用于筛选和操作会话列表中的session&#xff08;会话&#xff09;。 虽然它不是很显眼&#xff0c;但用好它&#xff0c;会让你的工作效率…

听GPT 讲Rust源代码--compiler(40)

File: rust/compiler/rustc_borrowck/src/region_infer/mod.rs 文件rust/compiler/rustc_borrowck/src/region_infer/mod.rs是Rust编译器中用于区域推断的模块文件。该文件中定义了一些类型和枚举&#xff0c;用于帮助编译器分析和推断代码中的生命周期和借用关系&#xff0c;以…

【霹雳吧啦】手把手带你入门语义分割の番外13:U2-Net 源码讲解(PyTorch)—— 损失的计算

目录 前言 Preparation 一、U2-Net 网络结构图 二、U2-Net 网络源代码 1、损失计算 2、model.py 3、train_and_eval.py 附&#xff1a;train_and_eval.py 源代码 前言 文章性质&#xff1a;学习笔记 &#x1f4d6; 视频教程&#xff1a;U2-Net 源码讲解&#xff08;PyT…

HTML JavaScript 康威生命游戏

<!DOCTYPE html> <html> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>康威生命游戏</title><style>body {font-family: Arial, sa…

CompletableFuture超详解与实践

0.背景 一个接口可能需要调用 N 个其他服务的接口&#xff0c;这在项目开发中还是挺常见的。举个例子&#xff1a;用户请求获取订单信息&#xff0c;可能需要调用用户信息、商品详情、物流信息、商品推荐等接口&#xff0c;最后再汇总数据统一返回。 如果是串行&#xff08;按…

SpringBoot项目的三种创建方式

手动创建方式&#xff1a; ①&#xff1a;新建maven项目 ②&#xff1a;引入依赖 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.10.RELEASE</version>&l…

近似的同态比较:简单多项式的迭代计算

参考文献&#xff1a; [Gold64] Goldschmidt R E. Applications of division by convergence[D]. Massachusetts Institute of Technology, 1964.[CKKLL19] Cheon J H, Kim D, Kim D, et al. Numerical method for comparison on homomorphically encrypted numbers[C]//Inter…

普通人开视频号小店可以吗?

我是电商珠珠 我做电商也有五六年时间了&#xff0c;天猫、快手、抖店我都做过。 其中做抖店的时间最长&#xff0c;三年的时间&#xff0c;让我从个人化做到了团队化。 在22年的时候&#xff0c;发现了视频号小店这个项目&#xff0c;并开始投入进去。 一开始&#xff0c;…

Flask:模板渲染

本文章只作为个人笔记. 文章目录 前言 一、模板渲染 二、效果 前言 模板渲染 一、模板渲染 from flask import Flask, render_templateapp Flask(__name__)app.route(/) def hello_world():return render_template("index.html")app.route("/blog/<blog_…

java中常见的一些小知识(1)

1.数组转List 1.1. Arrays.asList public class Tesr {public static void main(String[] args) {String[] ary new String[]{ "1", "a"};List<String> list Arrays.asList((ary));list.add("ddsdsa");System.out.println(list);}}但是…

【EI会议征稿通知】第六届信息科学、电气与自动化工程国际学术会议(ISEAE 2024)

第六届信息科学、电气与自动化工程国际学术会议&#xff08;ISEAE 2024&#xff09; 2024 6th International Conference on Information Science, Electrical and Automation Engineering 第六届信息科学、电气与自动化工程国际学术会议&#xff08;ISEAE 2024&#xff09;定…

代码训练营Day.27 | 39. 组合总和、40. 组合总和II、131. 分割回文串

39. 组合总和 1. LeetCode链接 . - 力扣&#xff08;LeetCode&#xff09; 2. 题目描述 3. 解法 与其他组合总和题目不同的是&#xff0c;这一次数组中的数字可以重复使用。 回溯&#xff1a; 1. 参数和返回值。参数&#xff1a;数组、遍历起点、目标值。 2. 终止条件。…

Prometheus Blackbox_exporter笔记

一、安装Promtheus 在 Prometheus 官网 Download | Prometheus 获取适用于 Linux 的 Prometheus 安 装包&#xff0c;这里我选择最新的 2.46.0 版本&#xff0c;我是 Linux 系统&#xff0c;选择下载 prometheus-2.46.0.linux-amd64.tar.gz 下载安装包&#xff1a; wget htt…