|
|
@@ -7,12 +7,16 @@ import com.izouma.awesomeadmin.util.ImagesUtil;
|
|
|
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.log4j.Logger;
|
|
|
+import org.hibernate.validator.internal.util.privilegedactions.GetClassLoader;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
@@ -32,7 +36,7 @@ public class OSSFileService {
|
|
|
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("\\\\/", "");
|
|
|
}
|
|
|
@@ -59,8 +63,56 @@ 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 t = Thread.currentThread().getContextClassLoader().getResource("").getPath();
|
|
|
+ String uploadPath = t.replace("WEB-INF/classes/", "") + 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("base_domain", "");
|
|
|
+ return aliossendpoit + "/" + Path;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String getFullPathOss(String Path) {
|
|
|
if (Path == null || "".equals(Path)) {
|
|
|
return Path;
|
|
|
}
|