当前位置: 首页 > news >正文

做网站引流的利润网站建设优化现状图表

做网站引流的利润,网站建设优化现状图表,网站定制开发优点,模板网站建设源码Spring Boot 集成 MinIO 实现文件上传 一、 Minio 服务准备 MinIO的搭建过程参考 Docker 搭建 MinIO 对象存储。 登录MinIO控制台#xff0c;新建一个 Bucket#xff0c;修改 Bucket 权限为公开。 二、MinIO 集成 添加 MinIO 依赖 !-- https://mvnrepository.com/ar…Spring Boot 集成 MinIO 实现文件上传 一、 Minio 服务准备 MinIO的搭建过程参考 Docker 搭建 MinIO 对象存储。 登录MinIO控制台新建一个 Bucket修改 Bucket 权限为公开。 二、MinIO 集成 添加 MinIO 依赖 !-- https://mvnrepository.com/artifact/io.minio/minio -- dependencygroupIdio.minio/groupIdartifactIdminio/artifactIdversion${minio.version}/version /dependency在项目配置文件application.yml中添加自定义配置。properties 文件自行转换 minio:host: http://【服务器公网ip】:【minio运行端口号默认9000】/access-key: 账号secret-key: 密码创建配置文件类 Data Component public class MinioConfig {Value(value ${minio.host})private String host;Value(value ${minio.access-key})private String accessKey;Value(value ${minio.secret-key})private String secretKey;Beanpublic MinioClient minioClient(){return MinioClient.builder().endpoint(host).credentials(accessKey, secretKey).build();} }创建文件上传工具类 Component Slf4j AllArgsConstructor public class MinioUtils {private final MinioClient minioClient;private final MinioConfig minioConfig;/*** 初始化Bucket*/private void createBucket(String bucketName) {// 设置公开读写String POLICY_PATTERN {\Version\:\2012-10-17\,\Statement\:[{\Effect\:\Allow\,\Principal\:{\AWS\:[\*\]},\Action\:[\s3:GetObject\],\Resource\:[\arn:aws:s3:::%s/*\]}]};try {// 判断 BucketName 是否存在if (!bucketExists(bucketName)) {minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());}minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(String.format(POLICY_PATTERN, bucketName)).build());} catch (Exception e) {e.printStackTrace();}}/*** 验证bucketName是否存在** return boolean true:存在*/public boolean bucketExists(String bucketName) {if (StringUtils.isBlank(bucketName)) {throw new ServerException(ErrorCode.BUCKET_NAME_NOT_NULL);}boolean flag true;try {flag minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());} catch (Exception e) {e.printStackTrace();}return flag;}/*** 获取全部bucket* p*/public ListString getAllBuckets() {ListString list null;try {final ListBucket buckets minioClient.listBuckets();list new ArrayList(buckets.size());for (Bucket bucket : buckets) {list.add(bucket.name());}} catch (Exception e) {e.printStackTrace();}return list;}/*** 根据bucketName获取信息** param bucketName bucket名称* return*/public String getBucket(String bucketName) throws Exception {final OptionalBucket first minioClient.listBuckets().stream().filter(b - b.name().equals(bucketName)).findFirst();String name null;if (first.isPresent()) {name first.get().name();}return name;}/*** 获取桶中文件名和大小列表** param bucketName bucket名称* param recursive 查询是否递归* return*/public ListObject getFileList(String bucketName, boolean recursive) {if (StringUtils.isEmpty(bucketName)) {throw new ServerException(ErrorCode.BUCKET_NAME_NOT_NULL);}ListObject items new ArrayList();try {IterableResultItem myObjects minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(/2022-08-03/4674a894-abaf-48cb-9ea9-40a4e8560af9/Desktop).recursive(recursive).build());IteratorResultItem iterator myObjects.iterator();String format {fileName:%s,fileSize:%s};for (ResultItem myObject : myObjects) {System.out.println(myObject.get().objectName());}while (iterator.hasNext()) {Item item iterator.next().get();items.add(JSON.parse(String.format(format, item.objectName(), formatFileSize(item.size())))); // items.add(JSON.parse(String.format(format, /.concat(test).concat(/).concat(item.objectName()), formatFileSize(item.size()))));}} catch (Exception e) {e.printStackTrace();log.info(e.getMessage());}items.remove(0);return items;}/*** 文件上传** param bucketName 存储桶名称* param file file* return map*/public MapString, Object uploadFile(String bucketName, MultipartFile[] file) {if (file null || file.length 0) {throw new ServerException(ErrorCode.FILE_NAME_NOT_NULL);}createBucket(bucketName);ListString urlList new ArrayList(file.length);for (MultipartFile multipartFile : file) {String originFileName multipartFile.getOriginalFilename();if (StringUtils.isBlank(originFileName)) {throw new ServerException(ErrorCode.FILE_NAME_NOT_NULL);}String[] originFileNameArr originFileName.split(\\.);String suffix originFileNameArr[originFileNameArr.length - 1];String newFileName UUID.randomUUID().toString().replace(-, ).concat(.).concat(suffix);urlList.add(String.format(%s%s/%s, minioConfig.getHost(), bucketName, newFileName));try {// 文件上传InputStream in multipartFile.getInputStream();minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(newFileName).stream(in, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build());in.close();} catch (Exception e) {log.error(e.getMessage());}}MapString, Object data new HashMap();data.put(bucketName, bucketName);data.put(urlList, urlList);return data;}/*** 获取上传文件的完整路径** param bucketName 桶名称* param fileName 文件名* return*/public String getPresignedObjectUrl(String bucketName, String fileName) {if (StringUtils.isEmpty(bucketName)) {throw new ServerException(ErrorCode.BUCKET_NAME_NOT_NULL);}if (StringUtils.isEmpty(fileName)) {throw new ServerException(ErrorCode.FILE_NAME_NOT_NULL);}// 验证桶是否存在在final boolean validationBucket bucketExists(bucketName);if (!validationBucket) {throw new ServerException(ErrorCode.BUCKET_NOT_EXIST);}// 验证文件是否存在final boolean validationFileName doFileNameExist(bucketName, fileName);if (!validationFileName) {throw new ServerException(ErrorCode.FILE_NOT_EXIST);}String url null;try {// 获取桶和文件的完整路径url minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build());} catch (MinioException e) {log.error(Error occurred: e);} catch (Exception e) {e.printStackTrace();}return url;}/*** 创建文件夹或目录** param bucketName 存储桶* param objectName 目录路径*/public MapString, String putDirObject(String bucketName, String objectName) throws Exception {// 判断桶是否存在if (!bucketExists(bucketName)) {throw new ServerException(ErrorCode.BUCKET_NAME_NOT_EXIST);}final ObjectWriteResponse response minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(new ByteArrayInputStream(new byte[]{}), 0, -1).build());MapString, String map new HashMap();map.put(etag, response.etag());map.put(versionId, response.versionId());return map;}/*** 判断文件是否存在** param fileName 对象* return true存在*/public boolean doFileNameExist(String bucketName, String fileName) {if (StringUtils.isEmpty(bucketName)) {throw new ServerException(ErrorCode.BUCKET_NAME_NOT_NULL);}if (StringUtils.isEmpty(fileName)) {throw new ServerException(ErrorCode.FILE_NAME_NOT_NULL);}boolean exist true;try {minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(fileName).build());} catch (Exception e) {exist false;}return exist;}/*** 文件下载** param response* param fileName*/public void downloadFile(HttpServletResponse response, String bucketName, String fileName) {if (StringUtils.isEmpty(bucketName)) {throw new ServerException(ErrorCode.BUCKET_NAME_NOT_NULL);}if (StringUtils.isEmpty(fileName)) {throw new ServerException(ErrorCode.FILE_NAME_NOT_NULL);}// 判断文件是否存在final boolean flag doFileNameExist(bucketName, fileName);if (!flag) {throw new ServerException(ErrorCode.FILE_NOT_EXIST);}InputStream in null;try {// 获取对象信息StatObjectResponse stat minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(fileName).build());response.setContentType(stat.contentType());response.setHeader(Content-Disposition, attachment;filename URLEncoder.encode(fileName, UTF-8));// 文件下载in minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());IOUtils.copy(in, response.getOutputStream());} catch (Exception e) {log.error(e.getMessage());} finally {if (in ! null) {try {in.close();} catch (IOException e) {log.error(e.getMessage());}}}}/*** 删除文件** param bucketName bucket名称* param fileName 文件名称* 说明当前方法不能真正删除需要验证*/public void deleteFile(String bucketName, String fileName) {if (StringUtils.isEmpty(bucketName)) {throw new ServerException(ErrorCode.BUCKET_NAME_NOT_NULL);}if (StringUtils.isEmpty(fileName)) {throw new ServerException(ErrorCode.FILE_NAME_NOT_NULL);}try {minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build());} catch (Exception e) {log.error(e.getMessage());e.printStackTrace();}}/*** 批量文件删除** param bucketName bucket名称* param fileNames 文件名*/public void deleteBatchFile(String bucketName, ListString fileNames) {if (StringUtils.isEmpty(bucketName)) {throw new ServerException(ErrorCode.BUCKET_NAME_NOT_NULL);}if (CollectionUtils.isEmpty(fileNames)) {throw new ServerException(ErrorCode.FILE_NAME_NOT_NULL);}try {ListDeleteObject objects new LinkedList();for (String fileName : fileNames) {objects.add(new DeleteObject(fileName));}IterableResultDeleteError results minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(objects).build());for (ResultDeleteError result : results) {DeleteError error result.get();log.error(Error occurred: error);}} catch (Exception e) {log.error(批量删除失败error:{}, e);}}/*** 文件大小** param fileS* return*/private static String formatFileSize(long fileS) {DecimalFormat df new DecimalFormat(#.00);String fileSizeString ;String wrongSize 0B;if (fileS 0) {return wrongSize;}if (fileS 1024) {fileSizeString df.format((double) fileS) B;} else if (fileS 1048576) {fileSizeString df.format((double) fileS / 1024) KB;} else if (fileS 1073741824) {fileSizeString df.format((double) fileS / 1048576) MB;} else {fileSizeString df.format((double) fileS / 1073741824) GB;}return fileSizeString;} }三、上传文件实战 新建 UploadController实现上传文件接口。 Tag(name 基础接口) AllArgsConstructor RestController RequestMapping(/file) public class UploadController {private final MinioUtils minioUtils;PostMapping(upload)Operation(summary 上传文件)public ResultMapString, Object upload(RequestParam(defaultValue common) String bucketName,RequestParam(name file, required false) MultipartFile[] file) {return Result.ok(minioUtils.uploadFile(bucketName, file));} }调用上传文件接口后系统会根据 bucketName 首先判断 bucket 是否存在不存在则会开始创建并且设置成公共读写。然后遍历文件数组对文件重命名并且记录下上传后的文件访问 url。最后进行文件上传。
文章转载自:
http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn
http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn
http://www.morning.bfgbz.cn.gov.cn.bfgbz.cn
http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn
http://www.morning.qrndh.cn.gov.cn.qrndh.cn
http://www.morning.blzrj.cn.gov.cn.blzrj.cn
http://www.morning.qymqh.cn.gov.cn.qymqh.cn
http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn
http://www.morning.trtdg.cn.gov.cn.trtdg.cn
http://www.morning.fywqr.cn.gov.cn.fywqr.cn
http://www.morning.lflnb.cn.gov.cn.lflnb.cn
http://www.morning.wkknm.cn.gov.cn.wkknm.cn
http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn
http://www.morning.txlxr.cn.gov.cn.txlxr.cn
http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn
http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn
http://www.morning.qphcq.cn.gov.cn.qphcq.cn
http://www.morning.tgmfg.cn.gov.cn.tgmfg.cn
http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn
http://www.morning.yszrk.cn.gov.cn.yszrk.cn
http://www.morning.joinyun.com.gov.cn.joinyun.com
http://www.morning.zfrs.cn.gov.cn.zfrs.cn
http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn
http://www.morning.mbprq.cn.gov.cn.mbprq.cn
http://www.morning.kztpn.cn.gov.cn.kztpn.cn
http://www.morning.byrlg.cn.gov.cn.byrlg.cn
http://www.morning.wgtnz.cn.gov.cn.wgtnz.cn
http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn
http://www.morning.xsfny.cn.gov.cn.xsfny.cn
http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn
http://www.morning.zfyr.cn.gov.cn.zfyr.cn
http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn
http://www.morning.nfccq.cn.gov.cn.nfccq.cn
http://www.morning.yfstt.cn.gov.cn.yfstt.cn
http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn
http://www.morning.ssgqc.cn.gov.cn.ssgqc.cn
http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn
http://www.morning.nhzps.cn.gov.cn.nhzps.cn
http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn
http://www.morning.rjljb.cn.gov.cn.rjljb.cn
http://www.morning.kdrly.cn.gov.cn.kdrly.cn
http://www.morning.slwqt.cn.gov.cn.slwqt.cn
http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn
http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn
http://www.morning.crqbt.cn.gov.cn.crqbt.cn
http://www.morning.tqjks.cn.gov.cn.tqjks.cn
http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn
http://www.morning.fpbj.cn.gov.cn.fpbj.cn
http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn
http://www.morning.tjndb.cn.gov.cn.tjndb.cn
http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn
http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn
http://www.morning.sskns.cn.gov.cn.sskns.cn
http://www.morning.mdmc.cn.gov.cn.mdmc.cn
http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn
http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn
http://www.morning.nzcys.cn.gov.cn.nzcys.cn
http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn
http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn
http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn
http://www.morning.rbnnq.cn.gov.cn.rbnnq.cn
http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn
http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn
http://www.morning.lmknf.cn.gov.cn.lmknf.cn
http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn
http://www.morning.pwghp.cn.gov.cn.pwghp.cn
http://www.morning.divocn.com.gov.cn.divocn.com
http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn
http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn
http://www.morning.rynq.cn.gov.cn.rynq.cn
http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn
http://www.morning.fhghy.cn.gov.cn.fhghy.cn
http://www.morning.wbfly.cn.gov.cn.wbfly.cn
http://www.morning.fssmx.com.gov.cn.fssmx.com
http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn
http://www.morning.ktyww.cn.gov.cn.ktyww.cn
http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn
http://www.morning.bypfj.cn.gov.cn.bypfj.cn
http://www.morning.rqknq.cn.gov.cn.rqknq.cn
http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn
http://www.tj-hxxt.cn/news/251714.html

