SpringBoot实战之文件上传存入AzureStorage
将符号位一起右移str[k++] = hexdigits[byte0 0xf]; // 取字节中低 4 位的数字转换}s = new String(str); // 换后的结果转换为字符串return s;}} 7、上传文件到Azure Storage, null);return resultMsg;}ResultMsg resultMsg = new ResultMsg(ResultStatusCode.OK.getErrcode(), thumbnailStream.toByteArray().length);//将上传后的图片URL返回BlobUpload blobUploadEntity = new BlobUpload();blobUploadEntity.setFileName(tempMultipartFile.getOriginalFilename());blobUploadEntity.setFileUrl(blob.getUri().toString());blobUploadEntity.setThumbnailUrl(thumbnailBlob.getUri().toString());blobUploadEntities.add(blobUploadEntity);}catch(Exception e){ResultMsg resultMsg = new ResultMsg(ResultStatusCode.SYSTEM_ERR.getErrcode(),用来配置连接信息package com.xiaofangtech.sunt.storage;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = azureblob,然后将接收到的文件upload到Azure Storage中存成blob package com.xiaofangtech.sunt.storage;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.imageio.ImageIO;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestPart;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import com.microsoft.azure.storage.blob.CloudBlobContainer;import com.microsoft.azure.storage.blob.CloudBlockBlob;import com.xiaofangtech.sunt.utils.MyUtils;import com.xiaofangtech.sunt.utils.ResultMsg;import com.xiaofangtech.sunt.utils.ResultStatusCode;@RestController@RequestMapping(file)public class FileUploadModule {@Autowiredprivate StorageConfig storageConfig;//设置缩略图的宽高private static int thumbnailWidth = 150;private static int thumbnailHeight = 100;@RequestMapping(value = upload,以上传图片为例 1、本章与Azure的交互使用到Azure storage相关的依赖库。
storageConfig);for (int i=0;imultipartFile.length;i++){MultipartFile tempMultipartFile = multipartFile[i];if (!tempMultipartFile.isEmpty()){try{//过滤非jpg, BufferedImage.TYPE_INT_RGB);img.createGraphics().drawImage(ImageIO.read(tempMultipartFile.getInputStream()).getScaledInstance(thumbnailWidth, thumbnailHeight。
Image.SCALE_SMOOTH),0, storageConfig.getBlobEndpoint(), Boolean thumbnail){String afterName = ;if (thumbnail){afterName = thumbnail/;}switch (fileType){case 1:return logo/ + afterName;case 2:return food/ + afterName;case 3:return head/ + afterName;case 4:return ads/ + afterName;default :return ;}}} 8、测试 上传文件 测试访问图片 ,ResultStatusCode.SYSTEM_ERR.getErrmsg(), int type,ResultStatusCode.OK.getErrmsg(),storageConfig.getQueueEndpoint(), @RequestPart(file) MultipartFile[] multipartFile){ListBlobUpload blobUploadEntities = new ArrayListBlobUpload();try{if(multipartFile != null){//获取或创建containerCloudBlobContainer blobContainer = BlobHelper.getBlobContainer(id.toLowerCase(), a。
并上传至AzureStorageBufferedImage img = new BufferedImage(thumbnailWidth, null);return resultMsg;}//拼装blob的名称(前缀名称+文件的md5值+文件扩展名称)String checkSum = MyUtils.getMD5(tempMultipartFile.getInputStream());String fileExtension = getFileExtension(tempMultipartFile.getOriginalFilename()).toLowerCase();String preName = getBlobPreName(type, jpg, true).toLowerCase();String thumbnailCheckSum = MyUtils.getMD5(new ByteArrayInputStream(thumbnailStream.toByteArray()));String blobThumbnail = thumbnailPreName + thumbnailCheckSum + .jpg;CloudBlockBlob thumbnailBlob = blobContainer.getBlockBlobReference(blobThumbnail);thumbnailBlob.getProperties().setContentType(image/jpeg);thumbnailBlob.upload(inputStream, Spring Boot实战之文件上传存入Azure Storage 本章介绍, null);return resultMsg;}}}}}catch(Exception e){ResultMsg resultMsg = new ResultMsg(ResultStatusCode.SYSTEM_ERR.getErrcode(), 2,对 MD5 的每一个字节// 转换成 16 进制字符的转换byte byte0 = tmp[i]; // 取第 i 个字节str[k++] = hexdigits[byte0 4 0xf]; // 取字节中高 4 位的数字转换, locations = classpath:azureblob.properties)public class StorageConfig {private String defaultEndpointsProtocol;private String blobEndpoint;private String queueEndpoint;private String tableEndpoint;private String accountName;private String accountKey;public String getDefaultEndpointsProtocol() {return defaultEndpointsProtocol;}public void setDefaultEndpointsProtocol(String defaultEndpointsProtocol) {this.defaultEndpointsProtocol = defaultEndpointsProtocol;}public String getBlobEndpoint() {return blobEndpoint;}public void setBlobEndpoint(String blobEndpoint) {this.blobEndpoint = blobEndpoint;}public String getQueueEndpoint() {return queueEndpoint;}public void setQueueEndpoint(String queueEndpoint) {this.queueEndpoint = queueEndpoint;}public String getTableEndpoint() {return tableEndpoint;}public void setTableEndpoint(String tableEndpoint) {this.tableEndpoint = tableEndpoint;}public String getAccountName() {return accountName;}public void setAccountName(String accountName) {this.accountName = accountName;}public String getAccountKey() {return accountKey;}public void setAccountKey(String accountKey) {this.accountKey = accountKey;}}3、新建配置文件azureblob.propertiesazureblob.defaultEndpointsProtocol=httpazureblob.blobEndpoint=http://teststorage.blob.core.chinacloudapi.cn/azureblob.queueEndpoint=http://teststorage.queue.core.chinacloudapi.cn/azureblob.tableEndpoint=http://teststorage.table.core.chinacloudapi.cn/azureblob.accountName=teststorageazureblob.accountKey=accountkey 4、新建帮助类。
storageConfig.getAccountKey());CloudStorageAccount account = CloudStorageAccount.parse(blobStorageConnectionString);CloudBlobClient serviceClient = account.createCloudBlobClient();CloudBlobContainer container = serviceClient.getContainerReference(containerName);// Create a permissions object.BlobContainerPermissions containerPermissions = new BlobContainerPermissions();// Include public access in the permissions object.containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);// Set the permissions on the container.container.uploadPermissions(containerPermissions);container.createIfNotExists();return container;}catch(Exception e){return null;}}} 5、定义上传成功后,storageConfig.getDefaultEndpointsProtocol(),f };public static String getMD5(String inStr) {MessageDigest md5 = null;try {md5 = MessageDigest.getInstance(MD5);} catch (Exception e) {e.printStackTrace();return ;}char[] charArray = inStr.toCharArray();byte[] byteArray = new byte[charArray.length];for (int i = 0; i charArray.length; i++)byteArray[i] = (byte) charArray[i];byte[] md5Bytes = md5.digest(byteArray);StringBuffer hexValue = new StringBuffer();for (int i = 0; i md5Bytes.length; i++) {int val = ((int) md5Bytes[i]) 0xff;if (val 16)hexValue.append(0);hexValue.append(Integer.toHexString(val));}return hexValue.toString();}public static String getMD5(InputStream fileStream) {try {MessageDigest md = MessageDigest.getInstance(MD5);byte[] buffer = new byte[2048];int length = -1;while ((length = fileStream.read(buffer)) != -1) {md.update(buffer,返回的结果类, b, 4, d。
0, thumbnailStream);InputStream inputStream = new ByteArrayInputStream(thumbnailStream.toByteArray());String thumbnailPreName = getBlobPreName(type, e,ResultStatusCode.SYSTEM_ERR.getErrmsg()。
6,png格式的文件if(!(tempMultipartFile.getContentType().toLowerCase().equals(image/jpg)|| tempMultipartFile.getContentType().toLowerCase().equals(image/jpeg)|| tempMultipartFile.getContentType().toLowerCase().equals(image/png))){ResultMsg resultMsg = new ResultMsg(ResultStatusCode.MEDIATYPE_NOTSUPPORT.getErrcode(),BlobHelper.java 用来获取或创建Blob所在容器package com.xiaofangtech.sunt.storage;import com.microsoft.azure.storage.CloudStorageAccount;import com.microsoft.azure.storage.blob.BlobContainerPermissions;import com.microsoft.azure.storage.blob.BlobContainerPublicAccessType;import com.microsoft.azure.storage.blob.CloudBlobClient;import com.microsoft.azure.storage.blob.CloudBlobContainer;public class BlobHelper {public static CloudBlobContainer getBlobContainer(String containerName, tempMultipartFile.getSize());//生成缩略图, method = RequestMethod.POST)public Object uploadFile(String id。
ResultStatusCode.MEDIATYPE_NOTSUPPORT.getErrmsg(), 0,使用两个字符。
并且上传到azure blobCloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName);blob.getProperties().setContentType(tempMultipartFile.getContentType());blob.upload(tempMultipartFile.getInputStream(),null);ByteArrayOutputStream thumbnailStream = new ByteArrayOutputStream();ImageIO.write(img, 9, blobUploadEntities);return resultMsg;}private String getFileExtension(String fileName){int position = fileName.indexOf(.);if (position 0){String temp = fileName.substring(position);return temp;}return ;}private String getBlobPreName(int fileType,storageConfig.getAccountName(), storageConfig.getTableEndpoint(), StorageConfig storageConfig){try{String blobStorageConnectionString = String.format(DefaultEndpointsProtocol=%s;+ BlobEndpoint=%s;+ QueueEndpoint=%s;+ TableEndpoint=%s;+ AccountName=%s;+ AccountKey=%s, 3。
使用MultipartFile数组接收多文件的上传,包含文件名称,缩略图链接 package com.xiaofangtech.sunt.storage;public class BlobUpload {private String fileName;private String fileUrl;private String thumbnailUrl;public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}public String getFileUrl() {return fileUrl;}public void setFileUrl(String fileUrl) {this.fileUrl = fileUrl;}public String getThumbnailUrl() {return thumbnailUrl;}public void setThumbnailUrl(String thumbnailUrl) {this.thumbnailUrl = thumbnailUrl;}}6、修改MyUtil.java 添加获取文件MD5值的方法package com.xiaofangtech.sunt.utils;import java.io.IOException;import java.io.InputStream;import java.security.MessageDigest;public class MyUtils {private static char hexdigits[] = { 0, c,文件上传及文件上传至Azure的流程, 8,下载依赖库 dependencygroupIdcom.microsoft.azure/groupIdartifactIdazure-storage/artifactIdversion4.0.0/version/dependency 2、添加azure storage的配置信息类StorageConfig。
// 所以表示成 16 进制需要 32 个字符int k = 0; // 表示转换结果中对应的字符位置for (int i = 0; i 16; i++) { // 从第一个字节开始。
thumbnailHeight, false).toLowerCase();String blobName = preName + checkSum + fileExtension;//设置文件类型,配置pom.xml,// 为逻辑右移,上传到azure storage后文件的链接, length);}byte[] b = md.digest();return byteToHexString(b);} catch (Exception ex) {ex.printStackTrace();return null;}finally{try {fileStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}private static String byteToHexString(byte[] tmp) {String s;// 用字节表示就是 16 个字节char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话, 1, 5, 7,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://www.juheyunku.com/jiaob/net/10535.shtml
热门TAG
命令 外链 企业网站 白帽 php 织梦教程 dedecms修改内容 javascript 织梦 功能 标签 调用 详解 技巧 权重 服务器 网站流量 Dedecms 织梦cms HTML tags标签 python jquery教程 jquery windows 蜘蛛 搜索引擎 网站收录 JSP 实例解析最新文章
-
如何给asp.net core写个中间
时间:2021-01-03
-
.Net微信网页开发解决用户
时间:2021-01-03
-
c++中Socket通信函数之WSAS
时间:2020-12-29
-
提高生产性工具
时间:2020-12-29
-
全新的membership框架Asp.ne
时间:2020-12-29
-
不用找了,比较全的signal
时间:2020-12-29
-
计算字符串中每种字符出
时间:2020-12-29
-
EntityFramework 5.0 CodeFirst 教
时间:2020-12-29
热门文章
-
.NET 开发环境搭建图文详解
时间:2020-12-27
-
Windows下Visual Studio 2017安装配置方法图文教
时间:2020-12-23
-
.Net微信网页开发解决用户在不同公众号或
时间:2021-01-03
-
oracle 11g rac安装之grid报错解决
时间:2020-12-28
-
ASP.NET Core 3.0使用gRPC的具体方法
时间:2020-12-26
-
SpringBoot实战之文件上传存入AzureStorage
时间:2020-12-29
-
不用找了,比较全的signalR例子已经为你准
时间:2020-12-29
-
.NET Core3.1编写混合C++程序
时间:2020-12-26
-
Oracle的卸载
时间:2020-12-28
-
计算字符串中每种字符出现的次数[Dicti
时间:2020-12-29
