diff --git a/xymanager_admin/src/main/resources/application-test.yml b/xymanager_admin/src/main/resources/application-test.yml index 2164dab..5911b72 100644 --- a/xymanager_admin/src/main/resources/application-test.yml +++ b/xymanager_admin/src/main/resources/application-test.yml @@ -91,7 +91,8 @@ swagger2: # urlPatterns: /system/*,/monitor/*,/tool/* #运维配置 maintain: - apk: /home/xymp/apk + apk: /home/xymp/apk/ + uri: http://61.169.135.146:40085/apk/ cma: server: 127.0.0.1 diff --git a/xymanager_common/src/main/java/com/shxy/xymanager_common/util/DigestUtils.java b/xymanager_common/src/main/java/com/shxy/xymanager_common/util/DigestUtils.java new file mode 100644 index 0000000..e83f7f0 --- /dev/null +++ b/xymanager_common/src/main/java/com/shxy/xymanager_common/util/DigestUtils.java @@ -0,0 +1,40 @@ +package com.shxy.xymanager_common.util; + +import java.security.MessageDigest; +import java.util.Base64; + +public class DigestUtils { + + public static byte[] hash(byte[] message, String alg) throws Exception { + MessageDigest md = MessageDigest.getInstance(alg); + md.update(message); + return md.digest(); + } + + public static String md5(byte[] message, boolean b64Encoded) throws Exception { + byte[] data = hash(message, "MD5"); + return b64Encoded ? new String(Base64.getEncoder().encode(data)) : HexUtils.byte2Hex(data); + } + + public static String sha1(byte[] message, boolean b64Encoded) throws Exception { + byte[] data = hash(message, "SHA1"); + return b64Encoded ? new String(Base64.getEncoder().encode(data)) : HexUtils.byte2Hex(data); + } + + public static String sha256(byte[] message, boolean b64Encoded) throws Exception { + byte[] data = hash(message, "SHA256"); + return b64Encoded ? new String(Base64.getEncoder().encode(data)) : HexUtils.byte2Hex(data); + } + + public static String md5(byte[] message) throws Exception { + return md5(message, false); + } + + public static String md5(String message) throws Exception { + return md5(message.getBytes(), false); + } + + public static String sha1(byte[] message) throws Exception { + return sha1(message, false); + } +} diff --git a/xymanager_common/src/main/java/com/shxy/xymanager_common/util/HexUtils.java b/xymanager_common/src/main/java/com/shxy/xymanager_common/util/HexUtils.java new file mode 100644 index 0000000..53310aa --- /dev/null +++ b/xymanager_common/src/main/java/com/shxy/xymanager_common/util/HexUtils.java @@ -0,0 +1,35 @@ +package com.shxy.xymanager_common.util; + +public class HexUtils { + private static final String HEX = "0123456789ABCDEF"; + + public static String byte2Hex(byte[] bytes) { + String result = ""; + if (null != bytes) { + for (int i = 0; i < bytes.length; ++i) { + result = result + "0123456789ABCDEF".charAt(bytes[i] >> 4 & 15); + result = result + "0123456789ABCDEF".charAt(bytes[i] & 15); + } + } + + return result; + } + + public static byte[] hex2Byte(String text) throws Exception { + String hexText = text; + if (text.length() % 2 == 1) { + hexText = "0" + text; + } + + hexText = hexText.toLowerCase(); + int len = hexText.length() / 2; + byte[] result = new byte[len]; + + for (int i = 0; i < len; ++i) { + String s = hexText.substring(2 * i, 2 * i + 2); + result[i] = (byte) (Integer.parseInt(s, 16) & 255); + } + + return result; + } +} diff --git a/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradeServiceImpl.java b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradeServiceImpl.java index 43a99bf..d0813c6 100644 --- a/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradeServiceImpl.java +++ b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/UpgradeServiceImpl.java @@ -5,9 +5,11 @@ import com.shxy.xymanager_common.entity.MntnUpgrades; import com.shxy.xymanager_common.entity.MntnUpgradesExample; import com.shxy.xymanager_common.exception.ApiException; import com.shxy.xymanager_common.util.DateUtil; +import com.shxy.xymanager_common.util.DigestUtils; import com.shxy.xymanager_dao.dao.MntnUpgradesMapper; import com.shxy.xymanager_service.service.UpgradeService; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -16,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; +import java.nio.file.Files; import java.util.Date; import java.util.List; import java.util.UUID; @@ -28,6 +31,8 @@ public class UpgradeServiceImpl implements UpgradeService { @Value("${maintain.apk}") private String apkpath; + @Value("${maintain.uri}") + private String uri; @Resource MntnUpgradesMapper mapper; @@ -42,7 +47,7 @@ public class UpgradeServiceImpl implements UpgradeService { example.setOrderByClause("id desc"); List list = mapper.selectByExample(example); for (MntnUpgrades item : list) { - + item.setPath(uri + item.getPath()); } return list; } @@ -62,8 +67,10 @@ public class UpgradeServiceImpl implements UpgradeService { item.setCreateTime(new Date()); item.setUpdateTime(new Date()); - File dest = new File(apkpath + "/" + path); + File dest = new File(apkpath + path); file.transferTo(dest); + String md5 = DigestUtils.md5(file.getBytes()); + item.setMd5(md5); mapper.insert(item); return true; } @@ -81,6 +88,8 @@ public class UpgradeServiceImpl implements UpgradeService { @Override public void delete(Integer id) throws Exception { + MntnUpgrades record = mapper.selectByPrimaryKey(id); mapper.deleteByPrimaryKey(id); + new File(apkpath + record.getPath()).delete(); } }