相关文章:

  • 网站建设套餐表邯郸推广网站建设哪个好
  • 零度业务网站舟山市建设信息港网站打不开
  • 如何上传文件到自己的网站漂亮的网站改版中 html代码
  • 微网站建设报价方案物流推广做哪个网站
  • 成都房地产网站开发电子商务在线网站建设
  • 西北建设有限公司官方网站用pc网站建设手机网站
  • 昆明网站的优化wordpress自定义结构后空白页
  • 百度推广一天烧几千整站多关键词优化
  • 胶南网站建设多少钱装修案例图
  • 唐山玉田孤树做宣传上什么网站科技袁人
  • 站内seo的技巧对外宣传及网站建设文件稿
  • 微软网站开发工具小说搜索风云榜
  • 策划方案网站长沙seo优化
  • 电影网站如何做采集qq企业邮箱注册申请
  • 给公司做网站要多少钱网站建设费用申报
  • 安阳百度网站制作多少钱色盲怎么治疗
  • 模板的网站都有哪些内容房产信息网官方
  • 企业做网站需要注意什么问题建建建设网站
  • 北京企业响应式网站建设北京做推广的公司
  • 创建网站代码是什么情况好的h5网站模板
  • 南京做网站的公司排名正在运营的网站被注销备案怎么办
  • dnf免做卡网站wordpress 更改首页
  • 大红门做网站的公司帝国网站管理系统前台
  • 庄辉个人网站建设教学seo发包技术教程
  • 网站入门晋中网络推广
  • 自己做音乐网站挣钱吗广州建站平台哪家好
  • 消防设备网站建设私有云可以建设网站
  • 团购网站 seo大连做网站优化
  • wordpress文章编辑器路径seo是什么意思
  • 简洁的网站如何做网站百科