Commit 2bc14fe1 authored by ZWT's avatar ZWT

feat(吉林演示): 松原

1.天气数据接收定时任务,解决代码扫描问题,修改文件读取相关代码,解决资源未关流问题;

BREAKING CHANGE: 无

Closes 无

[skip ci]
parent 710821ad
...@@ -6,7 +6,6 @@ import cn.hutool.core.date.DateUtil; ...@@ -6,7 +6,6 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.NioUtil; import cn.hutool.core.io.NioUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
...@@ -59,6 +58,7 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService { ...@@ -59,6 +58,7 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService {
private static final String username = XStartup.getCurrent().getProperty("weather.mail.username"); private static final String username = XStartup.getCurrent().getProperty("weather.mail.username");
private static final String password = XStartup.getCurrent().getProperty("weather.mail.password"); private static final String password = XStartup.getCurrent().getProperty("weather.mail.password");
private static final String protocol = XStartup.getCurrent().getProperty("weather.mail.protocol"); private static final String protocol = XStartup.getCurrent().getProperty("weather.mail.protocol");
private static final String storeDir = XStartup.getCurrent().getProperty("weather.file.temp.path");
/** /**
* 天气数据接收Cloud模块--天气数据处理 * 天气数据接收Cloud模块--天气数据处理
...@@ -95,9 +95,7 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService { ...@@ -95,9 +95,7 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService {
re.getMailContent(message); re.getMailContent(message);
String tempFilePath = this.saveAttachment(message); String tempFilePath = this.saveAttachment(message);
logger.info("------ weatherDataProcess load file: {}", tempFilePath); logger.info("------ weatherDataProcess load file: {}", tempFilePath);
FileReader fileReader = new FileReader(tempFilePath); JSONObject jsonObject = JSON.parseObject(FileUtil.readUtf8String(tempFilePath));
String jsonString = fileReader.readString();
JSONObject jsonObject = JSON.parseObject(jsonString);
jsonObjectList.add(jsonObject); jsonObjectList.add(jsonObject);
logger.info("------ weatherDataProcess del temp file path: {}", tempFilePath); logger.info("------ weatherDataProcess del temp file path: {}", tempFilePath);
FileUtil.del(tempFilePath); FileUtil.del(tempFilePath);
...@@ -180,9 +178,8 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService { ...@@ -180,9 +178,8 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService {
re.getMailContent(message); re.getMailContent(message);
String tempFilePath = this.saveAttachment(message); String tempFilePath = this.saveAttachment(message);
logger.info("------ weatherApiDataProcess load file: {}", tempFilePath); logger.info("------ weatherApiDataProcess load file: {}", tempFilePath);
FileReader fileReader = new FileReader(tempFilePath);
String jsonString = fileReader.readString();
//处理读取流部分数据丢失精度问题 //处理读取流部分数据丢失精度问题
String jsonString = FileUtil.readUtf8String(tempFilePath);
jsonString = CharSequenceUtil.replace(jsonString, "%", "."); jsonString = CharSequenceUtil.replace(jsonString, "%", ".");
List<Weather15mData> weather15mDataList = JSON.parseArray(jsonString, Weather15mData.class); List<Weather15mData> weather15mDataList = JSON.parseArray(jsonString, Weather15mData.class);
if (CollUtil.isEmpty(weather15mDataList)) { if (CollUtil.isEmpty(weather15mDataList)) {
...@@ -370,15 +367,13 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService { ...@@ -370,15 +367,13 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService {
if (CharSequenceUtil.isNotEmpty(fileName) && StringUtils.lowerCase(fileName).contains("GB2312")) { if (CharSequenceUtil.isNotEmpty(fileName) && StringUtils.lowerCase(fileName).contains("GB2312")) {
fileName = MimeUtility.decodeText(fileName); fileName = MimeUtility.decodeText(fileName);
} }
fileName = CharSequenceUtil.replace(fileName, "\\?|=", ""); filePath = this.saveTempFile(mPart, CharSequenceUtil.replace(fileName, "\\?|=", ""));
filePath = this.saveTempFile(fileName, mPart.getInputStream());
} else if (mPart.isMimeType("multipart/*")) { } else if (mPart.isMimeType("multipart/*")) {
this.saveAttachment(mPart); this.saveAttachment(mPart);
} else { } else {
fileName = mPart.getFileName(); fileName = mPart.getFileName();
if (CharSequenceUtil.isNotEmpty(fileName) && StringUtils.lowerCase(fileName).contains("GB2312")) { if (CharSequenceUtil.isNotEmpty(fileName) && StringUtils.lowerCase(fileName).contains("GB2312")) {
fileName = MimeUtility.decodeText(fileName); filePath = this.saveTempFile(mPart, MimeUtility.decodeText(fileName));
filePath = this.saveTempFile(fileName, mPart.getInputStream());
} }
} }
} }
...@@ -396,7 +391,6 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService { ...@@ -396,7 +391,6 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService {
* @return {@link String} * @return {@link String}
*/ */
private String saveTempFile(String fileName, InputStream in) { private String saveTempFile(String fileName, InputStream in) {
String storeDir = XStartup.getCurrent().getProperty("weather.file.temp.path");
StringBuilder stringBuilder = new StringBuilder(storeDir) StringBuilder stringBuilder = new StringBuilder(storeDir)
.append(File.separator) .append(File.separator)
.append(DateTime.now().toString("yyyyMMdd")); .append(DateTime.now().toString("yyyyMMdd"));
...@@ -407,6 +401,28 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService { ...@@ -407,6 +401,28 @@ public class BaseWeatherCloudServiceImpl implements IBaseWeatherCloudService {
return filePath; return filePath;
} }
/**
* 保存临时文件
*
* @param mPart m部分
* @param fileName 文件名
* @return {@link String }
*/
private String saveTempFile(BodyPart mPart, String fileName) {
String filePath;
try (InputStream inputStream = mPart.getInputStream()) {
StringBuilder stringBuilder = new StringBuilder(storeDir)
.append(File.separator)
.append(DateTime.now().toString("yyyyMMdd"));
filePath = stringBuilder.append(File.separator).append(fileName).toString();
FileUtil.touch(filePath);
FileUtil.writeFromStream(inputStream, filePath);
} catch (IOException | MessagingException e) {
throw new RuntimeException(e);
}
return filePath;
}
/** /**
* 零填充 * 零填充
* *
......
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