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

无锡模板建站多少钱厦门抖音代运营公司

无锡模板建站多少钱,厦门抖音代运营公司,超轻粘土做动漫网站,网站建设公司招商1. 项目背景 唉#xff01;本文写起来都是泪点。不是刻意写的本文#xff0c;主要是对日常用到的文件上传做了一个汇总总结#xff0c;同时希望可以给用到的小伙伴带来一点帮助吧。 上传本地#xff0c;这个就不水了#xff0c;基本做技术的都用到过吧#xff1b; 阿里…1. 项目背景 唉本文写起来都是泪点。不是刻意写的本文主要是对日常用到的文件上传做了一个汇总总结同时希望可以给用到的小伙伴带来一点帮助吧。 上传本地这个就不水了基本做技术的都用到过吧 阿里云OSS阿里云是业界巨鳄了吧用到的人肯定不少吧不过博主好久不用了简单记录下 华为云OBS工作需要也简单记录下吧 七牛云个人网站最开始使用的图床目的是为了白嫖10G文件存储。后来网站了升级了https域名七牛云免费只支持httphttps域名加速是收费的。https域名的网站在谷歌上请求图片时会强制升级为https。 又拍云个人网站目前在用的图床加入了又拍云联盟网站底部挂链接算是推广合作模式吧对我这种不介意的来说就是白嫖。速度还行可以去我的网站看一下笑小枫 还有腾讯云等等云暂没用过就先不整理了使用都很简单SDK文档很全也很简单。 2. 上传思路 分为两点来说。本文的精华也都在这里了统一思想。 2.1 前端调用上传文件 前端上传的话应该是我们常用的吧通过RequestParam(value file) MultipartFile file接收然后转为InputStream or byte[] or File然后调用上传就可以了核心也就在这很简单的尤其上传到云服务器装载好配置后直接调用SDK接口即可。 2.2 通过url地址上传网络文件 通过url上传应该很少用到吧使用场景呢例如爬取文章的时候把网络图片上传到自己的图床图片库根据url地址迁移。 说到这突然想起了一个问题大家写文章的时候图片上传到图床后在文章内是怎么保存的呢是全路径还是怎么保存的如果加速域名换了或者换图床地址了需要怎么迁移。希望有经验的大佬可以留言指导 3. 上传到本地 这个比较简单啦贴下核心代码吧 在yml配置下上传路径 file:local:maxFileSize: 10485760imageFilePath: D:/test/image/docFilePath: D:/test/file/创建配置类读取配置文件的参数 package com.maple.upload.properties;import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration;/*** 上传本地配置** author 笑小枫* date 2022/7/22* see a hrefhttps://www.xiaoxiaofeng.comhttps://www.xiaoxiaofeng.com/a*/ Data Configuration public class LocalFileProperties {// ---------------本地文件配置 start------------------/*** 图片存储路径*/Value(${file.local.imageFilePath})private String imageFilePath;/*** 文档存储路径*/Value(${file.local.docFilePath})private String docFilePath;/*** 文件限制大小*/Value(${file.local.maxFileSize})private long maxFileSize;// --------------本地文件配置 end-------------------}创建上传下载工具类 package com.maple.upload.util;import com.maple.upload.properties.LocalFileProperties; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile;import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.text.SimpleDateFormat; import java.util.*;/*** author 笑小枫* date 2024/1/10* see a hrefhttps://www.xiaoxiaofeng.comhttps://www.xiaoxiaofeng.com/a*/ Slf4j Component AllArgsConstructor public class LocalFileUtil {private final LocalFileProperties fileProperties;private static final ListString FILE_TYPE_LIST_IMAGE Arrays.asList(image/png,image/jpg,image/jpeg,image/bmp);/*** 上传图片*/public String uploadImage(MultipartFile file) {// 检查图片类型String contentType file.getContentType();if (!FILE_TYPE_LIST_IMAGE.contains(contentType)) {throw new RuntimeException(上传失败不允许的文件类型);}int size (int) file.getSize();if (size fileProperties.getMaxFileSize()) {throw new RuntimeException(文件过大);}String fileName file.getOriginalFilename();//获取文件后缀String afterName StringUtils.substringAfterLast(fileName, .);//获取文件前缀String prefName StringUtils.substringBeforeLast(fileName, .);//获取一个时间毫秒值作为文件名fileName new SimpleDateFormat(yyyyMMddHHmmss).format(new Date()) _ prefName . afterName;File filePath new File(fileProperties.getImageFilePath(), fileName);//判断文件是否已经存在if (filePath.exists()) {throw new RuntimeException(文件已经存在);}//判断文件父目录是否存在if (!filePath.getParentFile().exists()) {filePath.getParentFile().mkdirs();}try {file.transferTo(filePath);} catch (IOException e) {log.error(图片上传失败, e);throw new RuntimeException(图片上传失败);}return fileName;}/*** 批量上传文件*/public ListMapString, Object uploadFiles(MultipartFile[] files) {int size 0;for (MultipartFile file : files) {size (int) file.getSize() size;}if (size fileProperties.getMaxFileSize()) {throw new RuntimeException(文件过大);}ListMapString, Object fileInfoList new ArrayList();for (int i 0; i files.length; i) {MapString, Object map new HashMap();String fileName files[i].getOriginalFilename();//获取文件后缀String afterName StringUtils.substringAfterLast(fileName, .);//获取文件前缀String prefName StringUtils.substringBeforeLast(fileName, .);String fileServiceName new SimpleDateFormat(yyyyMMddHHmmss).format(new Date()) i _ prefName . afterName;File filePath new File(fileProperties.getDocFilePath(), fileServiceName);// 判断文件父目录是否存在if (!filePath.getParentFile().exists()) {filePath.getParentFile().mkdirs();}try {files[i].transferTo(filePath);} catch (IOException e) {log.error(文件上传失败, e);throw new RuntimeException(文件上传失败);}map.put(fileName, fileName);map.put(filePath, filePath);map.put(fileServiceName, fileServiceName);fileInfoList.add(map);}return fileInfoList;}/*** 批量删除文件** param fileNameArr 服务端保存的文件的名数组*/public void deleteFile(String[] fileNameArr) {for (String fileName : fileNameArr) {String filePath fileProperties.getDocFilePath() fileName;File file new File(filePath);if (file.exists()) {try {Files.delete(file.toPath());} catch (IOException e) {e.printStackTrace();log.warn(文件删除失败, e);}} else {log.warn(文件: {} 删除失败该文件不存在, fileName);}}}/*** 下载文件*/public void downLoadFile(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {String encodeFileName URLDecoder.decode(fileName, UTF-8);File file new File(fileProperties.getDocFilePath() encodeFileName);// 下载文件if (!file.exists()) {throw new RuntimeException(文件不存在);}try (FileInputStream inputStream new FileInputStream(file);ServletOutputStream outputStream response.getOutputStream()) {response.reset();//设置响应类型 PDF文件为application/pdfWORD文件为application/msword EXCEL文件为application/vnd.ms-excel。response.setContentType(application/octet-stream;charsetutf-8);//设置响应的文件名称,并转换成中文编码String afterName StringUtils.substringAfterLast(fileName, _);//保存的文件名,必须和页面编码一致,否则乱码afterName response.encodeURL(new String(afterName.getBytes(), StandardCharsets.ISO_8859_1.displayName()));response.setHeader(Content-type, application-download);//attachment作为附件下载inline客户端机器有安装匹配程序则直接打开注意改变配置清除缓存否则可能不能看到效果response.addHeader(Content-Disposition, attachment;filename afterName);response.addHeader(filename, afterName);//将文件读入响应流int length 1024;byte[] buf new byte[1024];int readLength inputStream.read(buf, 0, length);while (readLength ! -1) {outputStream.write(buf, 0, readLength);readLength inputStream.read(buf, 0, length);}outputStream.flush();} catch (Exception e) {e.printStackTrace();}} }访问图片的话可以通过重写WebMvcConfigurer的addResourceHandlers方法来实现。 通过请求/local/images/**将链接虚拟映射到我们配置的localFileProperties.getImageFilePath()下文件访问同理。 详细代码如下 package com.maple.upload.config;import com.maple.upload.properties.LocalFileProperties; import lombok.AllArgsConstructor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** author 笑小枫 https://www.xiaoxiaofeng.com/* date 2024/1/10*/ Configuration AllArgsConstructor public class LocalFileConfig implements WebMvcConfigurer {private final LocalFileProperties localFileProperties;Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// 重写方法// 修改tomcat 虚拟映射// 定义图片存放路径registry.addResourceHandler(/local/images/**).addResourceLocations(file: localFileProperties.getImageFilePath());//定义文档存放路径registry.addResourceHandler(/local/doc/**).addResourceLocations(file: localFileProperties.getDocFilePath());} }controller调用代码 package com.maple.upload.controller;import com.maple.upload.util.LocalFileUtil; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse; import java.util.List; import java.util.Map;/*** 文件相关操作接口** author 笑小枫* date 2024/1/10* see a hrefhttps://www.xiaoxiaofeng.comhttps://www.xiaoxiaofeng.com/a*/ Slf4j RestController AllArgsConstructor RequestMapping(/local) public class LocalFileController {private final LocalFileUtil fileUtil;/*** 图片上传*/PostMapping(/uploadImage)public String uploadImage(RequestParam(value file) MultipartFile file) {if (file.isEmpty()) {throw new RuntimeException(图片内容为空上传失败!);}return fileUtil.uploadImage(file);}/*** 文件批量上传*/PostMapping(/uploadFiles)public ListMapString, Object uploadFiles(RequestParam(value file) MultipartFile[] files) {return fileUtil.uploadFiles(files);}/*** 批量删除文件*/PostMapping(/deleteFiles)public void deleteFiles(RequestParam(value files) String[] files) {fileUtil.deleteFile(files);}/*** 文件下载功能*/GetMapping(value /download/{fileName:.*})public void download(PathVariable(fileName) String fileName, HttpServletResponse response) {try {fileUtil.downLoadFile(response, fileName);} catch (Exception e) {log.error(文件下载失败, e);}} }调用上传图片的接口可以看到图片已经上传成功。 通过请求/local/images/**将链接虚拟映射我们图片上。 批量上传删除等操作就不一一演示截图了代码已贴因为是先写的demo后写的文章获取代码片贴的有遗漏如有遗漏可以去文章底部查看源码地址。 4. 上传阿里云OSS 阿里云OSS官方sdk使用文档https://help.aliyun.com/zh/oss/developer-reference/java 阿里云OSS操作指南https://help.aliyun.com/zh/oss/user-guide 公共云下OSS Region和Endpoint对照表https://help.aliyun.com/zh/oss/user-guide/regions-and-endpoints 更多公共云下OSS Region和Endpoint对照参考上面链接 引入oss sdk依赖 !-- 阿里云OSS --dependencygroupIdcom.aliyun.oss/groupIdartifactIdaliyun-sdk-oss/artifactIdversion3.8.1/version/dependency配置上传配置信息 file:oss:bucketName: mapleBucketaccessKeyId: your aksecretAccessKey: your skendpoint: oss-cn-shanghai.aliyuncs.comshowUrl: cdn地址-file.xiaoxiaofeng.com创建配置类读取配置文件的参数 /** Copyright (c) 2018-2999 上海合齐软件科技科技有限公司 All rights reserved.**** 未经允许不可做商业用途** 版权所有侵权必究*/package com.maple.upload.properties;import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration;/*** 阿里云OSS配置** author 笑小枫 https://www.xiaoxiaofeng.com/* date 2024/1/10*/ Data Configuration public class AliOssProperties {Value(${file.oss.bucketName})private String bucketName;Value(${file.oss.accessKeyId})private String accessKeyId;Value(${file.oss.secretAccessKey})private String secretAccessKey;Value(${file.oss.endpoint})private String endpoint;Value(${file.oss.showUrl})private String showUrl; } 上传工具类 package com.maple.upload.util;import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.PutObjectResult; import com.maple.upload.properties.AliOssProperties; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile;import java.io.InputStream;/*** 阿里云OSS 对象存储工具类* 阿里云OSS官方sdk使用文档https://help.aliyun.com/zh/oss/developer-reference/java* 阿里云OSS操作指南https://help.aliyun.com/zh/oss/user-guide* * author 笑小枫 https://www.xiaoxiaofeng.com/* date 2024/1/15*/ Slf4j Component AllArgsConstructor public class AliOssUtil {private final AliOssProperties aliOssProperties;public String uploadFile(MultipartFile file) {String fileName file.getOriginalFilename();if (StringUtils.isBlank(fileName)) {throw new RuntimeException(获取文件信息失败);}// 组建上传的文件名称命名规则可自定义更改String objectKey FileCommonUtil.setFilePath(xiaoxiaofeng) FileCommonUtil.setFileName(xxf, fileName.substring(fileName.lastIndexOf(.)));//构造一个OSS对象的配置类OSS ossClient new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getSecretAccessKey());try (InputStream inputStream file.getInputStream()) {log.info(String.format(阿里云OSS上传开始原文件名%s上传后的文件名%s, fileName, objectKey));PutObjectResult result ossClient.putObject(aliOssProperties.getBucketName(), objectKey, inputStream);log.info(String.format(阿里云OSS上传结束文件名%s返回结果%s, objectKey, result.toString()));return aliOssProperties.getShowUrl() objectKey;} catch (Exception e) {log.error(调用阿里云OSS失败, e);throw new RuntimeException(调用阿里云OSS失败);}} }因为篇幅问题controller就不贴了。暂时没有阿里云oss的资源了这里就不做测试小伙伴在使用过程中如有问题麻烦留言告诉我下 5. 上传华为云OBS 华为云OBS官方sdk使用文档https://support.huaweicloud.com/sdk-java-devg-obs/obs_21_0101.html 华为云OBS操作指南https://support.huaweicloud.com/ugobs-obs/obs_41_0002.html 华为云各服务应用区域和各服务的终端节点https://developer.huaweicloud.com/endpoint?OBS 引入sdk依赖 !-- 华为云OBS --dependencygroupIdcom.huaweicloud/groupIdartifactIdesdk-obs-java-bundle/artifactIdversion3.21.8/version/dependency配置上传配置信息 file:obs:bucketName: mapleBucketaccessKey: your aksecretKey: your skendPoint: obs.cn-east-2.myhuaweicloud.comshowUrl: cdn地址-file.xiaoxiaofeng.com创建配置类读取配置文件的参数 package com.maple.upload.properties;import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration;/*** 华为云上传配置** author 笑小枫 https://www.xiaoxiaofeng.com/* date 2024/1/10*/ Data Configuration public class HwyObsProperties {Value(${file.obs.bucketName})private String bucketName;Value(${file.obs.accessKey})private String accessKey;Value(${file.obs.secretKey})private String secretKey;Value(${file.obs.endPoint})private String endPoint;Value(${file.obs.showUrl})private String showUrl; }上传工具类 package com.maple.upload.util;import com.alibaba.fastjson.JSON; import com.maple.upload.bean.HwyObsModel; import com.maple.upload.properties.HwyObsProperties; import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; import com.obs.services.model.PostSignatureRequest; import com.obs.services.model.PostSignatureResponse; import com.obs.services.model.PutObjectResult; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile;import java.io.IOException; import java.io.InputStream;/*** 华为云OBS 对象存储工具类* 华为云OBS官方sdk使用文档https://support.huaweicloud.com/sdk-java-devg-obs/obs_21_0101.html* 华为云OBS操作指南https://support.huaweicloud.com/ugobs-obs/obs_41_0002.html* 华为云各服务应用区域和各服务的终端节点https://developer.huaweicloud.com/endpoint?OBS** author 笑小枫* date 2022/7/22* see a hrefhttps://www.xiaoxiaofeng.comhttps://www.xiaoxiaofeng.com/a*/ Slf4j Component AllArgsConstructor public class HwyObsUtil {private final HwyObsProperties fileProperties;/*** 上传华为云obs文件存储** param file 文件* return 文件访问路径 如果配置CDN这里直接返回CDN文件名objectKey*/public String uploadFileToObs(MultipartFile file) {String fileName file.getOriginalFilename();if (StringUtils.isBlank(fileName)) {throw new RuntimeException(获取文件信息失败);}// 文件类型String fileType fileName.substring(file.getOriginalFilename().lastIndexOf(.));// 组建上传的文件名称命名规则可自定义更改String objectKey FileCommonUtil.setFilePath() FileCommonUtil.setFileName(null, fileType);PutObjectResult putObjectResult;try (InputStream inputStream file.getInputStream();ObsClient obsClient new ObsClient(fileProperties.getAccessKey(), fileProperties.getSecretKey(), fileProperties.getEndPoint())) {log.info(String.format(华为云obs上传开始原文件名%s上传后的文件名%s, fileName, objectKey));putObjectResult obsClient.putObject(fileProperties.getBucketName(), objectKey, inputStream);log.info(String.format(华为云obs上传结束文件名%s返回结果%s, objectKey, JSON.toJSONString(putObjectResult)));} catch (ObsException | IOException e) {log.error(华为云obs上传文件失败, e);throw new RuntimeException(华为云obs上传文件失败请重试);}if (putObjectResult.getStatusCode() 200) {return putObjectResult.getObjectUrl();} else {throw new RuntimeException(华为云obs上传文件失败请重试);}}/*** 获取华为云上传token将token返回给前端然后由前端上传这样文件不占服务器端带宽** param fileName 文件名称* return*/public HwyObsModel getObsConfig(String fileName) {if (StringUtils.isBlank(fileName)) {throw new RuntimeException(The fileName cannot be empty.);}String obsToken;String objectKey null;try (ObsClient obsClient new ObsClient(fileProperties.getAccessKey(), fileProperties.getSecretKey(), fileProperties.getEndPoint())) {String fileType fileName.substring(fileName.lastIndexOf(.));// 组建上传的文件名称命名规则可自定义更改objectKey FileCommonUtil.setFilePath() FileCommonUtil.setFileName(, fileType);PostSignatureRequest request new PostSignatureRequest();// 设置表单上传请求有效期单位秒request.setExpires(3600);request.setBucketName(fileProperties.getBucketName());if (StringUtils.isNotBlank(objectKey)) {request.setObjectKey(objectKey);}PostSignatureResponse response obsClient.createPostSignature(request);obsToken response.getToken();} catch (ObsException | IOException e) {log.error(华为云obs上传文件失败, e);throw new RuntimeException(华为云obs上传文件失败请重试);}HwyObsModel obsModel new HwyObsModel();obsModel.setBucketName(fileProperties.getBucketName());obsModel.setEndPoint(fileProperties.getEndPoint());obsModel.setToken(obsToken);obsModel.setObjectKey(objectKey);obsModel.setShowUrl(fileProperties.getShowUrl());return obsModel;} }篇幅问题不贴controller和测试截图了完整代码参考文章底部源码吧思想明白了别的都大差不差。 6. 上传七牛云 七牛云官方sdkhttps://developer.qiniu.com/kodo/1239/java 七牛云存储区域表链接https://developer.qiniu.com/kodo/1671/region-endpoint-fq 引入sdk依赖 !-- 七牛云 --dependencygroupIdcom.qiniu/groupIdartifactIdqiniu-java-sdk/artifactIdversion7.2.29/version/dependency配置上传配置信息 file:qiniuyun:bucket: mapleBucketaccessKey: your aksecretKey: your skregionId: z1showUrl: cdn地址-file.xiaoxiaofeng.com创建配置类读取配置文件的参数 package com.maple.upload.properties;import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration;/*** 七牛云配置** author 笑小枫 https://www.xiaoxiaofeng.com/* date 2024/1/10*/ Data Configuration public class QiNiuProperties {Value(${file.qiniuyun.accessKey})private String accessKey;Value(${file.qiniuyun.secretKey})private String secretKey;Value(${file.qiniuyun.bucket})private String bucket;Value(${file.qiniuyun.regionId})private String regionId;Value(${file.qiniuyun.showUrl})private String showUrl; }上传工具类注意有一个区域转换如后续有新增qiNiuConfig这里需要调整一下 package com.maple.upload.util;import com.maple.upload.properties.QiNiuProperties; import com.qiniu.http.Response; import com.qiniu.storage.Configuration; import com.qiniu.storage.Region; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile;import java.io.InputStream; import java.util.Objects;/*** * 七牛云 对象存储工具类* 七牛云官方sdkhttps://developer.qiniu.com/kodo/1239/java* 七牛云存储区域表链接https://developer.qiniu.com/kodo/1671/region-endpoint-fq* * author 笑小枫 https://www.xiaoxiaofeng.com/* date 2022/3/24*/ Slf4j Component AllArgsConstructor public class QiNiuYunUtil {private final QiNiuProperties qiNiuProperties;public String uploadFile(MultipartFile file) {String fileName file.getOriginalFilename();if (StringUtils.isBlank(fileName)) {throw new RuntimeException(获取文件信息失败);}// 组建上传的文件名称命名规则可自定义更改String objectKey FileCommonUtil.setFilePath(xiaoxiaofeng) FileCommonUtil.setFileName(xxf, fileName.substring(fileName.lastIndexOf(.)));//构造一个带指定 Region 对象的配置类Configuration cfg qiNiuConfig(qiNiuProperties.getRegionId());//...其他参数参考类注释UploadManager uploadManager new UploadManager(cfg);try (InputStream inputStream file.getInputStream()) {Auth auth Auth.create(qiNiuProperties.getAccessKey(), qiNiuProperties.getSecretKey());String upToken auth.uploadToken(qiNiuProperties.getBucket());log.info(String.format(七牛云上传开始原文件名%s上传后的文件名%s, fileName, objectKey));Response response uploadManager.put(inputStream, objectKey, upToken, null, null);log.info(String.format(七牛云上传结束文件名%s返回结果%s, objectKey, response.toString()));return qiNiuProperties.getShowUrl() objectKey;} catch (Exception e) {log.error(调用七牛云失败, e);throw new RuntimeException(调用七牛云失败);}}private static Configuration qiNiuConfig(String zone) {Region region null;if (Objects.equals(zone, z1)) {region Region.huabei();} else if (Objects.equals(zone, z0)) {region Region.huadong();} else if (Objects.equals(zone, z2)) {region Region.huanan();} else if (Objects.equals(zone, na0)) {region Region.beimei();} else if (Objects.equals(zone, as0)) {region Region.xinjiapo();}return new Configuration(region);} }篇幅问题不贴controller和测试截图了完整代码参考文章底部源码吧思想明白了别的都大差不差。 7. 上传又拍云 又拍云客户端配置https://help.upyun.com/knowledge-base/quick_start/ 又拍云官方sdkhttps://github.com/upyun/java-sdk 引入sdk依赖 !-- 又拍云OSS --dependencygroupIdcom.upyun/groupIdartifactIdjava-sdk/artifactIdversion4.2.3/version/dependency配置上传配置信息 file:upy:bucketName: mapleBucketuserName: 操作用户名称password: 操作用户密码showUrl: cdn地址-file.xiaoxiaofeng.com创建配置类读取配置文件的参数 package com.maple.upload.properties;import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration;/*** 又拍云上传配置** author 笑小枫 https://www.xiaoxiaofeng.com/* date 2024/1/10*/ Data Configuration public class UpyOssProperties {Value(${file.upy.bucketName})private String bucketName;Value(${file.upy.userName})private String userName;Value(${file.upy.password})private String password;/*** 加速域名*/Value(${file.upy.showUrl})private String showUrl; }上传工具类 package com.maple.upload.util;import com.maple.upload.properties.UpyOssProperties; import com.upyun.RestManager; import com.upyun.UpException; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import okhttp3.Response; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile;import java.io.IOException; import java.io.InputStream; import java.net.URI;/*** 又拍云 对象存储工具类* 又拍云客户端配置https://help.upyun.com/knowledge-base/quick_start/* 又拍云官方sdkhttps://github.com/upyun/java-sdk** author 笑小枫* date 2022/7/22* see a hrefhttps://www.xiaoxiaofeng.comhttps://www.xiaoxiaofeng.com/a*/ Slf4j Component AllArgsConstructor public class UpyOssUtil {private final UpyOssProperties fileProperties;/*** 根据url上传文件到又拍云*/public String uploadUpy(String url) {// 组建上传的文件名称命名规则可自定义更改String fileName FileCommonUtil.setFilePath(xiaoxiaofeng) FileCommonUtil.setFileName(xxf, url.substring(url.lastIndexOf(.)));RestManager restManager new RestManager(fileProperties.getBucketName(), fileProperties.getUserName(), fileProperties.getPassword());URI u URI.create(url);try (InputStream inputStream u.toURL().openStream()) {byte[] bytes IOUtils.toByteArray(inputStream);Response response restManager.writeFile(fileName, bytes, null);if (response.isSuccessful()) {return fileProperties.getShowUrl() fileName;}} catch (IOException | UpException e) {log.error(又拍云oss上传文件失败, e);}throw new RuntimeException(又拍云oss上传文件失败请重试);}/*** MultipartFile上传文件到又拍云*/public String uploadUpy(MultipartFile file) {String fileName file.getOriginalFilename();if (StringUtils.isBlank(fileName)) {throw new RuntimeException(获取文件信息失败);}// 组建上传的文件名称命名规则可自定义更改String objectKey FileCommonUtil.setFilePath(xiaoxiaofeng) FileCommonUtil.setFileName(xxf, fileName.substring(fileName.lastIndexOf(.)));RestManager restManager new RestManager(fileProperties.getBucketName(), fileProperties.getUserName(), fileProperties.getPassword());try (InputStream inputStream file.getInputStream()) {log.info(String.format(又拍云上传开始原文件名%s上传后的文件名%s, fileName, objectKey));Response response restManager.writeFile(objectKey, inputStream, null);log.info(String.format(又拍云上传结束文件名%s返回结果%s, objectKey, response.isSuccessful()));if (response.isSuccessful()) {return fileProperties.getShowUrl() objectKey;}} catch (IOException | UpException e) {log.error(又拍云oss上传文件失败, e);}throw new RuntimeException(又拍云oss上传文件失败请重试);} }篇幅问题不贴controller和测试截图了完整代码参考文章底部源码吧思想明白了别的都大差不差。 8. 项目源码 本文到此就结束了如果帮助到你了帮忙点个赞 本文源码https://github.com/hack-feng/maple-product/tree/main/maple-file-upload 我是笑小枫全网皆可搜的【笑小枫】
http://www.zqtcl.cn/news/108436/

