【Java】java实现文件上传和下载(上传到指定路径/数据库/minio)

       

目录

上传到指定路径

一、代码层级结构

二、文件上传接口

三、使用postman进行测试;

MultipartFile接收前端传递的文件:127.0.0.1:8082/path/uploadFile

part接收前端传递的文件:127.0.0.1:8082/path/uploadFileByRequest

接收前端传递binary 类型:127.0.0.1:8082/path/upload2

四、springboot 配置

五、文件下载接口

六、使用postman进行测试

上传至数据库

一、代码层级结构

二、文件上传接口

三、文件下载接口

四、使用postman进行测试

上传至Minio

一、代码层级结构

二、文件上传/下载接口

MinioUtils

三、使用postman进行测试


        业务分析:实际开发过程中,我们经常对文件的上传和下载的功能实现,所以这也是一个程序员应该掌握的基本开发能力。所以下面我给大家分享一下文件上传和下载的三种方式,分别将文件上传到指定路径下/本地数据库/minio当中。

       所有代码都以上传至压缩包资源,可以自行免费进行下载测试;

上传到指定路径

一、代码层级结构

        

二、文件上传接口

/**
 * 上传到指定路径下
 */
@RestController
@Slf4j
@RequestMapping("/path")
public class UploadController {

    @Autowired
    private ResourceLoader resourceLoader;


    private BufferedOutputStream bufferedOutputStream = null;


    /**
     * form-data 类型
     * form-data 类型即常用的表单提交
     * 两种处理参数的方式
     * <p>
     * MultipartFile 类接受前台传过来的文件
     * part 接收字节流
     */
    @PostMapping("/uploadFile")
    public String uploadFile(@RequestParam("name") String name, @RequestPart("file1") MultipartFile file1, @RequestPart("file2") MultipartFile[] file2) throws IOException, ServletException {


        // 获取项目部署路径
       /* String appPath = request.getServletContext().getRealPath("/");
        // 构建上传文件的目录路径
        String path = appPath + "static/upload/";*/

        //绝对路劲
//        String path = "D:\\Users\\MXIN\\IdeaProjects\\springboot-uploadanddownload\\src\\main\\resources\\static\\";
        //相对路径
        String path = "src/main/resources/static/";

        //前端传递多个file(只对file2进行处理)
        for (MultipartFile multipartFile : file2) {
//            使用 MultipartFile 字节流保存文件
            fileUtil(multipartFile, String.valueOf(path));
        }
        fileUtil(file1, String.valueOf(path));

        return "success";
    }

    /**
     * part 接收字节流
     */
    @PostMapping("/uploadFileByRequest")
    public String uploadFileByRequest(HttpServletRequest request) throws IOException, ServletException {
        // 获取项目部署路径
       /* String appPath = request.getServletContext().getRealPath("/");
        // 构建上传文件的目录路径
        String path = appPath + "static/upload/";*/

        //绝对路劲
        String path = "D:\\Users\\MXIN\\IdeaProjects\\springboot-uploadanddownload\\src\\main\\resources\\static\\";


//		使用 Part 接收文件字节流
//        Part file1 = request.getPart("file1");
//        file1.write(path + file1.getSubmittedFileName());

        // request.getParts() 获取的是全部参数(name,age,file1,file2),包括文件参数和非文件参数
        for (Part part : request.getParts()) {
            // 获取文件类型
            String contentType = part.getContentType();
            // 获取文件大小
            long size = part.getSize();
            // 获取文件名
            String submittedFileName = part.getSubmittedFileName();
           
            if (part.getContentType() != null) {
                //如果是文件会进行写入
                part.write(path + part.getSubmittedFileName());
            } else {
                // 如果是参数会获取参数(根据实际需求对参数进行处理)
                // 获取参数名 
                String name1 = part.getName();
            }
        }
        return "success";
    }


    public String fileUtil(MultipartFile file, String path) {

        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(
                        new File(path + file.getOriginalFilename())));
                bufferedOutputStream.write(bytes);
                bufferedOutputStream.close();
                return file.getOriginalFilename() + "上传成功";
            } catch (Exception e) {
                return file.getOriginalFilename() + "上传失败,错误信息为:" + e;
            }
        } else {
            return "上传得文件为空";
        }
    }


}

三、使用postman进行测试;

MultipartFile接收前端传递的文件:127.0.0.1:8082/path/uploadFile

