springboot 通过url下载文件并上传到OSS

DEMO流程

  • 传入一个需要下载并上传的url地址
  • 下载文件
  • 上传文件并返回OSS的url地址

springboot pom文件依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.15</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>springboot-rocketmq</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-demo</name><description>springboot-demo</description><properties><java.version>11</java.version><rocketmq-client-java-version>5.1.3</rocketmq-client-java-version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.22</version></dependency><!-- 阿里云oss --><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.13.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

application.yml 文件配置

ali:oss:#oss end-pointend-point: #oss access-key-idaccess-key-id: #oss access-key-secretaccess-key-secret: #oss bucket-namebucket-name: ali-url: https://${ali.oss.bucket-name}.${ali.oss.end-point}/

FileUtil 工具类


import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;import java.io.File;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class FileUtil {private static final String projectUrl = System.getProperty("user.dir").replaceAll("\\\\", "/");public static void deleteFiles(String path) {File file = new File(path);if (file.exists()) {if (file.isDirectory()) {File[] temp = file.listFiles(); //获取该文件夹下的所有文件for (File value : temp) {deleteFile(value.getAbsolutePath());}} else {file.delete(); //删除子文件}file.delete(); //删除文件夹}}public static void deleteFile(String path){File dest = new File(path);if (dest.isFile() && dest.exists()) {dest.delete();}}public static String getNewFileRootPath(){return projectUrl+File.separator+ IdUtil.simpleUUID();}public static String getFileNameFromUrl(String url) {Pattern pattern = Pattern.compile("[^/]*$");Matcher matcher = pattern.matcher(url);if (matcher.find()) {return matcher.group();}return "";}/*** 获取扩展名* @param urlPath* @return {@link String}*/public static String getExtName(String urlPath) {String fileName = getFileNameFromUrl(urlPath);return FileNameUtil.extName(fileName);}
}

请求配置 RestTemplateConfig


import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory requestFactory) {return new RestTemplate(requestFactory);}@Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory() {HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();factory.setReadTimeout(10000);factory.setConnectTimeout(10000);factory.setHttpClient(httpClient());return factory;}/*** @return*/@Beanpublic HttpClient httpClient() {Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);//设置整个连接池最大连接数connectionManager.setMaxTotal(500);//MaxPerRoute路由是对maxTotal的细分,每个主机的并发,这里route指的是域名connectionManager.setDefaultMaxPerRoute(200);RequestConfig requestConfig = RequestConfig.custom()//返回数据的超时时间.setSocketTimeout(20000)//连接上服务器的超时时间.setConnectTimeout(10000)//从连接池中获取连接的超时时间.setConnectionRequestTimeout(1000).build();return HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).build();}
}

阿里组件配置

读取配置类 AliOssProperties

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "ali.oss")
@Data
public class AliOssProperties {/*** OSS配置信息*/private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;private String aliUrl;
}

OSS组件类 OssComponent


