|
@@ -16,64 +16,77 @@ import java.security.NoSuchAlgorithmException;
|
|
|
* Created by yuanpan on 2017/5/11.
|
|
|
*/
|
|
|
@Service
|
|
|
-public class UpyunServiceImpl implements UpyunService{
|
|
|
+public class UpyunServiceImpl implements UpyunService {
|
|
|
|
|
|
- private UpYun upyun;
|
|
|
+ private UpYun upyun;
|
|
|
|
|
|
- @Value("${app.upyun.path}")
|
|
|
- private String path;
|
|
|
+ @Value("${app.upyun.path}")
|
|
|
+ private String path;
|
|
|
|
|
|
- @Value("${app.upyun.bucket}")
|
|
|
- private String bucket;
|
|
|
+ @Value("${app.upyun.bucket}")
|
|
|
+ private String bucket;
|
|
|
|
|
|
- @Value("${app.upyun.operator}")
|
|
|
- private String operator;
|
|
|
+ @Value("${app.upyun.operator}")
|
|
|
+ private String operator;
|
|
|
|
|
|
- @Value("${app.upyun.password}")
|
|
|
- private String password;
|
|
|
+ @Value("${app.upyun.password}")
|
|
|
+ private String password;
|
|
|
|
|
|
- public String getDownloadUrl(String identityNumber, String fileName, String fileSuffix) {
|
|
|
-// http://exam-cloud-test.b0.upaiyun.com/student_base_photo/420678442922121/11b1841125259151f0347415d1740595.jpg
|
|
|
+ private UpYun getUpyun() {
|
|
|
+ if (upyun == null) {
|
|
|
+ upyun = new UpYun(bucket, operator, password);
|
|
|
+ }
|
|
|
+ return upyun;
|
|
|
+ }
|
|
|
|
|
|
- if (fileSuffix.startsWith(".")) {
|
|
|
- return "http://" + bucket + ".b0.upaiyun.com/student_base_photo/" + identityNumber + "/" + fileName + "" + fileSuffix;
|
|
|
- } else {
|
|
|
- return "http://" + bucket + ".b0.upaiyun.com/student_base_photo/" + identityNumber + "/" + fileName + "." + fileSuffix;
|
|
|
- }
|
|
|
+ public String getDownloadUrl(String identityNumber, String fileName, String fileSuffix) {
|
|
|
+ // http://exam-cloud-test.b0.upaiyun.com/student_base_photo/420678442922121/11b1841125259151f0347415d1740595.jpg
|
|
|
|
|
|
- }
|
|
|
+ if (fileSuffix.startsWith(".")) {
|
|
|
+ return "http://" + bucket + ".b0.upaiyun.com/student_base_photo/" + identityNumber + "/"
|
|
|
+ + fileName + "" + fileSuffix;
|
|
|
+ } else {
|
|
|
+ return "http://" + bucket + ".b0.upaiyun.com/student_base_photo/" + identityNumber + "/"
|
|
|
+ + fileName + "." + fileSuffix;
|
|
|
+ }
|
|
|
|
|
|
- public String getMd5(byte[] bytes) throws NoSuchAlgorithmException {
|
|
|
- //用来计算md5摘要的
|
|
|
- char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
|
|
+ }
|
|
|
|
|
|
- MessageDigest md5 = MessageDigest.getInstance("MD5");
|
|
|
- md5.update(bytes);
|
|
|
+ public String getMd5(byte[] bytes) throws NoSuchAlgorithmException {
|
|
|
+ // 用来计算md5摘要的
|
|
|
+ char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
|
|
|
+ 'c', 'd', 'e', 'f'};
|
|
|
|
|
|
- byte[] md5DigestBytes = md5.digest();
|
|
|
- int dlen = md5DigestBytes.length;
|
|
|
+ MessageDigest md5 = MessageDigest.getInstance("MD5");
|
|
|
+ md5.update(bytes);
|
|
|
|
|
|
- char[] md5val = new char[dlen * 2];
|
|
|
- int k = 0;
|
|
|
- for (byte encoded : md5DigestBytes) {
|
|
|
- md5val[k++] = hexDigits[encoded >> 4 & 15];
|
|
|
- md5val[k++] = hexDigits[encoded & 5];
|
|
|
- }
|
|
|
+ byte[] md5DigestBytes = md5.digest();
|
|
|
+ int dlen = md5DigestBytes.length;
|
|
|
|
|
|
- return new String(md5val);
|
|
|
- }
|
|
|
+ char[] md5val = new char[dlen * 2];
|
|
|
+ int k = 0;
|
|
|
+ for (byte encoded : md5DigestBytes) {
|
|
|
+ md5val[k++] = hexDigits[encoded >> 4 & 15];
|
|
|
+ md5val[k++] = hexDigits[encoded & 5];
|
|
|
+ }
|
|
|
|
|
|
- public boolean saveStudentPhoto(String identityNumber, byte[] bytes, String fileName, String fileSuffix) throws IOException {
|
|
|
+ return new String(md5val);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean saveStudentPhoto(String identityNumber, byte[] bytes, String fileName,
|
|
|
+ String fileSuffix) throws IOException {
|
|
|
|
|
|
- if (fileSuffix.startsWith(".")) {
|
|
|
- return getUpyun().writeFile(path + "/" + identityNumber + "/" + fileName + fileSuffix, bytes, true);
|
|
|
- } else {
|
|
|
- return getUpyun().writeFile(path + "/" + identityNumber + "/" + fileName + "." + fileSuffix, bytes, true);
|
|
|
- }
|
|
|
+ if (fileSuffix.startsWith(".")) {
|
|
|
+ return getUpyun().writeFile(path + "/" + identityNumber + "/" + fileName + fileSuffix,
|
|
|
+ bytes, true);
|
|
|
+ } else {
|
|
|
+ return getUpyun().writeFile(
|
|
|
+ path + "/" + identityNumber + "/" + fileName + "." + fileSuffix, bytes, true);
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public boolean saveStudentPhoto(Long studentId, Long rootOrgId, String fileSuffix, File file) {
|
|
|
String filePath = null;
|
|
|
if (fileSuffix.startsWith(".")) {
|
|
@@ -83,15 +96,24 @@ public class UpyunServiceImpl implements UpyunService{
|
|
|
}
|
|
|
try {
|
|
|
return getUpyun().writeFile(filePath, file, true);
|
|
|
- } catch (IOException e) {
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("EX-100001", "调用又拍云API写文件失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean saveStudentPhoto(Long studentId, Long rootOrgId, String fileSuffix,
|
|
|
+ byte[] bytes) {
|
|
|
+ String filePath = null;
|
|
|
+ if (fileSuffix.startsWith(".")) {
|
|
|
+ filePath = path + "/" + rootOrgId + "/" + studentId + fileSuffix;
|
|
|
+ } else {
|
|
|
+ filePath = path + "/" + rootOrgId + "/" + studentId + "." + fileSuffix;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return getUpyun().writeFile(filePath, bytes, true);
|
|
|
+ } catch (Exception e) {
|
|
|
throw new StatusException("EX-100001", "调用又拍云API写文件失败");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private UpYun getUpyun() {
|
|
|
- if (upyun == null) {
|
|
|
- upyun = new UpYun(bucket, operator, password);
|
|
|
- }
|
|
|
- return upyun;
|
|
|
- }
|
|
|
}
|