测试结果

part接收前端传递的文件:127.0.0.1:8082/path/uploadFileByRequest

测试结果

 

四、上传文件大小设置

        由于springboot默认上传文件大小为1M,单个请求最大为10M,当文件超过1M会报错。所以可以通过配置文件限制文件上传大小。

上传大于1M报错信息为:

Resolved [org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded]

所以修改配置文件(根据自己实际开发需求进行修改):

上述简单的介绍了文件上传到指定路径下的方法,下面来简单介绍一下文件下载是如何实现的。

五、文件下载接口

/**
 * 下载指定路径下的文件
 * */
@RestController
@Slf4j
@RequestMapping("/path")
public class DownloadController {

    /**
     * 文件下载 isOnline默认为false
     */
    @GetMapping("/download")
    public void download(String fileName, HttpServletResponse response, boolean isOnLine) throws IOException {
        // 路径可以指定当前项目相对路径
        File file = new File("D:\\Users\\Mixi\\IdeaProjects\\springboot-uploadanddownload\\src\\main\\resources\\static\\" + fileName);
        if (file.exists()) {
            FileInputStream fileInputStream = new FileInputStream(file);
            ServletOutputStream outputStream = response.getOutputStream();

            // 获取文件扩展名
            String extension = fileName.substring(fileName.lastIndexOf(".") + 1);

            if (!isOnLine) {
                // 根据文件扩展名设置Content-Type
                String contentType = getContentType(extension);
                response.setContentType(contentType);

                // 如果文件名为中文需要设置编码
                response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "utf8"));
                // 返回前端文件名需要添加
                response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            }
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
        }
    }

    // 根据文件扩展名获取Content-Type
    private String getContentType(String extension) {
        if ("jpg".equalsIgnoreCase(extension) || "jpeg".equalsIgnoreCase(extension)) {
            return "image/jpeg";
        } else if ("png".equalsIgnoreCase(extension)) {
            return "image/png";
        } else if ("gif".equalsIgnoreCase(extension)) {
            return "image/gif";
        } else if ("txt".equalsIgnoreCase(extension)) {
            return "text/plain";
        } else if ("pdf".equalsIgnoreCase(extension)) {
            return "application/pdf";
        } else if ("doc".equalsIgnoreCase(extension) || "docx".equalsIgnoreCase(extension)) {
            return "application/msword";
        } else if ("xls".equalsIgnoreCase(extension) || "xlsx".equalsIgnoreCase(extension)) {
            return "application/vnd.ms-excel";
        } else if ("ppt".equalsIgnoreCase(extension) || "pptx".equalsIgnoreCase(extension)) {
            return "application/vnd.ms-powerpoint";
        } else if ("zip".equalsIgnoreCase(extension)) {
            return "application/zip";
        } else if ("tar".equalsIgnoreCase(extension)) {
            return "application/x-tar";
        } else if ("rar".equalsIgnoreCase(extension)) {
            return "application/x-rar-compressed";
        } else if ("gz".equalsIgnoreCase(extension)) {
            return "application/gzip";
        } else {
            return "application/octet-stream";
        }
    }
}

六、使用postman进行测试

isOnlie为true和false决定了是否在浏览器上在线查看。

上传至数据库

这种业务需求就是用于项目开发过程中,文件上传下载的功能用的很少,避免搭建部署文件存储的服务器,简化了部署和管理,节约成本资源。

一、代码层级结构

二、文件上传接口

UploadToDBController:

/**
 * 上传到数据库
 * */
@RestController
@Slf4j
@RequestMapping("/db")
public class UploadToDBController {

    @Autowired
    private FilesService filesService;


    @PostMapping("/uploadFile")
    public Files inputFile(@RequestParam("file") MultipartFile file) {

        Files files = new Files();
        if (null != file) {
            String name = file.getOriginalFilename();
            byte[] bytes;
            try {
                bytes = file.getBytes();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            files.setName(name);
            files.setFile(bytes);
            filesService.save(files);
        }
        return files;
    }
}

FilesServiceImpl:

    @Override
    public void save(Files files) {
        filesMapper.insert(files);
    }

简单代码的增删改查我就不写了哈。

三、文件下载接口

DownloadFromDBController:

/**
 * 从数据库下载
 */
@RestController
@Slf4j
@RequestMapping("/db")
public class DownloadFromDBController {

