Commit 0ed72236 authored by 李德才's avatar 李德才

查找并写入CPU唯一值

parent 4da62551
package com.archser.aserver.controller;
import com.archser.aserver.service.SnService;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
public class SnController extends Controller {
......@@ -11,5 +14,23 @@ public class SnController extends Controller {
SnService snService;
/**
* 获取序列号
*/
@Clear
public void getPropertiesValue() {
String cupCode = getPara("cupCode");
try {
String snCode = snService.getPropertiesValue(cupCode);
if (StrKit.isBlank(snCode)) {
snService.setProperty(cupCode, "");
}
renderJson(Ret.ok("snCode", snCode));
} catch (Exception e) {
e.printStackTrace();
renderJson(Ret.fail());
}
}
}
package com.archser.aserver.service;
import com.archser.aserver.util.PropertyUtil;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import java.io.File;
/**
* 读写 SN配置文件 20201201 lidecai
*/
public class SnService {
private static String FILE_PATH;
static {
FILE_PATH = PathKit.getWebRootPath() + File.separator + "SN.properties";
}
/**
* 从 Properties 文件中获取指定对应的值
*
* @param cupCode
* @return
*/
public String getPropertiesValue(String cupCode) {
return PropKit.use(new File(FILE_PATH)).get(cupCode);
}
/**
* 向 Properties 写入
*
* @param cupCode
* @param snCode
*/
public void setProperty(String cupCode, String snCode) {
PropertyUtil.setProperty(cupCode, snCode, FILE_PATH);
}
}
package com.archser.aserver.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Properties;
/**
* 20201201 lidecai 读写 Property 文件工具类
*/
public class PropertyUtil {
/**
* 读取配置文件中的值
*
* @param key
* @return
*/
public static String getProperty(String key, String filePath) {
String value = "";
Properties props = new Properties();
InputStream input = null;
try {
input = new FileInputStream(filePath);
props.load(input);
value = props.getProperty(key);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
Objects.requireNonNull(input).close();
} catch (IOException e) {
e.printStackTrace();
}
}
return value;
}
/**
* 向配置文件中写值
* @param key
* @param value
* @param filePath
*/
public static void setProperty(String key, String value, String filePath) {
Properties props = new Properties();
OutputStream out = null;
InputStream input = null;
try {
input = new FileInputStream(filePath);
props.load(input);
props.setProperty(key, value);
out = new FileOutputStream(filePath);
props.store(out, null);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
Objects.requireNonNull(input).close();
} catch (IOException e) {
e.printStackTrace();
}
try {
Objects.requireNonNull(out).close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
#Tue Dec 01 16:02:59 CST 2020
AAAAAAAAAAAAAAA=cdcdcdcdcdc
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment