WANG 6 gadi atpakaļ
vecāks
revīzija
4ef71e315a

+ 58 - 1
examcloud-exchange-inner-service/src/main/java/cn/com/qmth/examcloud/exchange/inner/service/upyun/UpYunClient.java

@@ -17,6 +17,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpStatus;
 import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpPut;
 import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
@@ -174,7 +175,7 @@ public class UpYunClient {
 
 			if (HttpStatus.SC_OK != statusCode) {
 				log.error("[upyun error] " + EntityUtils.toString(response.getEntity(), "UTF-8"));
-				throw new StatusException("100001", "upyun upload failure");
+				throw new StatusException("100001", "[upyun]. fail to write file");
 			}
 		} catch (StatusException e) {
 			throw e;
@@ -197,6 +198,62 @@ public class UpYunClient {
 		return new UpYunPathInfo(fileUrl, filePath);
 	}
 
+	/**
+	 * 删除文件
+	 *
+	 * @author WANGWEI
+	 * @param filePath
+	 * @return
+	 */
+	public void deleteFile(String filePath) {
+		String path = formatPath(filePath);
+		String url = "https://" + API_DOMAIN + path;
+		if (filePath.endsWith(".test")) {
+			url = testUrl;
+		}
+
+		HttpDelete httpDelete = new HttpDelete(url);
+		httpDelete.setConfig(this.requestConfig);
+		CloseableHttpResponse response = null;
+
+		long s = System.currentTimeMillis();
+		try {
+
+			String date = getGMTDate();
+			httpDelete.addHeader(DATE, date);
+
+			if (unsafe) {
+				httpDelete.addHeader(AUTHORIZATION, base64Auth);
+			} else {
+				httpDelete.addHeader(AUTHORIZATION, UpYunUtils
+						.sign(METHOD_PUT, date, path, userName, this.md5Password, null).trim());
+			}
+
+			response = httpclient.execute(httpDelete);
+			int statusCode = response.getStatusLine().getStatusCode();
+
+			if (HttpStatus.SC_OK != statusCode) {
+				log.error("[upyun error] " + EntityUtils.toString(response.getEntity(), "UTF-8"));
+				throw new StatusException("100002", "[upyun]. fail to delete file");
+			}
+		} catch (StatusException e) {
+			throw e;
+		} catch (UpException e) {
+			throw new ExamCloudRuntimeException(e);
+		} catch (Exception e) {
+			throw new ExamCloudRuntimeException(e);
+		} finally {
+			IOUtils.closeQuietly(response);
+			httpDelete.releaseConnection();
+		}
+
+		if (log.isDebugEnabled()) {
+			log.debug("[upyun]. path=" + path + "; cost " + (System.currentTimeMillis() - s)
+					+ " ms.");
+		}
+
+	}
+
 	/**
 	 * 格式化路径参数,去除前后的空格并确保以"/"开头,最后添加"/空间名"
 	 * <p>