|
@ -1,340 +0,0 @@
|
|
|
package com.yihu.jw.util.common;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.yihu.fastdfs.FastDFSUtil;
|
|
|
import com.yihu.jw.entity.util.SystemConfEntity;
|
|
|
import com.yihu.jw.util.http.HttpClientUtil;
|
|
|
import org.apache.commons.lang3.StringEscapeUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.NameValuePair;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.net.URLDecoder;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* Created by yeshijie on 2023/2/13.
|
|
|
*/
|
|
|
@Component
|
|
|
public class CommonUtil {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(CommonUtil.class);
|
|
|
|
|
|
// @Value("${neiwang.enable}")
|
|
|
private Boolean isneiwang = false; //如果不是内网项目要转到到内网wlyy在上传
|
|
|
// @Value("${fastDFS.fastdfs_file_url}")
|
|
|
private String fastdfs_file_url;
|
|
|
|
|
|
// @Value("${neiwang.wlyy}")
|
|
|
private String neiwangWlyy; //内网的项目地址
|
|
|
|
|
|
@Autowired
|
|
|
private HttpClientUtil httpClientUtil;
|
|
|
|
|
|
public static String getCode() {
|
|
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 传入身高体重,计算BMI值
|
|
|
*
|
|
|
* @param weightStr 体重
|
|
|
* @param heightStr 身高
|
|
|
* @return
|
|
|
*/
|
|
|
public static double getBMIByWeightAndHeight(String weightStr, String heightStr) {
|
|
|
|
|
|
DecimalFormat df2 = new DecimalFormat("###.00");
|
|
|
|
|
|
double weight = Double.parseDouble(weightStr);
|
|
|
Integer heightCM = Integer.parseInt(heightStr);
|
|
|
heightStr = df2.format(heightCM / 100d);
|
|
|
|
|
|
double height = Double.parseDouble(heightStr);
|
|
|
double bmi = weight / (height * height);
|
|
|
|
|
|
return bmi;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除文件夹
|
|
|
* @param folder
|
|
|
*/
|
|
|
public static void deleteFolder(File folder){
|
|
|
File[] files = folder.listFiles();
|
|
|
if (files != null){
|
|
|
for(File f: files){
|
|
|
if (f.isDirectory()){
|
|
|
deleteFolder(f);
|
|
|
}else {
|
|
|
f.delete();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
folder.delete();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* double转字符串,在转int
|
|
|
* double*100转int 有bug 34.3会会变成3429
|
|
|
* @param d
|
|
|
* @return
|
|
|
*/
|
|
|
public static Integer doubleToInt(Double d){
|
|
|
if(d==null){
|
|
|
return 0;
|
|
|
}
|
|
|
String currency = String.valueOf(d);
|
|
|
int index = currency.indexOf(".");
|
|
|
int length = currency.length();
|
|
|
Integer amLong = 0;
|
|
|
if(index == -1){
|
|
|
amLong = Integer.valueOf(currency+"00");
|
|
|
}else if(length - index >= 3){
|
|
|
amLong = Integer.valueOf((currency.substring(0, index+3)).replace(".", ""));
|
|
|
if(length-index>3){
|
|
|
if(Integer.valueOf(currency.substring(index+3,index+4))>=5){
|
|
|
amLong++;
|
|
|
}
|
|
|
}
|
|
|
}else if(length - index == 2){
|
|
|
amLong = Integer.valueOf((currency.substring(0, index+2)).replace(".", "")+0);
|
|
|
}else{
|
|
|
amLong = Integer.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");
|
|
|
}
|
|
|
return amLong;
|
|
|
}
|
|
|
|
|
|
public static List<String> getTagContent(String source, String regString) {
|
|
|
List<String> result = new ArrayList<String>();
|
|
|
Matcher m = Pattern.compile(regString).matcher(source);
|
|
|
while (m.find()) {
|
|
|
try {
|
|
|
String r = StringEscapeUtils.unescapeHtml3(URLDecoder.decode(m.group(1),"utf-8"));
|
|
|
result.add(r);
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public String copyTempVoice(String voices) throws Exception {
|
|
|
if (isneiwang) {
|
|
|
// 文件保存的临时路径
|
|
|
String serverUrl = fastdfs_file_url;
|
|
|
FastDFSUtil fastDFSUtil = new FastDFSUtil();
|
|
|
String fileUrls = "";
|
|
|
File f = new File(voices);
|
|
|
if (f.exists()) {
|
|
|
String fileName = f.getName();
|
|
|
InputStream in = new FileInputStream(f);
|
|
|
ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
|
|
|
in.close();
|
|
|
if (result != null) {
|
|
|
fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",") + serverUrl
|
|
|
+ result.get("groupName").toString().replaceAll("\"", "") + "/"
|
|
|
+ result.get("remoteFileName").toString().replaceAll("\"", "");
|
|
|
f.delete();
|
|
|
}
|
|
|
}
|
|
|
return fileUrls;
|
|
|
} else {
|
|
|
String fileUrls = toNeiWang(voices, "");
|
|
|
return fileUrls;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 如果是外网那是发送到内网的服务器
|
|
|
*
|
|
|
* @param files
|
|
|
* @return
|
|
|
*/
|
|
|
public String toNeiWang(String files, String tempPath) throws FileNotFoundException {
|
|
|
logger.info(" toNeiWang start files :" + tempPath + files);
|
|
|
//转发到内网服务器
|
|
|
String[] fileArray = files.split(",");
|
|
|
String fileUrls = "";
|
|
|
for (String file : fileArray) {
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
|
|
|
File f = new File(tempPath + file);
|
|
|
logger.info(" toNeiWang File exists :" + f.exists());
|
|
|
if (f.exists()) {
|
|
|
String fileName = f.getName();
|
|
|
InputStream in = new FileInputStream(f);
|
|
|
String returnStr = request(request, in, fileName);
|
|
|
logger.info("returnStr :" + returnStr);
|
|
|
logger.info("fileName :" + fileName);
|
|
|
logger.info("result :" + returnStr.toString());
|
|
|
if (returnStr != null) {
|
|
|
//1.3.7去掉前缀
|
|
|
fileUrls += returnStr+",";
|
|
|
f.delete();
|
|
|
}
|
|
|
} else {
|
|
|
String path = tempPath + file;
|
|
|
String returnStr = request(request, path);
|
|
|
logger.info("result :" + returnStr.toString());
|
|
|
if (returnStr != null) {
|
|
|
//1.3.7去掉前缀
|
|
|
fileUrls += returnStr+",";
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
logger.info(" toNeiWang end files :" + fileUrls);
|
|
|
|
|
|
return fileUrls.substring(0,fileUrls.length()-1);
|
|
|
}
|
|
|
|
|
|
private String request(HttpServletRequest request, String path) {
|
|
|
String url = neiwangWlyy + "/wlyy/upload/commonUpload";//uri请求路径
|
|
|
String result = "";
|
|
|
try {
|
|
|
List<NameValuePair> params = new ArrayList<>();
|
|
|
|
|
|
params.add(new BasicNameValuePair("filePaths", path));
|
|
|
// 将响应内容转换为字符串
|
|
|
result = httpClientUtil.post(url, params, "UTF-8");
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public String request(String remote_url, MultipartFile file, String type) {
|
|
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
logger.info(file.getOriginalFilename());
|
|
|
String result = "";
|
|
|
try {
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
HttpPost httpPost = new HttpPost(remote_url);
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
|
|
|
builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
|
|
|
if (!org.springframework.util.StringUtils.isEmpty(type)) {
|
|
|
builder.addTextBody("type", type); //发送类型
|
|
|
}
|
|
|
HttpEntity entity = builder.build();
|
|
|
httpPost.setEntity(entity);
|
|
|
HttpResponse response = httpClient.execute(httpPost);// 执行提交
|
|
|
HttpEntity responseEntity = response.getEntity();
|
|
|
if (responseEntity != null) {
|
|
|
// 将响应内容转换为字符串
|
|
|
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
httpClient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public String request(HttpServletRequest request, InputStream in, String fileName) {
|
|
|
String url = neiwangWlyy + "/wlyy/upload/commonUpload";//uri请求路径
|
|
|
|
|
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
String result = "";
|
|
|
try {
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
builder.addBinaryBody("file", in, ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
|
|
|
builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
|
|
|
if (!org.springframework.util.StringUtils.isEmpty(request.getParameter("type")) ||
|
|
|
!org.springframework.util.StringUtils.isEmpty(request.getAttribute("type"))) {
|
|
|
builder.addTextBody("type", null != request.getParameter("type") ? request.getParameter("type") : String.valueOf(request.getAttribute("type"))); //发送类型
|
|
|
}
|
|
|
HttpEntity entity = builder.build();
|
|
|
httpPost.setEntity(entity);
|
|
|
HttpResponse response = httpClient.execute(httpPost);// 执行提交
|
|
|
HttpEntity responseEntity = response.getEntity();
|
|
|
if (responseEntity != null) {
|
|
|
// 将响应内容转换为字符串
|
|
|
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
httpClient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
try {
|
|
|
in.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public String copyTempImage(String files) throws Exception {
|
|
|
String tempPath = SystemConfEntity.getInstance().getTempPath() + File.separator;
|
|
|
if (isneiwang) {
|
|
|
// 文件保存的临时路径
|
|
|
String[] fileArray = files.split(",");
|
|
|
FastDFSUtil fastDFSUtil = new FastDFSUtil();
|
|
|
String fileUrls = "";
|
|
|
for (String file : fileArray) {
|
|
|
File f = new File(tempPath + file);
|
|
|
File fs = new File(tempPath + file + "_small");
|
|
|
if (f.exists()) {
|
|
|
String fileName = f.getName();
|
|
|
InputStream in = new FileInputStream(f);
|
|
|
ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
|
|
|
in.close();
|
|
|
if (result != null) {
|
|
|
//1.3.7去掉前缀
|
|
|
fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",")
|
|
|
+ result.get("groupName").toString().replaceAll("\"", "") + "/"
|
|
|
+ result.get("remoteFileName").toString().replaceAll("\"", "");
|
|
|
f.delete();
|
|
|
if (fs.exists()) {
|
|
|
fs.delete();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return fileUrls;
|
|
|
} else {
|
|
|
String fileUrls = toNeiWang(files, tempPath);
|
|
|
return fileUrls;
|
|
|
}
|
|
|
}
|
|
|
}
|