|
@@ -1,107 +1,100 @@
|
|
|
-/*
|
|
|
- * *************************************************
|
|
|
- * Copyright (c) 2018 QMTH. All Rights Reserved.
|
|
|
- * Created by Deason on 2018-11-01 17:02:06.
|
|
|
- * *************************************************
|
|
|
- */
|
|
|
-
|
|
|
-package cn.com.qmth.examcloud.core.print.common.upyun;
|
|
|
-
|
|
|
-import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
-import cn.com.qmth.examcloud.core.print.common.SysProperty;
|
|
|
-import cn.com.qmth.examcloud.core.print.common.utils.Check;
|
|
|
-import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
|
|
|
-import cn.com.qmth.examcloud.web.config.SystemProperties;
|
|
|
-import main.java.com.UpYun;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-
|
|
|
-import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_400;
|
|
|
-import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_500;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author: QMTH
|
|
|
- * @since: 2018/11/1
|
|
|
- */
|
|
|
-@Component
|
|
|
-public class UpYunClient {
|
|
|
-
|
|
|
- private static final Logger log = LoggerFactory.getLogger(UpYunClient.class);
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private SystemProperties systemProperties;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private SysProperty sysProperty;
|
|
|
-
|
|
|
- public String upload(File file) {
|
|
|
- Check.isEmpty(file, "上传的文件不能为空!");
|
|
|
- if (!file.exists() || !file.isFile()) {
|
|
|
- throw new StatusException(SYS_CODE_400, "上传的文件不存在!");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- final String newFileName = FileUtils.newFileName(file.getName());
|
|
|
- final String newFilePath = sysProperty.getUploadUrl() + FileUtils.dateDir() + newFileName;
|
|
|
- this.getInstance().writeFile(newFilePath, file, true);
|
|
|
-
|
|
|
- //成功后,返回文件访问地址
|
|
|
- return this.getUrlPrefix() + newFilePath;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- throw new StatusException(SYS_CODE_500, "上传文件异常!");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public String upload(byte[] bytes, String originalFilename) {
|
|
|
- Check.isEmpty(bytes, "上传的文件不能为空!");
|
|
|
- Check.isBlank(originalFilename, "上传的文件名不正确!");
|
|
|
-
|
|
|
- try {
|
|
|
- final String newFileName = FileUtils.newFileName(originalFilename);
|
|
|
- final String newFilePath = sysProperty.getUploadUrl() + FileUtils.dateDir() + newFileName;
|
|
|
- this.getInstance().writeFile(newFilePath, bytes, true);
|
|
|
-
|
|
|
- //成功后,返回文件访问地址
|
|
|
- return this.getUrlPrefix() + newFilePath;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- throw new StatusException(SYS_CODE_500, "上传文件异常!");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public File download(String filePath) {
|
|
|
- try {
|
|
|
- //final String rootDir = Constants.rootFileDir();
|
|
|
- final String rootDir = systemProperties.getTempDataDir();
|
|
|
- FileUtils.makeDirs(rootDir);
|
|
|
- final String newFilePath = rootDir + "/" + FileUtils.newFileName(filePath);
|
|
|
- File file = new File(newFilePath);
|
|
|
- this.getInstance().readFile(filePath, file);
|
|
|
- return file;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public UpYun getInstance() {
|
|
|
- UpYun upyun = new UpYun(sysProperty.getBucketName(), sysProperty.getUserName(), sysProperty.getPassword());
|
|
|
- upyun.setDebug(true);
|
|
|
- upyun.setApiDomain(UpYun.ED_AUTO);
|
|
|
- return upyun;
|
|
|
- }
|
|
|
-
|
|
|
- public String getUrlPrefix() {
|
|
|
- return sysProperty.getDomain();
|
|
|
- }
|
|
|
-
|
|
|
- public SysProperty getProperty() {
|
|
|
- return sysProperty;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+// package cn.com.qmth.examcloud.core.print.common.upyun;
|
|
|
+//
|
|
|
+// import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
+// import cn.com.qmth.examcloud.core.print.common.SysProperty;
|
|
|
+// import cn.com.qmth.examcloud.core.print.common.utils.Check;
|
|
|
+// import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
|
|
|
+// import cn.com.qmth.examcloud.web.config.SystemProperties;
|
|
|
+// import main.java.com.UpYun;
|
|
|
+// import org.slf4j.Logger;
|
|
|
+// import org.slf4j.LoggerFactory;
|
|
|
+// import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+// import org.springframework.stereotype.Component;
|
|
|
+//
|
|
|
+// import java.io.File;
|
|
|
+//
|
|
|
+// import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_400;
|
|
|
+// import static cn.com.qmth.examcloud.core.print.common.Constants.SYS_CODE_500;
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * @author: QMTH
|
|
|
+// * @since: 2018/11/1
|
|
|
+// */
|
|
|
+// @Component
|
|
|
+// public class UpYunClient {
|
|
|
+//
|
|
|
+// private static final Logger log = LoggerFactory.getLogger(UpYunClient.class);
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// private SystemProperties systemProperties;
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// private SysProperty sysProperty;
|
|
|
+//
|
|
|
+// public String upload(File file) {
|
|
|
+// Check.isEmpty(file, "上传的文件不能为空!");
|
|
|
+// if (!file.exists() || !file.isFile()) {
|
|
|
+// throw new StatusException(SYS_CODE_400, "上传的文件不存在!");
|
|
|
+// }
|
|
|
+//
|
|
|
+// try {
|
|
|
+// final String newFileName = FileUtils.newFileName(file.getName());
|
|
|
+// final String newFilePath = SysProperty.UPLOAD_DIR + FileUtils.dateDir() + newFileName;
|
|
|
+// this.getInstance().writeFile(newFilePath, file, true);
|
|
|
+//
|
|
|
+// //成功后,返回文件访问地址
|
|
|
+// return this.getUrlPrefix() + newFilePath;
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error(e.getMessage(), e);
|
|
|
+// throw new StatusException(SYS_CODE_500, "上传文件异常!");
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public String upload(byte[] bytes, String originalFilename) {
|
|
|
+// Check.isEmpty(bytes, "上传的文件不能为空!");
|
|
|
+// Check.isBlank(originalFilename, "上传的文件名不正确!");
|
|
|
+//
|
|
|
+// try {
|
|
|
+// final String newFileName = FileUtils.newFileName(originalFilename);
|
|
|
+// final String newFilePath = SysProperty.UPLOAD_DIR + FileUtils.dateDir() + newFileName;
|
|
|
+// this.getInstance().writeFile(newFilePath, bytes, true);
|
|
|
+//
|
|
|
+// //成功后,返回文件访问地址
|
|
|
+// return this.getUrlPrefix() + newFilePath;
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error(e.getMessage(), e);
|
|
|
+// throw new StatusException(SYS_CODE_500, "上传文件异常!");
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public File download(String filePath) {
|
|
|
+// try {
|
|
|
+// //final String rootDir = Constants.rootFileDir();
|
|
|
+// final String rootDir = systemProperties.getTempDataDir();
|
|
|
+// FileUtils.makeDirs(rootDir);
|
|
|
+// final String newFilePath = rootDir + "/" + FileUtils.newFileName(filePath);
|
|
|
+// File file = new File(newFilePath);
|
|
|
+// this.getInstance().readFile(filePath, file);
|
|
|
+// return file;
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error(e.getMessage(), e);
|
|
|
+// }
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public UpYun getInstance() {
|
|
|
+// UpYun upyun = new UpYun(sysProperty.getBucketName(), sysProperty.getUserName(), sysProperty.getPassword());
|
|
|
+// upyun.setDebug(true);
|
|
|
+// upyun.setApiDomain(UpYun.ED_AUTO);
|
|
|
+// return upyun;
|
|
|
+// }
|
|
|
+//
|
|
|
+// public String getUrlPrefix() {
|
|
|
+// return sysProperty.getDomain();
|
|
|
+// }
|
|
|
+//
|
|
|
+// public SysProperty getProperty() {
|
|
|
+// return sysProperty;
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|