    @Autowired
    private FilesService filesService;

    @GetMapping("/download/{id}")
    public void download(HttpServletResponse response, @PathVariable("id") Integer id) {
        filesService.download(response,id);
    }
}

FilesServiceImpl:

 @Override
    public HttpServletResponse download(HttpServletResponse response, Integer id) {
        try {
            byte[] buf;
            Files byId = filesMapper.selectById(id);
            if (byId == null) {
                return response;
            }
            buf = byId.getFile();
            String suffix = FileToMultipartFile.getSuffix(byId.getName());
            String contentType = "";
            switch (Objects.requireNonNull(FileTypeEnum.getEnum(suffix))) {
                case DOC:
                    contentType = "application/msword";
                    break;
                case DOCX:
                    contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    break;
                case PDF:
                    contentType = "application/powerpoint";
                    break;
                case JPE:
                case JPG:
                case JPEG:
                    contentType = "image/jpeg";
                    break;
                case PNG:
                    contentType = "image/png";
                    break;
                case ZIP:
                    contentType = "application/zip";
                    break;
                case TAR:
                    contentType = "application/x-tar";
                    break;
                case GZ:
                    contentType = "application/x-gzip";
                    break;
                case XLS:
                    contentType = "application/vnd.ms-excel";
                    break;
                case XLSX:
                    contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    break;
                case PPT:
                    contentType = "application/vnd.ms-powerpoint";
                    break;
                case PPTX:
                    contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                    break;
            }

            // 清空
            response.reset();
            String encodedFileName = URLEncoder.encode(byId.getName().replaceAll(" ", "+"), "UTF-8");
            // 设置header
            response.addHeader("Content-Disposition", "attachment;filename=" + encodedFileName);
//            response.addHeader("Content-Length", "" + byId.getFileNo());
            response.setCharacterEncoding("UTF-8");
            response.setContentType(contentType);
            response.getOutputStream().write(buf);
            // 强制输出,不然会留在内存里丢失掉
            response.getOutputStream().flush();
            return response;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

四、使用postman进行测试

文件上传:

测试结果数据库:

文件下载:

 测试结果:

上传至Minio

        首先要在本地或者服务器上部署minio并启动才可以进行文件的上传和下载,还未安装minio的可以参考一下这篇文章:【Docker】手把手教你在windows使用Docker安装Minio[详细教程]_minio windows-CSDN博客

一、代码层级结构

二、文件上传/下载接口

/**
 * 文件上传、下载、删除、获取文件信息、获取文件url接口
 * */
@Slf4j
@RestController
@RequestMapping("/oss")
public class MinioController {

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private MinioConfig minioConfig;

    /**
     * 文件上传
     *
     * @param file
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        try {
            //文件名
            String fileName = file.getOriginalFilename();
            String newFileName = System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");
            //类型
            String contentType = file.getContentType();
            minioUtils.uploadFile(minioConfig.getBucketName(), file, newFileName, contentType);
            return "上传成功,文件名:" + newFileName;
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }
    }

    /**
     * 删除
     *
     * @param fileName
     */
    @DeleteMapping("/")
    public void delete(@RequestParam("fileName") String fileName) {
        minioUtils.removeFile(minioConfig.getBucketName(), fileName);
    }

    /**
     * 获取文件信息
     *
     * @param fileName
     * @return
     */
    @GetMapping("/info")
    public String getFileStatusInfo(@RequestParam("fileName") String fileName) {
        return minioUtils.getFileStatusInfo(minioConfig.getBucketName(), fileName);
    }

    /**
     * 获取文件外链
     *
     * @param fileName
     * @return
     */
    @GetMapping("/url")
    public String getPresignedObjectUrl(@RequestParam("fileName") String fileName) {
        return minioUtils.getPresignedObjectUrl(minioConfig.getBucketName(), fileName);
    }

    /**
     * 文件下载
     *
     * @param fileName
     * @param response
     */
    @GetMapping("/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        try {
            InputStream fileInputStream = minioUtils.getObject(minioConfig.getBucketName(), fileName);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
            IOUtils.copy(fileInputStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载失败");
        }
    }
}

MinioUtils

@Slf4j
@Component
public class MinioUtils {

    @Autowired
    private MinioClient minioClient;


    /**
     * 启动SpringBoot容器的时候初始化Bucket
     * 如果没有Bucket则创建
     *
     * @param bucketName
     */
    public void createBucket(String bucketName) {
        try {
            if (!bucketExists(bucketName)) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                log.info("创建bucketName = {}完成!", bucketName);
                return;
            }
            log.info("bucketName = {}已存在!策略为:{}", bucketName, getBucketPolicy(bucketName));
        } catch (Exception e) {
            log.error("创建bucketName = {}异常!e = {}", bucketName, e);
        }
    }

    /**
     * 判断Bucket是否存在,true:存在,false:不存在
     *
     * @param bucketName
     * @return
     */
    @SneakyThrows
    public boolean bucketExists(String bucketName) {
        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    }

    /**
     * 获得Bucket的策略
     *
     * @param bucketName
     * @return
     */
    @SneakyThrows
    public String getBucketPolicy(String bucketName) {
        return minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build());
    }

    /**
     * 获得所有Bucket列表
     *
     * @return
     */
    @SneakyThrows
    public List<Bucket> getAllBuckets() {
        return minioClient.listBuckets();
    }

    /**
     * 根据bucketName获取其相关信息
     *
     * @param bucketName
     * @return
     */
    @SneakyThrows(Exception.class)
    public Optional<Bucket> getBucket(String bucketName) {
        return getAllBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
    }

    /**
     * 根据bucketName删除Bucket,true:删除成功; false:删除失败,文件或已不存在
     *
     * @param bucketName
     * @throws Exception
     */
    @SneakyThrows(Exception.class)
    public void removeBucket(String bucketName) {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }


    /**
     * 判断文件是否存在
     *
     * @param bucketName
     * @param objectName
     * @return
     */
    public boolean isObjectExist(String bucketName, String objectName) {
        boolean exist = true;
        try {
            minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
        } catch (Exception e) {
            log.error("[Minio工具类]>>>> 判断文件是否存在, 异常:", e);
            exist = false;
        }
        return exist;
    }

    /**
     * 判断文件夹是否存在
     *
     * @param bucketName
     * @param objectName
     * @return
     */
    public boolean isFolderExist(String bucketName, String objectName) {
        boolean exist = false;
        try {
            Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(objectName).recursive(false).build());
            for (Result<Item> result : results) {
                Item item = result.get();
                if (item.isDir() && objectName.equals(item.objectName())) {
                    exist = true;
                }
            }
        } catch (Exception e) {
            log.error("[Minio工具类]>>>> 判断文件夹是否存在,异常:", e);
            exist = false;
        }
        return exist;
    }

