1
0
suochencheng 7 лет назад
Родитель
Сommit
a7cb76beb5

+ 46 - 5
src/main/java/com/izouma/awesomeadmin/service/OSSFileService.java

@@ -1,7 +1,6 @@
 package com.izouma.awesomeadmin.service;
 
-import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Random;
@@ -15,6 +14,10 @@ import com.aliyun.oss.model.ObjectMetadata;
 import com.aliyun.oss.model.PutObjectResult;
 import com.izouma.awesomeadmin.util.ImagesUtil;
 import com.izouma.awesomeadmin.util.PropertiesFileLoader;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.servlet.http.HttpServletRequest;
 
 @Service
 public class OSSFileService {
@@ -22,10 +25,10 @@ public class OSSFileService {
     /**
      * 日志对象
      */
-    private static       Logger logger      = Logger.getLogger(OSSFileService.class);
+    private static Logger logger = Logger.getLogger(OSSFileService.class);
     private static final String projectName = PropertiesFileLoader.getProperties("projectname");
 
-    public String upload(InputStream fin, String path) {
+    public String uploadOss(InputStream fin, String path) {
         if (path.startsWith("/")) {
             path = path.replaceFirst("\\\\/", "");
         }
@@ -52,12 +55,50 @@ public class OSSFileService {
         return null;
     }
 
+    public String upload(InputStream fin, String imageName) {
+
+        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+
+        String uploadPath = request.getSession().getServletContext().getRealPath(projectName);
+
+        String imgUrlPath = "";
+
+
+        try {
+
+
+            File file = new File(uploadPath + "/" + imageName.split("/")[0]);
+            if (!file.exists() && !file.isDirectory())
+                file.mkdirs();
+
+            InputStream in = fin;
+            OutputStream out = new FileOutputStream(uploadPath + "/" + imageName, true);
+            byte b[] = new byte[1024];
+            int len = 0;
+            while ((len = in.read(b)) != -1) {
+                out.write(b, 0, len);
+            }
+            in.close();
+            out.close();
+            System.out.println("存入照片:" + uploadPath + "/" + imageName);
+            imgUrlPath = projectName + "/" + imageName;
+        } catch (Exception r) {
+            logger.error("OSS上传异常:", r);
+        }
+        // 如果不设置content-length, 默认为chunked编码。
+        if (imgUrlPath != null) {
+            return getFullPath(imgUrlPath);
+        }
+        return null;
+    }
+
 
     private static String getFullPath(String Path) {
         if (Path == null || "".equals(Path)) {
             return Path;
         }
-        String aliossendpoit = PropertiesFileLoader.getDefaultProperties("aliImageSever", "");
+//        String aliossendpoit = PropertiesFileLoader.getDefaultProperties("aliImageSever", "");
+        String aliossendpoit = PropertiesFileLoader.getDefaultProperties("base_domain", "");
         return aliossendpoit + "/" + Path;
     }
 

+ 2 - 1
src/main/resources/properties/outsidews.properties

@@ -10,4 +10,5 @@ aliImageSever=http://oss.izouma.com
 rongyunappkey=pwe86ga5ee376
 rongyunappsecret=ARj0hyU5Cp
 jwtsecret=3MgWy/pJjxHCQW2pHlQbkjomTUPRo9WhsPFjUPYZZlo=
-host=http://1eaed69e.ngrok.io
+host=http://1eaed69e.ngrok.io
+base_domain=http://121.43.171.110:8080