|
@@ -1,6 +1,7 @@
|
|
package cn.com.qmth.examcloud.exchange.inner.service.upyun;
|
|
package cn.com.qmth.examcloud.exchange.inner.service.upyun;
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
+import java.io.FileInputStream;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.security.MessageDigest;
|
|
import java.security.MessageDigest;
|
|
@@ -11,6 +12,7 @@ import java.util.Locale;
|
|
import java.util.TimeZone;
|
|
import java.util.TimeZone;
|
|
|
|
|
|
import org.apache.commons.compress.utils.IOUtils;
|
|
import org.apache.commons.compress.utils.IOUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.http.HttpStatus;
|
|
import org.apache.http.HttpStatus;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
@@ -20,7 +22,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
-import com.UpYun;
|
|
|
|
import com.upyun.UpException;
|
|
import com.upyun.UpException;
|
|
import com.upyun.UpYunUtils;
|
|
import com.upyun.UpYunUtils;
|
|
|
|
|
|
@@ -63,6 +64,8 @@ public class UpYunClient {
|
|
|
|
|
|
private final String AUTHORIZATION = "Authorization";
|
|
private final String AUTHORIZATION = "Authorization";
|
|
|
|
|
|
|
|
+ private final String SEPARATOR = "/";
|
|
|
|
+
|
|
private CloseableHttpClient httpclient;
|
|
private CloseableHttpClient httpclient;
|
|
|
|
|
|
private RequestConfig requestConfig;
|
|
private RequestConfig requestConfig;
|
|
@@ -96,15 +99,15 @@ public class UpYunClient {
|
|
*/
|
|
*/
|
|
public String writeFile(String filePath, File file) {
|
|
public String writeFile(String filePath, File file) {
|
|
|
|
|
|
- UpYun upYun = new UpYun(bucketName, userName, password);
|
|
|
|
|
|
+ InputStream in = null;
|
|
try {
|
|
try {
|
|
- upYun.writeFile(filePath, file, true);
|
|
|
|
|
|
+ in = new FileInputStream(file);
|
|
|
|
+ return writeFile(filePath, in);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
throw new StatusException("EX-100001", "upyun upload failure");
|
|
throw new StatusException("EX-100001", "upyun upload failure");
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(in);
|
|
}
|
|
}
|
|
-
|
|
|
|
- String path = " http://" + bucketName + ".b0.upaiyun.com" + filePath;
|
|
|
|
- return path;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -116,7 +119,7 @@ public class UpYunClient {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public String writeFile(String filePath, InputStream in) {
|
|
public String writeFile(String filePath, InputStream in) {
|
|
-
|
|
|
|
|
|
+ filePath = formatPath(filePath);
|
|
String url = "http://" + API_DOMAIN + filePath;
|
|
String url = "http://" + API_DOMAIN + filePath;
|
|
|
|
|
|
HttpPut httpPut = new HttpPut(url);
|
|
HttpPut httpPut = new HttpPut(url);
|
|
@@ -154,6 +157,31 @@ public class UpYunClient {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 格式化路径参数,去除前后的空格并确保以"/"开头,最后添加"/空间名"
|
|
|
|
+ * <p>
|
|
|
|
+ * 最终构成的格式:"/空间名/文件路径"
|
|
|
|
+ *
|
|
|
|
+ * @param path
|
|
|
|
+ * 目录路径或文件路径
|
|
|
|
+ * @return 格式化后的路径
|
|
|
|
+ */
|
|
|
|
+ private String formatPath(String path) {
|
|
|
|
+
|
|
|
|
+ if (StringUtils.isNotBlank(path)) {
|
|
|
|
+
|
|
|
|
+ // 去除前后的空格
|
|
|
|
+ path = path.trim();
|
|
|
|
+
|
|
|
|
+ // 确保路径以"/"开头
|
|
|
|
+ if (!path.startsWith(SEPARATOR)) {
|
|
|
|
+ return SEPARATOR + bucketName + SEPARATOR + path;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return SEPARATOR + bucketName + path;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 对字符串进行 MD5 加密
|
|
* 对字符串进行 MD5 加密
|
|
*
|
|
*
|