    /**
     * 根据文件前置查询文件
     *
     * @param bucketName 存储桶
     * @param prefix     前缀
     * @param recursive  是否使用递归查询
     * @return MinioItem 列表
     */
    @SneakyThrows(Exception.class)
    public List<Item> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
        List<Item> list = new ArrayList<>();
        Iterable<Result<Item>> objectsIterator = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build());
        if (objectsIterator != null) {
            for (Result<Item> o : objectsIterator) {
                Item item = o.get();
                list.add(item);
            }
        }
        return list;
    }

    /**
     * 获取文件流
     *
     * @param bucketName 存储桶
     * @param objectName 文件名
     * @return 二进制流
     */
    @SneakyThrows(Exception.class)
    public InputStream getObject(String bucketName, String objectName) {
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    /**
     * 断点下载
     *
     * @param bucketName 存储桶
     * @param objectName 文件名称
     * @param offset     起始字节的位置
     * @param length     要读取的长度
     * @return 二进制流
     */
    @SneakyThrows(Exception.class)
    public InputStream getObject(String bucketName, String objectName, long offset, long length) {
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).offset(offset).length(length).build());
    }

    /**
     * 获取路径下文件列表
     *
     * @param bucketName 存储桶
     * @param prefix     文件名称
     * @param recursive  是否递归查找,false:模拟文件夹结构查找
     * @return 二进制流
     */
    public Iterable<Result<Item>> listObjects(String bucketName, String prefix, boolean recursive) {
        return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build());
    }

    /**
     * 使用MultipartFile进行文件上传
     *
     * @param bucketName  存储桶
     * @param file        文件名
     * @param objectName  对象名
     * @param contentType 类型
     * @return
     */
    @SneakyThrows(Exception.class)
    public ObjectWriteResponse uploadFile(String bucketName, MultipartFile file, String objectName, String contentType) {
        InputStream inputStream = file.getInputStream();
        return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).contentType(contentType).stream(inputStream, inputStream.available(), -1).build());
    }

    /**
     * 图片上传
     *
     * @param bucketName
     * @param imageBase64
     * @param imageName
     * @return
     */
    public ObjectWriteResponse uploadImage(String bucketName, String imageBase64, String imageName) {
        if (!StringUtils.isEmpty(imageBase64)) {
            InputStream in = base64ToInputStream(imageBase64);
            String newName = System.currentTimeMillis() + "_" + imageName + ".jpg";
            String year = String.valueOf(new Date().getYear());
            String month = String.valueOf(new Date().getMonth());
            return uploadFile(bucketName, year + "/" + month + "/" + newName, in);

        }
        return null;
    }

    public static InputStream base64ToInputStream(String base64) {
        ByteArrayInputStream stream = null;
        try {
            byte[] bytes = Base64.getEncoder().encode(base64.trim().getBytes());
            stream = new ByteArrayInputStream(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stream;
    }


    /**
     * 上传本地文件
     *
     * @param bucketName 存储桶
     * @param objectName 对象名称
     * @param fileName   本地文件路径
     * @return
     */
    @SneakyThrows(Exception.class)
    public ObjectWriteResponse uploadFile(String bucketName, String objectName, String fileName) {
        return minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build());
    }

    /**
     * 通过流上传文件
     *
     * @param bucketName  存储桶
     * @param objectName  文件对象
     * @param inputStream 文件流
     * @return
     */
    @SneakyThrows(Exception.class)
    public ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream) {
        return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, inputStream.available(), -1).build());
    }

    /**
     * 创建文件夹或目录
     *
     * @param bucketName 存储桶
     * @param objectName 目录路径
     * @return
     */
    @SneakyThrows(Exception.class)
    public ObjectWriteResponse createDir(String bucketName, String objectName) {
        return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(new ByteArrayInputStream(new byte[]{}), 0, -1).build());
    }

    /**
     * 获取文件信息, 如果抛出异常则说明文件不存在
     *
     * @param bucketName 存储桶
     * @param objectName 文件名称
     * @return
     */
    @SneakyThrows(Exception.class)
    public String getFileStatusInfo(String bucketName, String objectName) {
        return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()).toString();
    }

    /**
     * 拷贝文件
     *
     * @param bucketName    存储桶
     * @param objectName    文件名
     * @param srcBucketName 目标存储桶
     * @param srcObjectName 目标文件名
     */
    @SneakyThrows(Exception.class)
    public ObjectWriteResponse copyFile(String bucketName, String objectName, String srcBucketName, String srcObjectName) {
        return minioClient.copyObject(CopyObjectArgs.builder().source(CopySource.builder().bucket(bucketName).object(objectName).build()).bucket(srcBucketName).object(srcObjectName).build());
    }

    /**
     * 删除文件
     *
     * @param bucketName 存储桶
     * @param objectName 文件名称
     */
    @SneakyThrows(Exception.class)
    public void removeFile(String bucketName, String objectName) {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    /**
     * 批量删除文件
     *
     * @param bucketName 存储桶
     * @param keys       需要删除的文件列表
     * @return
     */
    public void removeFiles(String bucketName, List<String> keys) {
        List<DeleteObject> objects = new LinkedList<>();
        keys.forEach(s -> {
            objects.add(new DeleteObject(s));
            try {
                removeFile(bucketName, s);
            } catch (Exception e) {
                log.error("[Minio工具类]>>>> 批量删除文件,异常:", e);
            }
        });
    }

    /**
     * 获取文件外链
     *
     * @param bucketName 存储桶
     * @param objectName 文件名
     * @param expires    过期时间 <=7 秒 (外链有效时间(单位:秒))
     * @return url
     */
    @SneakyThrows(Exception.class)
    public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) {
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().expiry(expires).bucket(bucketName).object(objectName).build();
        return minioClient.getPresignedObjectUrl(args);
    }

    /**
     * 获得文件外链
     *
     * @param bucketName
     * @param objectName
     * @return url
     */
    @SneakyThrows(Exception.class)
    public String getPresignedObjectUrl(String bucketName, String objectName) {
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName).method(Method.GET).build();
        return minioClient.getPresignedObjectUrl(args);
    }

    /**
     * 将URLDecoder编码转成UTF8
     *
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public String getUtf8ByURLDecoder(String str) throws UnsupportedEncodingException {
        String url = str.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
        return URLDecoder.decode(url, "UTF-8");
    }
}

三、使用postman进行测试

文件上传到minio

测试结果:

从minio中下载文件

 

直接下载即可,这样就完成了从minio中下载指定文件,还有部分接口,如果感兴趣的小伙伴可以自行测试看下实际效果。

        综上所有的代码就简单的介绍了一下Java实现文件上传和下载,希望能给你们实际开发带来一定的帮助,如果有出现问题的地方,希望指出,便于后续修改。

所有源码均以上传至https://download.csdn.net/download/m0_64210833/89233948

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

基于大模型的智能案件询问系统

一、数据库层面 1、document表 这个表是用来存储文件信息的。具体字段含义如下&#xff1a; 1. id&#xff1a;文件的唯一标识&#xff0c;整型&#xff0c;自增。 2. name&#xff1a;文件名称&#xff0c;字符串类型&#xff0c;最大长度为255个字符。 3. type&#xff1a…

宠物领养|基于SprinBoot+vue的宠物领养管理系统(源码+数据库+文档)

宠物领养目录 基于Spring Boot的宠物领养系统的设计与实现 一、前言 二、系统设计 三、系统功能设计 1前台 1.1 宠物领养 1.2 宠物认领 1.3 教学视频 2后台 2.1宠物领养管理 2.2 宠物领养审核管理 2.3 宠物认领管理 2.4 宠物认领审核管理 2.5 教学视频管理 四、…

Linux专栏03:使用Xshell远程连接云服务器

博客主页&#xff1a;Duck Bro 博客主页系列专栏&#xff1a;Linux专栏关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ 使用Xshell远程连接云服务器 编号&#xff1a;03 文章目录 使用Xsh…

Windows中Redis安装配置

一&#xff0c;下载 Redis官网 Redis中文网 Redis的Github资源 安装 更改资源路径及添加环境变量 添加防火墙异常 设置最大缓存 三、验证redis安装是否成功 redis-cli

TiDB系列之:部署TiDB集群常见报错解决方法

TiDB系列之&#xff1a;部署TiDB集群常见报错解决方法 一、部署TiDB集群二、unsupported filesystem ext3三、soft limit of nofile四、THP is enabled五、numactl not usable六、net.ipv4.tcp_syncookies 1七、service irqbalance not found,八、登陆TiDB数据库 一、部署TiDB…

RTSP,RTP,RTCP

机器学习 Machine Learning&#xff08;ML&#xff09; 深度学习&#xff08;DL&#xff0c;Deep Learning&#xff09; CV计算机视觉&#xff08;computer vision&#xff09; FFMPEG&#xff0c;MPEG2-TS,H.264,H.265,AAC rstp,rtp,rtmp,webrtc onvif,gb28181 最详细的音…

Rust中的并发性:Sync 和 Send Traits

在并发的世界中&#xff0c;最常见的并发安全问题就是数据竞争&#xff0c;也就是两个线程同时对一个变量进行读写操作。但当你在 Safe Rust 中写出有数据竞争的代码时&#xff0c;编译器会直接拒绝编译。那么它是靠什么魔法做到的呢&#xff1f; 这就不得不谈 Send 和 Sync 这…

排序算法大总结

引言 排序算法&#xff08;sorting algorithm&#xff09;是用于对一组数据按照特定顺序进行排列。排序算法有着广泛的应用&#xff0c;因为有序数据通常能够被更高效地查找、分析和处理。 如图 1-1 所示&#xff0c;排序算法中的数据类型可以是整数、浮点数、字符或字符串等…

SpringBoot中实现发送邮件

概要 在Spring Boot中发送电子邮件相对简单。你可以使用Spring的邮件支持来实现这一点。 步骤&#xff1a; 1.添加依赖&#xff1a;首先&#xff0c;需要在你的pom.xml文件中添加Spring Boot的邮件发送器依赖。 2. 配置邮件服务器&#xff1a;在application.properties或app…

Python 机器学习 基础 之 学习 基础环境搭建

Python 机器学习 基础 之 学习 基础环境搭建 目录 Python 机器学习 基础 之 学习 基础环境搭建 一、简单介绍 二、什么是机器学习 三、python 环境的搭建 1、Python 安装包下载 2、这里以 下载 Python 3.10.9 为例 3、安装 Python 3.10.9 4、检验 python 是否安装成功&…

设计模式第二次测试 | 数据库连接池设计(原型模式、创建者模式、适配器模式)

需求中文如下&#xff1a;原本是英文&#xff0c;用百度翻译转换而来 我们需要设计一个工具&#xff0c;它负责创建一个与数据库软件MySQL的连接池。 连接池中有数百个连接可供客户端使用。 所有连接对象都有相同的内容&#xff0c;但它们是不同的对象。 连接对象的创建是资源密…

【嵌入式笔试题】进程线程笔试题

非常经典的笔试题。 1.进程&线程(16道) 1.1异步IO和同步IO区别? 答案:如果是同步IO,当一个IO操作执行时,应用程序必须等待,直到此IO执行完。 相反,异步IO操作在后台运行,IO操作和应用程序可以同时运行,提高系统性能,提 高IO流量。 解读:在同步文件IO中,线…

Sarcasm detection论文解析 |用于微博讽刺检测的上下文增强卷积神经网络

论文地址 论文地址&#xff1a;Context-augmented convolutional neural networks for twitter sarcasm detection - ScienceDirect 论文首页 笔记大纲 用于微博讽刺检测的上下文增强卷积神经网络 &#x1f4c5;出版年份:2018 &#x1f4d6;出版期刊:Neurocomputing &#x1f…

Django后台项目开发实战五

完成两个功能&#xff1a; HR 可以维护候选人信息面试官可以录入面试反馈 第五阶段 创建 interview 应用&#xff0c;实现候选人面试评估表的增删改功能&#xff0c;并且按照页面分组来展示不同的内容&#xff0c;如候选人基础信息&#xff0c;一面&#xff0c;二面的面试结…

在离线环境中将 CentOS 7.5 原地升级并迁移至 RHEL 7.9

《OpenShift / RHEL / DevSecOps 汇总目录》 说明 本文将说明如何在离线环境中将 CentOS 7.5 升级并迁移至 RHEL 7.9。为了简化准备过程&#xff0c;本文前面将在在线环境中安装用到的各种所需验证软件&#xff0c;而在后面升级迁移的时候再切换到由 ISO 构成的离线 Yum Repo…

20240430,类模板案例-数组类封装,STL初识,STRING容器(构造函数,赋值)

我真的碎掉了&#xff0c;主要是我很缺那点钱啊现在&#xff0c;我真的碎掉了我碎掉了碎掉了碎掉了 0.8 类模板案例-数组类封装 需求&#xff1a;1&#xff0c;存储&#xff1a;内置和自定义数据类型&#xff1b; 2&#xff0c;存到堆区&#xff1b; 3&#xff0c;构造函数传入…

Docker部署RabbitMQ与简单使用

官网地址&#xff1a; Messaging that just works — RabbitMQ 我的Docker博客:Docker-CSDN博客 1.结构 其中包含几个概念&#xff1a; **publisher**&#xff1a;生产者&#xff0c;也就是发送消息的一方 **consumer**&#xff1a;消费者&#xff0c;也就是消费消息的一方 …

第三节课,功能2:开发后端用户的管理接口--http client -- debug测试

一、idea 中 Http client 使用 二、测试步骤&#xff0c;先进入主程序 2.1 先run &#xff0c;再debug 2.2 再进入想要测试的代码 2.2.1 进入测试的接口 三、程序逻辑 1&#xff09;用户注册逻辑&#xff1a;如果用户不存在再后端&#xff0c;看用户名&密码&校验码是…

一些优雅的监控运维技巧

准备工作 安装 sysstat sudo apt install sysstat查看某个进程的cpu情况 pidstst -u -p 256432查看某个进程的RAM情况 pidstst -r -p 256432查看某个进程的IO情况 pidstst -d -p 256432查看某个进程下的线程执行情况 pidstst -t -p 256432查看指定PID的进程对应的可执行文件…

ip ssl证书无限端口网站

IP SSL证书是由CA认证机构颁发的一种特殊数字证书。大部分SSL数字证书都需要用户使用域名进行申请&#xff0c;想要对公网IP地址加密实现https访问就需要申请IP SSL证书。IP SSL证书采用了强大的加密算法&#xff0c;可以有效地防止数据在传输过程中被窃取或篡改&#xff0c;具…
最新文章