|
@@ -1,7 +1,6 @@
|
|
|
package com.izouma.awesomeadmin.service;
|
|
package com.izouma.awesomeadmin.service;
|
|
|
|
|
|
|
|
-import java.io.InputStream;
|
|
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
|
|
|
|
+import java.io.*;
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.Random;
|
|
import java.util.Random;
|
|
@@ -15,6 +14,10 @@ import com.aliyun.oss.model.ObjectMetadata;
|
|
|
import com.aliyun.oss.model.PutObjectResult;
|
|
import com.aliyun.oss.model.PutObjectResult;
|
|
|
import com.izouma.awesomeadmin.util.ImagesUtil;
|
|
import com.izouma.awesomeadmin.util.ImagesUtil;
|
|
|
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
|
|
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
|
|
@Service
|
|
|
public class OSSFileService {
|
|
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");
|
|
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("/")) {
|
|
if (path.startsWith("/")) {
|
|
|
path = path.replaceFirst("\\\\/", "");
|
|
path = path.replaceFirst("\\\\/", "");
|
|
|
}
|
|
}
|
|
@@ -52,12 +55,50 @@ public class OSSFileService {
|
|
|
return null;
|
|
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) {
|
|
private static String getFullPath(String Path) {
|
|
|
if (Path == null || "".equals(Path)) {
|
|
if (Path == null || "".equals(Path)) {
|
|
|
return Path;
|
|
return Path;
|
|
|
}
|
|
}
|
|
|
- String aliossendpoit = PropertiesFileLoader.getDefaultProperties("aliImageSever", "");
|
|
|
|
|
|
|
+// String aliossendpoit = PropertiesFileLoader.getDefaultProperties("aliImageSever", "");
|
|
|
|
|
+ String aliossendpoit = PropertiesFileLoader.getDefaultProperties("base_domain", "");
|
|
|
return aliossendpoit + "/" + Path;
|
|
return aliossendpoit + "/" + Path;
|
|
|
}
|
|
}
|
|
|
|
|
|