您的位置:首页 > 教育 > 培训 > seo外包公司费用_平面设计有几个软件_免费网站代理访问_百度推广有用吗

seo外包公司费用_平面设计有几个软件_免费网站代理访问_百度推广有用吗

2024/10/31 11:23:07 来源:https://blog.csdn.net/bronzehammer/article/details/143267592  浏览:    关键词:seo外包公司费用_平面设计有几个软件_免费网站代理访问_百度推广有用吗
seo外包公司费用_平面设计有几个软件_免费网站代理访问_百度推广有用吗

在Minio文件服务器部署成功后(参考上篇文章Minio文件服务器:安装)接下来我们通过SpringBoot框架写一个接口,来实现文件的上传功能:文件通过SpringBoot接口,上传到Minio文件服务器。并且,如果上传的文件是图片类型,也要实现能够预览上传后的图片。

1 完成后的项目结构

在这里插入图片描述

2 代码

2.1 MinioserverApplication.java

package com.example.minioserver;import com.example.minioserver.config.MinioProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;@EnableConfigurationProperties(value = {MinioProperties.class})
@SpringBootApplication
public class MinioserverApplication {public static void main(String[] args) {SpringApplication.run(MinioserverApplication.class, args);}}

2.2 MinioProperties.java

package com.example.minioserver.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;/*** @author: xbronze* @date: 2024-10-26 17:04* @description: TODO*/
@Data
@ConfigurationProperties(prefix="minio") //读取节点
public class MinioProperties {private String endpointUrl;private String accessKey;private String secreKey;private String bucketName;}

2.3 ViewContentType.java

package com.example.minioserver.enums;import cn.hutool.core.util.StrUtil;public enum ViewContentType {DEFAULT("default","application/octet-stream"),JPG("jpg", "image/jpeg"),TIFF("tiff", "image/tiff"),GIF("gif", "image/gif"),JFIF("jfif", "image/jpeg"),PNG("png", "image/png"),TIF("tif", "image/tiff"),ICO("ico", "image/x-icon"),JPEG("jpeg", "image/jpeg"),WBMP("wbmp", "image/vnd.wap.wbmp"),FAX("fax", "image/fax"),NET("net", "image/pnetvue"),JPE("jpe", "image/jpeg"),RP("rp", "image/vnd.rn-realpix");private String prefix;private String type;public static String getContentType(String prefix){if(StrUtil.isEmpty(prefix)){return DEFAULT.getType();}prefix = prefix.substring(prefix.lastIndexOf(".") + 1);for (ViewContentType value : ViewContentType.values()) {if(prefix.equalsIgnoreCase(value.getPrefix())){return value.getType();}}return DEFAULT.getType();}ViewContentType(String prefix, String type) {this.prefix = prefix;this.type = type;}public String getPrefix() {return prefix;}public String getType() {return type;}}

2.4 FileUploadService.java

package com.example.minioserver.service;import org.springframework.web.multipart.MultipartFile;/*** @author: xbronze* @date: 2024-10-26 16:59* @description: TODO*/
public interface FileUploadService {public String fileUpload(MultipartFile multipartFile);}

2.5 FileUploadServiceImpl.java

package com.example.minioserver.service.impl;import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.example.minioserver.config.MinioProperties;
import com.example.minioserver.enums.ViewContentType;
import com.example.minioserver.service.FileUploadService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.util.Date;/*** @author: xbronze* @date: 2024-10-26 17:00* @description: TODO*/
@Service
public class FileUploadServiceImpl implements FileUploadService {@Autowiredprivate MinioProperties minioProperties;@Overridepublic String fileUpload(MultipartFile multipartFile) {try {// 创建一个Minio的客户端对象MinioClient minioClient = MinioClient.builder().endpoint(minioProperties.getEndpointUrl()).credentials(minioProperties.getAccessKey(), minioProperties.getSecreKey()).build();// 判断桶是否存在boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());if (!found) {       // 如果不存在,那么此时就创建一个新的桶minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());} else {  // 如果存在打印信息System.out.println("Bucket " + minioProperties.getBucketName() + " already exists.");}// 设置存储对象名称String dateDir = DateUtil.format(new Date(), "yyyyMMdd");String uuid = UUID.randomUUID().toString().replace("-", "");//20230801/443e1e772bef482c95be28704bec58a901.jpgString fileName = dateDir+"/"+uuid+multipartFile.getOriginalFilename();System.out.println(fileName);PutObjectArgs putObjectArgs = PutObjectArgs.builder().bucket(minioProperties.getBucketName()).stream(multipartFile.getInputStream(), multipartFile.getSize(), -1).object(fileName)// 上传时指定对应对ContentType// Content-Type 用于向接收方说明传输资源的媒体类型,// 从而让浏览器用指定码表去解码。.contentType(ViewContentType.getContentType(fileName)).build();minioClient.putObject(putObjectArgs) ;return minioProperties.getEndpointUrl() + "/" + minioProperties.getBucketName() + "/" + fileName ;} catch (Exception e) {throw new RuntimeException(e);}}}

2.6 FileUploadController.java

package com.example.minioserver.controller;import com.example.minioserver.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.util.HashMap;
import java.util.Map;/*** @author: xbronze* @date: 2024-10-26 16:59* @description: TODO*/
@RestController
@RequestMapping("/file")
public class FileUploadController {@Autowiredprivate FileUploadService fileUploadService ;@PostMapping(value = "/upload")public Map<String,String> fileUploadService(@RequestParam(value = "file") MultipartFile multipartFile) {Map<String, String> map = new HashMap<>();String fileUrl = fileUploadService.fileUpload(multipartFile);map.put("fileurl",fileUrl);return map;}}

2.7 application.yml

spring:application:name: minioserver
server:port: 8091# 部署的minio服务
minio:endpointUrl: http://192.168.72.135:9100accessKey: adminsecreKey: admin123bucketName: test

2.8 pom.xml

这里只展示引入的依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/io.minio/minio --><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.5.10</version></dependency><!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.32</version></dependency></dependencies>

3 运行

访问文件上传接口,选择一张图片上传,接口请求成功后,会返回一个url链接。

在这里插入图片描述

通过链接可预览上传的图片。

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com