相关文章:

  • 成都公司网站制作公司实验一 电子商务网站建设与维护
  • 即墨区城乡建设局网站300m空间够用吗 wordpress
  • 成都软件开发培训机构7个湖北seo网站推广策略
  • 嘉定企业网站建设深圳网站制作费用
  • 外贸网站有必要吗wordpress远程保存图片
  • 苏州吴中网站建设wordpress中文版安装教程
  • wordpress 网站静态页面赶集网网站建设分析
  • 伊春网站开发大型网站建设兴田德润专业
  • 温州平阳县营销型网站建设榆林做网站
  • 沽源网站建设娄底网站建设工作室
  • 商场网站 策划医疗网站是否全部需要前置备案
  • 电商网站开发实训心得wordpress网络验证
  • 美图网seo 优化技术难度大吗
  • 知名的传媒行业网站开发天空网站开发者
  • 网站域名年费多少钱二手手表交易平台哪个好
  • 用易语言做抢购网站软件下载自己可以做企业网站吗
  • 公司网站续费帐怎么做互联网专业
  • 网站开发公司深圳外贸营销策略
  • 主要搜索引擎网站搜索结果比较wordpress novelist
  • 校园网站制度建设WordPress手机不显示
  • 胶州哪家公司做网站wordpress的html
  • 辽宁省建设厅网站江苏住房和城乡建设厅官方网站
  • 链接关系 网站层次结构南宁做网站找哪家公司
  • 定制网站开发哪家好崇明建设镇网站
  • 上海网站制作建设是什么wordpress管理页面
  • 酒店网站设计的目的和意义网络营销相关理论
  • 用google翻译做多语言网站企业官网建站网站
  • 南阳网站建设培训学校莞城短视频seo优化
  • 开发商城网站建设做网站租用那个服务器好
  • 2015做导航网站wordpress中文主