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/>
</ 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>
< 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 :
end-point :
access-key-id :
access-key-secret :
bucket-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 "" ;
}
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 {
@Bean
public RestTemplate restTemplate ( ClientHttpRequestFactory requestFactory) {
return new RestTemplate ( requestFactory) ;
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory ( ) {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory ( ) ;
factory. setReadTimeout ( 10000 ) ;
factory. setConnectTimeout ( 10000 ) ;
factory. setHttpClient ( httpClient ( ) ) ;
return factory;
}
@Bean
public 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 ) ;
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 {
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 {
@Resource
private AliOssProperties aliOssProperties;
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 "" ;
}
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;
}
private String getShortUrl ( String url) {
String [ ] imgUrls = url. split ( "\\?" ) ;
return imgUrls[ 0 ] . trim ( ) ;
}
private void uploadFile2Oss ( String fileDir, InputStream inputStream, String fileName) {
OSS ossClient = new OSSClientBuilder ( ) . build ( aliOssProperties. getEndpoint ( ) , aliOssProperties. getAccessKeyId ( ) , aliOssProperties. getAccessKeySecret ( ) ) ;
String ret;
try {
ObjectMetadata 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 ( ) ;
}
}
}
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 {
@Autowired
private RestTemplate restTemplate;
@Autowired
private OssComponent ossComponent;
@Override
public 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) ;
}
}
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 {
@Autowired
private FileService fileService;
@GetMapping ( "/uploadFileToOss" )
public Object uploadJavaVideo ( String url) {
try {
return fileService. uploadJavaVideo ( url) ;
} catch ( Exception e) {
log. error ( "上传转码异常,异常原因e:{}" , e) ;
}
return null ;
}
}
启动服务 用 postman 请求