Ucloud 文件上传
关注 “弋凡”(YiFan)微信公众号吧 记录简单笔记 做你的最爱
创建一个空间 创建一个令牌
查看官方文档说明
https://github.com/ucloud/ufile-sdk-java
上传文件
File file = new File("your file path");try {PutObjectResultBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config).putObject(file, "mimeType").nameAs("save as keyName").toBucket("upload to which bucket")/*** 是否上传校验MD5, Default = true*/// .withVerifyMd5(false)/*** 指定progress callback的间隔, Default = 每秒回调*/// .withProgressConfig(ProgressConfig.callbackWithPercent(10))/*** 配置进度监听*/.setOnProgressListener(new OnProgressListener() {@Overridepublic void onProgress(long bytesWritten, long contentLength) {}}).execute();
} catch (UfileClientException e) {e.printStackTrace();
} catch (UfileServerException e) {e.printStackTrace();
}
案例
- 导入pom文件
<dependency><groupId>cn.ucloud.ufile</groupId><artifactId>ufile-client-java</artifactId><version>2.4.4</version>
</dependency>
application.properties
编写
# ucloud 你创建的令牌 以及你的空间名
ucloud.ufile.publickey=TOKEN_xfd2a2-6fx8-4c6c-8af6-502x966x8xxxx
ucloud.ufile.privatekey=1e077ax2-a923-4a1x-bbea-9x2d8x32xxxx
ucloud.bucketName=zfan
UcloudProvider
编写
import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.auth.ObjectAuthorization;
import cn.ucloud.ufile.auth.UfileObjectLocalAuthorization;
import cn.ucloud.ufile.bean.PutObjectResultBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileServerException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;import java.io.File;
import java.io.InputStream;
import java.util.UUID;@Service
public class UcloudProvider {@Value("${ucloud.ufile.publickey}")private String publicKey;@Value("${ucloud.ufile.privatekey}")private String privateKey;@Value("${ucloud.bucketName}")private String bucketName;public String upload(InputStream inputStream, String mimeType,String fileName){File file = new File("your file path");String newName = "";String[] split = fileName.split("\\.");if(split.length>1){newName =UUID.randomUUID()+"."+split[split.length-1];}else {return null;}try {// Bucket相关API的授权器ObjectAuthorization objectAuthorization = new UfileObjectLocalAuthorization(publicKey,privateKey);// 对象操作需要ObjectConfig来配置您的地区和域名后缀ObjectConfig config = new ObjectConfig("cn-bj", "ufileos.com");bucketName = "zfan";PutObjectResultBean response = UfileClient.object(objectAuthorization, config).putObject(inputStream, mimeType).nameAs(newName).toBucket(bucketName).setOnProgressListener((bytesWritten, contentLength) -> { }).execute();if(response!=null && response.getRetCode() == 0 ){String url = UfileClient.object(objectAuthorization, config).getDownloadUrlFromPrivateBucket(newName, bucketName, 24 * 60 * 60).createUrl();return url;}else {return "文件上传失败";}} catch (UfileClientException e) {e.printStackTrace();return null;} catch (UfileServerException e) {e.printStackTrace();return null;}}
}
controller
编写
// file.getOriginalFilename()是得到上传时的文件名
// file.getContentType()文件类型
@RestController
public class UploadController {@Autowiredprivate UcloudProvider ucloudProvider;@RequestMapping("upload")public String upload(HttpServletRequest request) throws IOException {MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;MultipartFile file = multipartRequest.getFile("file");String upload = ucloudProvider.upload(file.getInputStream(), file.getContentType(), file.getOriginalFilename());return upload;}}
- 上传页面编写
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h4>首页</h4>
<form action="/upload" method="post" enctype="multipart/form-data"><p><input type="file" name="file"></p><p><input type="submit" value="提交"></p>
</form></body>
</html>
end -
快来关注“弋凡”微信公众号吧
Ucloud 文件上传相关推荐
- html上传文件框架,文件上传 - FastAdmin框架文档 - FastAdmin开发文档
文件上传 最后更新时间:2021-01-15 17:29:11 FastAdmin支持将文件.图片.视频.压缩包等文件快速的上传至本地服务器或云存储,同时支持云存储直传模式和服务器进行中转模式. 你可 ...
- springboot设置文件上传大小(tomcat默认1M)
application.yml # 设置文件上传大小(tomcat默认1M) server:tomcat:max-http-form-post-size: -1 spring:servlet:mult ...
- 将文件上传至ftp服务器,FTP文件上传工具类,将文件上传至服务器指定目录
将文件上传至ftp服务器,传入File对象,将文件上传至ftp服务器 需要配置修改的点: 1. 服务器ip端口(服务器ip 端口22/21). 2. 服务器账号密码(服务器登录用户名密码). 3. 上 ...
- Springboot 多文件上传
其实多个文件和单个文件上传是一样的,可以使用同一个Controller 添加依赖 <!-- https://mvnrepository.com/artifact/commons-fileuplo ...
- django文件上传
Django在处理文件上传时,文件数据被打包封装在request.FILES中. 一.简单上传 首先,写一个form模型,它必须包含一个FileField: # forms.py from djang ...
- spring boot 文件上传工具类(bug 已修改)
以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...
- 模拟文件上传(一):手动文件上传
关于上传文件,首先我的第一个案例是一个文本文件的上传,简单容易上手! 首先我们上传文件肯定就属于实体内容部分了:所以不能过GET方式请求了,要通过POST方式请求: 因为: 1.get方式是URL传值 ...
- Java中的文件上传2(Commons FileUpload:commons-fileupload.jar)
相比上一篇使用Servlet原始去实现的文件上传(http://www.cnblogs.com/EasonJim/p/6554669.html),使用组件去实现相对来说功能更多,省去了很多需要配置和处 ...
- 转 [JAVA] 使用 common-fileupload 实现文件上传
就在前段时间,还在苦于找到不到合适的上传组件,虽然很早就知道了 common-fileupload,但当时却因为没有找到如何获取表单参数的方法而使用 jspSmartUpload,历尽艰辛终于找到了它 ...
最新文章
- 你认为是宽渠道好还是窄渠道好?请做具体分析
- CF372D. Choosing Subtree is Fun
- BugkuCTF-Reverse题逆向入门
- [转]CTO谈豆瓣网和校内网技术架构变迁
- 《Tensorflow 实战google深度学习框架》第二版源代码
- andorid 回调的理解
- 后羿射日般的精准 - 阿里云ECS调度是如何炼成的
- android gps信号一直获取不到_抵押车的GPS到底能不能拆干净!
- Arduino相关语法和函数
- c语言贺卡代码大全,C++实现新年贺卡程序
- How the System Finds and Loads Drivers
- 计算机设备系统包括哪些,计算机硬件系统包括哪些
- Excel改变了你的基因名,30% 相关Nature文章受影响,NCBI也受波及
- 国内做HIS(医院信息化软件)常见的厂商与产品的比较
- meadow显示服务器断开,Meadow(草甸)中文全成就指南
- 去掉Holo主题下Dialog的蓝色线
- LaTeX中手动修改参考文献格式
- Linux下海康威视工业相机的SDK二次开发
- lodash 学习记录
- 阐述一下ecshop各模块的功能_【光电通信】40G QSFP+ LR4 光模块的设计与实现(一)...