Spring mvc+oss存储+fileupload多文件上传实现SSO单点登录模板管理
文章目录
之前给大家介绍了sso的相关知识点和集成方案,考虑到每个系统所属行业的不同,这边针对于不同行业做了一些统一的sso单点登录界面模板,使用fileupload多文件上传+OSS阿里云存储方案。
(企业架构源码可以加求球:叁五三陆二肆柒二伍玖)
- 阿里云oss存储Utils
1. public class AliyunUtils {
3. private static AliyunUtils aliyun;
4. private AliyunUtils() {
6. }
8. public static synchronized AliyunUtils getInstance(){
9. if(aliyun==null){
10. aliyun=new AliyunUtils();
11. }
12. return aliyun;
13. }
15. /**
16. * 上传byte数组
17. * @param fileByte
18. * @param fileKey
19. */
20. public void uploadByte(byte[] fileByte, String fileKey){
21. // 创建OSSClient实例
22. OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);
23. // 上传byte数组
24. ossClient.putObject(CloudConstant.BUCKET, fileKey, new ByteArrayInputStream(fileByte));
25. // 关闭client
26. ossClient.shutdown();
27. }
29. /**
30. * 上传文件流
31. * @param inputStream
32. * @param fileKey
33. */
34. public void uploadInputStream(InputStream inputStream, String fileKey){
35. // 创建OSSClient实例
36. OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);
37. // 上传文件流
38. ossClient.putObject(CloudConstant.BUCKET, fileKey, inputStream);
39. // 关闭client
40. ossClient.shutdown();
41. }
43. /**
44. * 删除文件
45. * @param fileKey
46. */
47. public void deleteFile(String fileKey){
48. // 创建OSSClient实例
49. OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);
50. // 删除文件
51. ossClient.deleteObject(CloudConstant.BUCKET, fileKey);
52. // 关闭client
53. ossClient.shutdown();
54. }
56. //base64字符串转化成图片
57. @SuppressWarnings("restriction")
58. public static byte[] BASE64DecoderStringToByte(String imgStr)
59. { //对字节数组字符串进行Base64解码并生成图片
60. if (imgStr == null) //图像数据为空
61. return null;
62. sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
63. try {
64. //Base64解码
65. byte[] b = decoder.decodeBuffer(imgStr);
66. return b;
67. } catch (Exception e){
68. return null;
69. }
70. }
72. public static void main(String[] args) {
73. //AliyunUtils.getInstance().uploadByte(BASE64DecoderStringToByte(base64), "aabb.jpg");
74. }
75. }- 阿里云配置常量类(可以配置到数据库或者properties,后面会更新配置方式)
1. public class CloudConstant {
3. /****************阿里云OSS上传文件配置****************/
4. public static final String ENDPOINT = "http://oss-cn-shanghai.aliyuncs.com"; //外网访问域名
5. //public static final String ENDPOINT = "http://oss-cn-shanghai-internal.aliyuncs.com"; //内网访问域名
6. public static final String ACCESSKEYID = "12345678qwertyu; //标识用户
7. public static final String ACCESSKEYSECRET = "1234567890WERTYUIO"; //加密签名字符
8. public static final String BUCKET = "huiyi-bucket"; //存储空间
10. /****************背景文件路径配置****************/
11. public static final String BACK_IMG_INFO_PATH = "sso/backageImg/";
13. }- sso templateController类
1. public String save(SsoTemplate ssoTemplate, Model model, RedirectAttributes redirectAttributes, @RequestParam(value = "file", required = false) MultipartFile file) {
2. if (!beanValidator(model, ssoTemplate)) {
3. return form(ssoTemplate, model);
4. }
6. String fileName = String.valueOf(new Date().getTime());
7. String staff = "";
8. String fileKey = "";
9. // 上传文件
10. if (file.getSize() != 0) {
11. staff = FilenameUtils.getExtension(file.getOriginalFilename());
12. fileKey = CloudConstant.BACK_IMG_INFO_PATH + fileName + "." + staff;
13. // 删除OSS文件
14. AliyunUtils.getInstance().deleteFile(fileKey);
15. // 上传文件至阿里云OSS
16. try {
17. AliyunUtils.getInstance().uploadInputStream(file.getInputStream(), fileKey);
18. ssoTemplate.setImg(fileKey);
19. } catch (IOException e) {
20. e.printStackTrace();
21. }
22. }
24. ssoTemplateService.save(ssoTemplate);
25. addMessage(redirectAttributes, "保存模板成功");
26. return "redirect:" + Global.getAdminPath() + "/sso/ssoTemplate";
27. }
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论