Spring mvc+oss存儲+fileupload多文件上傳實現SSO單點登陸模板管理

以前給你們介紹了sso的相關知識點和集成方案,考慮到每一個系統所屬行業的不一樣,這邊針對於不一樣行業作了一些統一的sso單點登陸界面模板,使用fileupload多文件上傳+OSS阿里雲存儲方案。數據庫

1. 阿里雲oss存儲Utils數組

Java代碼 收藏代碼
  1. public class AliyunUtils {
  2. private static AliyunUtils aliyun;
  3. private AliyunUtils() {
  4. }
  5. public static synchronized AliyunUtils getInstance(){
  6. if(aliyun==null){
  7. aliyun=new AliyunUtils();
  8. }
  9. return aliyun;
  10. }
  11. /**
  12. * 上傳byte數組
  13. * @param fileByte
  14. * @param fileKey
  15. */
  16. public void uploadByte(byte[] fileByte, String fileKey){
  17. // 建立OSSClient實例
  18. OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);
  19. // 上傳byte數組
  20. ossClient.putObject(CloudConstant.BUCKET, fileKey, new ByteArrayInputStream(fileByte));
  21. // 關閉client
  22. ossClient.shutdown();
  23. }
  24. /**
  25. * 上傳文件流
  26. * @param inputStream
  27. * @param fileKey
  28. */
  29. public void uploadInputStream(InputStream inputStream, String fileKey){
  30. // 建立OSSClient實例
  31. OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);
  32. // 上傳文件流
  33. ossClient.putObject(CloudConstant.BUCKET, fileKey, inputStream);
  34. // 關閉client
  35. ossClient.shutdown();
  36. }
  37. /**
  38. * 刪除文件
  39. * @param fileKey
  40. */
  41. public void deleteFile(String fileKey){
  42. // 建立OSSClient實例
  43. OSSClient ossClient = new OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);
  44. // 刪除文件
  45. ossClient.deleteObject(CloudConstant.BUCKET, fileKey);
  46. // 關閉client
  47. ossClient.shutdown();
  48. }
  49. //base64字符串轉化成圖片
  50. @SuppressWarnings("restriction")
  51. public static byte[] BASE64DecoderStringToByte(String imgStr)
  52. { //對字節數組字符串進行Base64解碼並生成圖片
  53. if (imgStr == null) //圖像數據爲空
  54. return null;
  55. sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
  56. try {
  57. //Base64解碼
  58. byte[] b = decoder.decodeBuffer(imgStr);
  59. return b;
  60. } catch (Exception e){
  61. return null;
  62. }
  63. }
  64. public static void main(String[] args) {
  65. //AliyunUtils.getInstance().uploadByte(BASE64DecoderStringToByte(base64), "aabb.jpg");
  66. }
  67. }

2. 阿里雲配置常量類(能夠配置到數據庫或者properties,後面會更新配置方式)ui

Java代碼 收藏代碼
  1. public class CloudConstant {
  2. /****************阿里雲OSS上傳文件配置****************/
  3. public static final String ENDPOINT = "http://oss-cn-shanghai.aliyuncs.com"; //外網訪問域名
  4. //public static final String ENDPOINT = "http://oss-cn-shanghai-internal.aliyuncs.com"; //內網訪問域名
  5. public static final String ACCESSKEYID = "12345678qwertyu; //標識用戶
  6. public static final String ACCESSKEYSECRET = "1234567890WERTYUIO"; //加密簽名字符
  7. public static final String BUCKET = "huiyi-bucket"; //存儲空間
  8. /****************背景文件路徑配置****************/
  9. public static final String BACK_IMG_INFO_PATH = "sso/backageImg/";
  10. }

3. sso templateController類阿里雲

Java代碼 收藏代碼
  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. }
  5. String fileName = String.valueOf(new Date().getTime());
  6. String staff = "";
  7. String fileKey = "";
  8. // 上傳文件
  9. if (file.getSize() != 0) {
  10. staff = FilenameUtils.getExtension(file.getOriginalFilename());
  11. fileKey = CloudConstant.BACK_IMG_INFO_PATH + fileName + "." + staff;
  12. // 刪除OSS文件
  13. AliyunUtils.getInstance().deleteFile(fileKey);
  14. // 上傳文件至阿里雲OSS
  15. try {
  16. AliyunUtils.getInstance().uploadInputStream(file.getInputStream(), fileKey);
  17. ssoTemplate.setImg(fileKey);
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. ssoTemplateService.save(ssoTemplate);
  23. addMessage(redirectAttributes, "保存模板成功");
  24. return "redirect:" + Global.getAdminPath() + "/sso/ssoTemplate";
  25. }

4. 界面顯示效果加密








相關文章
相關標籤/搜索