import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;@Component
@Slf4j
@Getter
public class OssComponent {@Resourceprivate AliOssProperties aliOssProperties;/* -----------------对外功能---------------- *//*** 单个文件上传(指定文件名(带后缀))** @param inputStream 文件* @param fileName    文件名(带后缀)* @return 返回完整URL地址*/public String uploadFile(String fileDir, InputStream inputStream, String fileName) {try {this.uploadFile2Oss(fileDir, inputStream, fileName);String url = getFileUrl(fileDir, fileName);if (url != null && url.length() > 0) {return url;}} catch (Exception e) {e.printStackTrace();throw new RuntimeException("获取路径失败");}return "";}/*** 通过文件名获取文完整件路径** @param fileUrl 文件名* @return 完整URL路径*/public String getFileUrl(String fileDir, String fileUrl) {if (fileUrl != null && fileUrl.length() > 0) {String[] split = fileUrl.replaceAll("\\\\","/").split("/");String url = aliOssProperties.getAliUrl() + fileDir + split[split.length - 1];return Objects.requireNonNull(url);}return null;}public boolean deleteFile(String fileDir, String fileName) {OSS ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());// 删除文件ossClient.deleteObject(aliOssProperties.getBucketName(), fileDir + fileName);// 判断文件是否存在boolean found = ossClient.doesObjectExist(aliOssProperties.getBucketName(), fileDir + fileName);// 如果文件存在则删除失败return !found;}/* -----------内部辅助功能------------------------ *//*** 获取去掉参数的完整路径** @param url URL* @return 去掉参数的URL*/private String getShortUrl(String url) {String[] imgUrls = url.split("\\?");return imgUrls[0].trim();}/*** 上传文件(指定文件名)** @param inputStream 输入流* @param fileName    文件名*/private void uploadFile2Oss(String fileDir, InputStream inputStream, String fileName) {OSS ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());String ret;try {//创建上传Object的MetadataObjectMetadata objectMetadata = new ObjectMetadata();objectMetadata.setContentLength(inputStream.available());objectMetadata.setCacheControl("no-cache");objectMetadata.setHeader("Pragma", "no-cache");String contentType = getContentType(fileName.substring(fileName.lastIndexOf(".")));if(StrUtil.isNotEmpty(contentType)){objectMetadata.setContentType(contentType);}objectMetadata.setContentDisposition("inline;filename=" + fileName);//上传文件PutObjectResult putResult = ossClient.putObject(aliOssProperties.getBucketName(), fileDir + fileName, inputStream, objectMetadata);ret = putResult.getETag();if (StrUtil.isEmpty(ret)) {log.error("上传失败,文件ETag为空");}ossClient.shutdown();} catch (IOException e) {log.error(e.getMessage(), e);} finally {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}/*** 请求类型** @param filenameExtension :* @return :*/private static String getContentType(String filenameExtension) {if (FileNameSuffixEnum.BMP.getSuffix().equalsIgnoreCase(filenameExtension)) {return "image/bmp";}if (FileNameSuffixEnum.GIF.getSuffix().equalsIgnoreCase(filenameExtension)) {return "image/gif";}if (FileNameSuffixEnum.JPEG.getSuffix().equalsIgnoreCase(filenameExtension) ||FileNameSuffixEnum.JPG.getSuffix().equalsIgnoreCase(filenameExtension) ||FileNameSuffixEnum.PNG.getSuffix().equalsIgnoreCase(filenameExtension)) {return "image/jpeg";}if (FileNameSuffixEnum.HTML.getSuffix().equalsIgnoreCase(filenameExtension)) {return "text/html";}if (FileNameSuffixEnum.TXT.getSuffix().equalsIgnoreCase(filenameExtension)) {return "text/plain";}if (FileNameSuffixEnum.VSD.getSuffix().equalsIgnoreCase(filenameExtension)) {return "application/vnd.visio";}if (FileNameSuffixEnum.PPTX.getSuffix().equalsIgnoreCase(filenameExtension) ||FileNameSuffixEnum.PPT.getSuffix().equalsIgnoreCase(filenameExtension)) {return "application/vnd.ms-powerpoint";}if (FileNameSuffixEnum.DOCX.getSuffix().equalsIgnoreCase(filenameExtension) ||FileNameSuffixEnum.DOC.getSuffix().equalsIgnoreCase(filenameExtension)) {return "application/msword";}if (FileNameSuffixEnum.XML.getSuffix().equalsIgnoreCase(filenameExtension)) {return "text/xml";}if (FileNameSuffixEnum.PDF.getSuffix().equalsIgnoreCase(filenameExtension)) {return "application/pdf";}return "";}}@Getter
enum FileNameSuffixEnum {/*** 文件后缀名*/BMP(".bmp", "bmp文件"),GIF(".gif", "gif文件"),JPEG(".jpeg", "jpeg文件"),JPG(".jpg", "jpg文件"),PNG(".png", "png文件"),HTML(".html", "HTML文件"),TXT(".txt", "txt文件"),VSD(".vsd", "vsd文件"),PPTX(".pptx", "PPTX文件"),DOCX(".docx", "DOCX文件"),PPT(".ppt", "PPT文件"),DOC(".doc", "DOC文件"),XML(".xml", "XML文件"),PDF(".pdf", "PDF文件");/*** 后缀名*/private final String suffix;/*** 描述*/private final String description;FileNameSuffixEnum(String suffix, String description) {this.suffix = suffix;this.description = description;}
}

文件服务

FileService
public interface FileService {String uploadJavaVideo(String url) throws Exception;
}
FileServiceImpl

import cn.hutool.core.util.IdUtil;
import com.example.springbootrocketmq.config.OssComponent;
import com.example.springbootrocketmq.service.FileService;
import com.example.springbootrocketmq.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;@Slf4j
@Service
public class FileServiceImpl implements FileService {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate OssComponent ossComponent;@Overridepublic String uploadJavaVideo(String url) throws Exception {String extName = FileUtil.getExtName(url);//获取扩展名称String fileName = IdUtil.simpleUUID()+"."+extName;log.info("fileName:{}",fileName);String newFileRootPath = FileUtil.getNewFileRootPath();File rootFile = new File(newFileRootPath);if(!rootFile.exists()){rootFile.mkdirs();}String toPath = newFileRootPath+ File.separator + fileName;try {log.info("toPath:{}",toPath);uploadBigFile(url,toPath);return ossComponent.uploadFile("demo/",new FileInputStream(toPath),fileName);} finally {FileUtil.deleteFiles(newFileRootPath);}}/*** 下载文件* @param url* @param toPath* @throws Exception*/public void uploadBigFile(String url, String toPath) throws Exception {//定义请求头的接收类型RequestCallback requestCallback = request -> request.getHeaders().setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));//对响应进行流式处理而不是将其全部加载到内存中restTemplate.execute(url, HttpMethod.GET, requestCallback, clientHttpResponse -> {Files.copy(clientHttpResponse.getBody(), Paths.get(toPath));return null;});}
}

测试 TestController 类


import com.example.springbootrocketmq.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Slf4j
@RestController
@RequestMapping("/")
public class TestController {@Autowiredprivate FileService fileService;@GetMapping("/uploadFileToOss")public Object uploadJavaVideo(String url) {try {return fileService.uploadJavaVideo(url);}catch (Exception e){log.error("上传转码异常,异常原因e:{}",e);}return null;}
}

启动服务 用 postman 请求

在这里插入图片描述

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

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

相关文章

论文阅读:

来源&#xff1a;公众号看到一篇文章 原文&#xff1a;https://arxiv.org/pdf/2301.04275.pdf 代码&#xff1a;GitHub - fengluodb/LENet: LENet: Lightweight And Efficient LiDAR Semantic Segmentation Using Multi-Scale Convolution Attention 0、摘要 基于LiDAR的语义…

Linux:【Kafka四】集群介绍与单机搭建

目录 环境简介 一、搭建kafka集群 1.1、复制出两个kafka的配置文件 1.2、修改配置文件中的如下属性 二、启动kafka集群 三、可校验kafka三个节点是否均启动成功 四、查看集群中主题的分区和副本 4.1、新建一个包含了分区和副本的主题 4.2、查看该主题的详细信息 五、…

linux下安装ffmpeg的详细教程、ffmpeg is not installed

1、下载解压 wget http://www.ffmpeg.org/releases/ffmpeg-6.0.tar.gz tar -zxvf ffmpeg-6.0.tar.gz 2、 进入解压后目录,输入如下命令/usr/local/ffmpeg为自己指定的安装目录 cd ffmpeg-6.0 ./configure --prefix/usr/local/ffmpeg make sudo make install 3、配置变量 v…

kaggle新赛:写作质量预测大赛【数据挖掘】

赛题名称&#xff1a;Linking Writing Processes to Writing Quality 赛题链接&#xff1a;https://www.kaggle.com/competitions/linking-writing-processes-to-writing-quality 赛题背景 写作过程中存在复杂的行为动作和认知活动&#xff0c;不同作者可能采用不同的计划修…

Google zxing 生成带logo的二维码图片

环境准备 开发环境 JDK 1.8SpringBoot2.2.1Maven 3.2 开发工具 IntelliJ IDEAsmartGitNavicat15 添加maven配置 <dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.4.0</version> </…

thinkphp6 入门(8)-- Session

开启Session Session功能默认是没有开启的&#xff08;API应用通常不需要使用Session&#xff09; think\middleware\SessionInit// 添加引用 use think\facade\Session; 赋值 Session::set(name, thinkphp);取值 // 如果值不存在&#xff0c;返回null Session::get(name)…

机器学习-有监督学习-神经网络

目录 线性模型分类与回归感知机模型激活函数维度诅咒过拟合和欠拟合正则数据增强数值稳定性神经网络大家族CNNRNNGNN&#xff08;图神经网络&#xff09;GAN 线性模型 向量版本 y ⟨ w , x ⟩ b y \langle w, x \rangle b y⟨w,x⟩b 分类与回归 懂得两者区别激活函数&a…

Service Weaver:以单体形式编码,以微服务形式部署

分布式应用的主流架构模式演化为微服务架构已经有些年头了。微服务、DevOps、持续交付和容器技术(k8s)是构成最初云原生概念[1]的核心要素。它们相生相拌&#xff0c;共同演进&#xff0c;并推动了云计算全面进入云原生时代。 云原生应用普遍采用微服务架构&#xff0c;遗留的单…

C# Onnx Yolov8 Detect 涉黄检测

效果 项目 检测类别 代码 using Microsoft.ML.OnnxRuntime; using Microsoft.ML.OnnxRuntime.Tensors; using OpenCvSharp; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; usi…

【Linux】从零开始学习Linux基本指令(一)

&#x1f6a9;纸上得来终觉浅&#xff0c; 绝知此事要躬行。 &#x1f31f;主页&#xff1a;June-Frost &#x1f680;专栏&#xff1a;Linux入门 &#x1f525;该文章主要了解Linux操作系统下的基本指令。 目录&#xff1a; ⌛️指令的理解⏳目录和文件的理解⏳一些常见指令✉…

“小程序:改变电商行业的新趋势“

目录 引言1. 小程序的简介1.1 什么是小程序&#xff1f;1.2 小程序的优势 2. 小程序之电商演示1.注册微信小程序2.安装开发工具3.创建项目 3. 小程序之入门案例总结 引言 随着移动互联网的迅猛发展&#xff0c;小程序作为一种全新的应用形态&#xff0c;正在逐渐改变着传统电商…

鲲山科技:引入和鲸 ModelWhale,实现量化策略的高效迭代

量化投资是数据科学在金融行业的应用。 2023 年&#xff0c;量化行业的超额收益开始收敛&#xff0c;量化私募如何形成自身核心竞争力&#xff1f; 和鲸拜访客户鲲山科技&#xff08;深圳&#xff09;&#xff0c;揭示其“弯道超车”的独家秘诀。 群体作战 年初至今&#xff…

【软考-中级】系统集成项目管理工程师-配置管理历年案例

持续更新。。。。。。。。。。。。。。。 目录 2023 上 试题三(20分) 2023 上 试题三(20分) 某公司有自己的质量管理体系&#xff0c;其中配置管理程序已运行多年&#xff0c;由项目经理牵头组建变更控制委员会(CCB)&#xff0c;在创建配置管理环境后&#xff0c;并经过变更申请…

NewStarCTF 2023 公开赛道 WEEK2|Crypto

目录 T1.滴啤 T2.不止一个pi T3.halfcandecode T4.Rotate Xor T5.broadcast T6.partial decrypt T1.滴啤 下载题目附件&#xff0c;我们获得到以下代码。 from Crypto.Util.number import * import gmpy2 from flag import flag def gen_prime(number):p getPrime(numb…

跨行或跨列布局

关键点 1、float 实现 2、flex 实现 3、grid 实现效果预览: html: <div class="container"><h2>float 实现</h2>

10.12按键中断

设置按键中断&#xff0c;按键1按下&#xff0c;LED亮&#xff0c;再按一次&#xff0c;灭 按键2按下&#xff0c;蜂鸣器响。再按一次&#xff0c;不响 按键3按下&#xff0c;风扇转&#xff0c;再按一次&#xff0c;风扇停 keyit.h: #ifndef __KEYIT_H__ #define __KEYIT_…

Go语言介绍与安装

介绍与安装 本教程介绍了 Go&#xff0c;并讨论了选择 Go 相对于其他编程语言的优势。我们还将学习如何在Windows 中安装 Go。 介绍 Go也称为Golang&#xff0c;是由 Google 开发的一种开源、编译型、静态类型的编程语言。 Go创造背后的关键人物是Rob Pike、 Ken Thompson和…

Blender:渲染一个简单动画

接上 Blender&#xff1a;对模型着色_六月的翅膀的博客-CSDN博客 目标是做一个这种视频 先添加一个曲线&#xff0c;作为相机轨迹 然后添加一个相机 对相机添加物体约束&#xff0c;跟随路径&#xff0c;选择曲线&#xff0c;然后点击动画路径 假如对相机设置跟随路径后&…

Linux高性能服务器编程 学习笔记 第十三章 多进程编程

我们将讨论Linux多进程编程的以下内容&#xff1a; 1.复制进程映像的fork系统调用和替换进程映像的exec系列系统调用。 2.僵尸进程以及如何避免僵尸进程。 3.进程间通信&#xff08;Inter Process Communication&#xff0c;IPC&#xff09;最简单的方式&#xff1a;管道。 …

SSM - Springboot - MyBatis-Plus 全栈体系(二十六)

第六章 SpringBoot 快速启动框架&#xff1a;SpringBoot3 实战 一、SpringBoot3 介绍 1. SpringBoot3 简介 SpringBoot 版本&#xff1a;3.0.5 到目前为止已经学习了多种配置 Spring 程序的方式。但是无论使用 XML、注解、Java 配置类还是他们的混合用法&#xff0c;都会觉…