|
@ -1,40 +1,69 @@
|
|
package com.yihu.wlyy.util;
|
|
package com.yihu.wlyy.util;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
|
|
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
|
|
import it.sauronsoftware.jave.*;
|
|
import it.sauronsoftware.jave.*;
|
|
|
|
import net.sf.json.JSONObject;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
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.util.EntityUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
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 org.springframework.web.multipart.MultipartRequest;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
import java.nio.charset.Charset;
|
|
import java.text.DecimalFormat;
|
|
import java.text.DecimalFormat;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Component
|
|
@Component
|
|
public class CommonUtil {
|
|
public class CommonUtil {
|
|
|
|
private Logger logger = LoggerFactory.getLogger(CommonUtil.class);
|
|
@Value("${fastDFS.fastdfs_file_url}")
|
|
@Value("${fastDFS.fastdfs_file_url}")
|
|
private String fastdfs_file_url;
|
|
private String fastdfs_file_url;
|
|
@Value("${server.server_url}")
|
|
@Value("${server.server_url}")
|
|
private String server_url;
|
|
private String server_url;
|
|
|
|
@Value("${neiwang.wlyy}")
|
|
|
|
private String neiwangWlyy; //内网的项目地址
|
|
|
|
@Value("${neiwang.enable}")
|
|
|
|
private Boolean isneiwang = false; //如果不是内网项目要转到到内网wlyy在上传
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取图片全路径
|
|
* 获取图片全路径
|
|
*
|
|
*
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public String getPhoneUrl(String url) {
|
|
|
|
|
|
public String getPhoneUrl(String url) {
|
|
if (StringUtils.isEmpty(url)) {
|
|
if (StringUtils.isEmpty(url)) {
|
|
return "";
|
|
return "";
|
|
} else {
|
|
} else {
|
|
if (url.indexOf("http") > -1) {
|
|
if (url.indexOf("http") > -1) {
|
|
return url;
|
|
return url;
|
|
} else {
|
|
} else {
|
|
return server_url+ url;
|
|
|
|
|
|
return server_url + url;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getIdcardEncode(String idcard) {
|
|
|
|
|
|
public String getIdcardEncode(String idcard) {
|
|
if (idcard != null) {
|
|
if (idcard != null) {
|
|
if (idcard.length() == 18) {
|
|
if (idcard.length() == 18) {
|
|
return idcard.substring(0, 9) + "*******" + idcard.substring(16, 18);
|
|
return idcard.substring(0, 9) + "*******" + idcard.substring(16, 18);
|
|
@ -51,7 +80,7 @@ public class CommonUtil {
|
|
* @param obj
|
|
* @param obj
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public byte[] toByteArray(Object obj) {
|
|
|
|
|
|
public byte[] toByteArray(Object obj) {
|
|
byte[] bytes = null;
|
|
byte[] bytes = null;
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
try {
|
|
try {
|
|
@ -73,7 +102,7 @@ public class CommonUtil {
|
|
* @param bytes
|
|
* @param bytes
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public Object toObject(byte[] bytes) {
|
|
|
|
|
|
public Object toObject(byte[] bytes) {
|
|
Object obj = null;
|
|
Object obj = null;
|
|
try {
|
|
try {
|
|
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
|
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
|
@ -99,54 +128,60 @@ public class CommonUtil {
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
|
|
public String copyTempVoice(String voices) throws Exception {
|
|
|
|
// 文件保存的临时路径
|
|
|
|
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();
|
|
|
|
|
|
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;
|
|
}
|
|
}
|
|
return fileUrls;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* double转字符串,在转int
|
|
* double转字符串,在转int
|
|
* double*100转int 有bug 34.3会会变成3429
|
|
* double*100转int 有bug 34.3会会变成3429
|
|
|
|
*
|
|
* @param d
|
|
* @param d
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static Integer doubleToInt(Double d){
|
|
|
|
if(d==null){
|
|
|
|
|
|
public static Integer doubleToInt(Double d) {
|
|
|
|
if (d == null) {
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
String currency = String.valueOf(d);
|
|
String currency = String.valueOf(d);
|
|
int index = currency.indexOf(".");
|
|
int index = currency.indexOf(".");
|
|
int length = currency.length();
|
|
int length = currency.length();
|
|
Integer amLong = 0;
|
|
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){
|
|
|
|
|
|
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++;
|
|
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");
|
|
|
|
|
|
} 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;
|
|
return amLong;
|
|
}
|
|
}
|
|
@ -159,7 +194,7 @@ public class CommonUtil {
|
|
* @param standardMin 最小标准值
|
|
* @param standardMin 最小标准值
|
|
* @return 0正常,1高,-1低
|
|
* @return 0正常,1高,-1低
|
|
*/
|
|
*/
|
|
public double checkHealthIndex(double curValue, double standardMax, double standardMin) {
|
|
|
|
|
|
public double checkHealthIndex(double curValue, double standardMax, double standardMin) {
|
|
if (curValue <= 0) {
|
|
if (curValue <= 0) {
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
@ -181,42 +216,44 @@ public class CommonUtil {
|
|
* @return
|
|
* @return
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
public String copyTempImage(String files) throws Exception {
|
|
|
|
// 文件保存的临时路径
|
|
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
|
|
String serverUrl = fastdfs_file_url;
|
|
|
|
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) {
|
|
|
|
// fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",") + serverUrl
|
|
|
|
// + result.get("groupName").toString().replaceAll("\"", "") + "/"
|
|
|
|
// + result.get("remoteFileName").toString().replaceAll("\"", "");
|
|
|
|
//1.3.7去掉前缀
|
|
|
|
fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",")
|
|
|
|
+ result.get("groupName").toString().replaceAll("\"", "") + "/"
|
|
|
|
+ result.get("remoteFileName").toString().replaceAll("\"", "");
|
|
|
|
f.delete();
|
|
|
|
if (fs.exists()) {
|
|
|
|
fs.delete();
|
|
|
|
|
|
public String copyTempImage(String files) throws Exception {
|
|
|
|
if (isneiwang) {
|
|
|
|
// 文件保存的临时路径
|
|
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
|
|
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;
|
|
|
|
|
|
return fileUrls;
|
|
|
|
} else {
|
|
|
|
String fileUrls = toNeiWang(files);
|
|
|
|
return fileUrls;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void changeToMp3(String sourcePath, String targetPath) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void changeToMp3(String sourcePath, String targetPath) {
|
|
File source = new File(sourcePath);
|
|
File source = new File(sourcePath);
|
|
File target = new File(targetPath);
|
|
File target = new File(targetPath);
|
|
AudioAttributes audio = new AudioAttributes();
|
|
AudioAttributes audio = new AudioAttributes();
|
|
@ -236,12 +273,12 @@ public class CommonUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public long getVideoTimeAndImg(String sourcePath,String targetPath) throws Exception{
|
|
|
|
|
|
public long getVideoTimeAndImg(String sourcePath, String targetPath) throws Exception {
|
|
File source = new File(sourcePath);
|
|
File source = new File(sourcePath);
|
|
Encoder encoder = new Encoder();
|
|
Encoder encoder = new Encoder();
|
|
MultimediaInfo m = encoder.getInfo(source);
|
|
MultimediaInfo m = encoder.getInfo(source);
|
|
long ms = m.getDuration();
|
|
long ms = m.getDuration();
|
|
if(ms<1000)return ms;
|
|
|
|
|
|
if (ms < 1000) return ms;
|
|
File target = new File(targetPath);//转图片
|
|
File target = new File(targetPath);//转图片
|
|
VideoAttributes video = new VideoAttributes();
|
|
VideoAttributes video = new VideoAttributes();
|
|
video.setCodec("png");//转图片
|
|
video.setCodec("png");//转图片
|
|
@ -256,22 +293,21 @@ public class CommonUtil {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void changeToMp4(String sourcePath, String targetPath) {
|
|
|
|
|
|
public void changeToMp4(String sourcePath, String targetPath) {
|
|
File source = new File(sourcePath);
|
|
File source = new File(sourcePath);
|
|
File target = new File(targetPath);
|
|
File target = new File(targetPath);
|
|
AudioAttributes audio = new AudioAttributes();
|
|
AudioAttributes audio = new AudioAttributes();
|
|
|
|
|
|
//audio.setCodec("aac");
|
|
//audio.setCodec("aac");
|
|
audio.setCodec("libvorbis");
|
|
audio.setCodec("libvorbis");
|
|
// audio.setBitRate(new Integer(64000));
|
|
|
|
|
|
// audio.setBitRate(new Integer(64000));
|
|
//audio.setChannels(new Integer(1));
|
|
//audio.setChannels(new Integer(1));
|
|
// audio.setSamplingRate(new Integer(22050));
|
|
|
|
|
|
// audio.setSamplingRate(new Integer(22050));
|
|
VideoAttributes video = new VideoAttributes();
|
|
VideoAttributes video = new VideoAttributes();
|
|
//video.setCodec("libxvid");// 转MP4
|
|
//video.setCodec("libxvid");// 转MP4
|
|
video.setCodec("libtheora");//
|
|
video.setCodec("libtheora");//
|
|
// video.setBitRate(new Integer(240000));// 180kb/s比特率
|
|
|
|
// video.setFrameRate(new Integer(28));// 1f/s帧频,1是目前测试比较清楚的,越大越模糊
|
|
|
|
|
|
// video.setBitRate(new Integer(240000));// 180kb/s比特率
|
|
|
|
// video.setFrameRate(new Integer(28));// 1f/s帧频,1是目前测试比较清楚的,越大越模糊
|
|
EncodingAttributes attrs = new EncodingAttributes();
|
|
EncodingAttributes attrs = new EncodingAttributes();
|
|
attrs.setFormat("ogg");// 转MP4
|
|
attrs.setFormat("ogg");// 转MP4
|
|
attrs.setAudioAttributes(audio);
|
|
attrs.setAudioAttributes(audio);
|
|
@ -295,23 +331,19 @@ public class CommonUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public static void main(String args[]) throws Exception{
|
|
|
|
System.out.print("中文".length());
|
|
|
|
System.out.print("中文".substring(0,30));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getSubString(String content,int min,int max){
|
|
|
|
if(StringUtils.isBlank(content)){
|
|
|
|
|
|
public String getSubString(String content, int min, int max) {
|
|
|
|
if (StringUtils.isBlank(content)) {
|
|
return "";
|
|
return "";
|
|
}else if(content.length()<=max){
|
|
|
|
|
|
} else if (content.length() <= max) {
|
|
return content;
|
|
return content;
|
|
}else{
|
|
|
|
return content.substring(min,max);
|
|
|
|
|
|
} else {
|
|
|
|
return content.substring(min, max);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public String saveVoiceToDisk(InputStream inputStream,String newFileName) throws Exception {
|
|
|
|
|
|
public String saveVoiceToDisk(InputStream inputStream, String newFileName) throws Exception {
|
|
// 文件保存的临时路径
|
|
// 文件保存的临时路径
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
// 拼接年月日路径
|
|
// 拼接年月日路径
|
|
@ -331,7 +363,7 @@ public class CommonUtil {
|
|
fileOutputStream.write(data, 0, len);
|
|
fileOutputStream.write(data, 0, len);
|
|
}
|
|
}
|
|
// 返回保存路径
|
|
// 返回保存路径
|
|
return tempPath+datePath + newFileName;
|
|
|
|
|
|
return tempPath + datePath + newFileName;
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
} finally {
|
|
} finally {
|
|
@ -355,17 +387,18 @@ public class CommonUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 传入身高体重,计算BMI值
|
|
* 传入身高体重,计算BMI值
|
|
|
|
*
|
|
* @param weightStr 体重
|
|
* @param weightStr 体重
|
|
* @param heightStr 身高
|
|
* @param heightStr 身高
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public double getBMIByWeightAndHeight(String weightStr,String heightStr){
|
|
|
|
|
|
public double getBMIByWeightAndHeight(String weightStr, String heightStr) {
|
|
|
|
|
|
DecimalFormat df2 = new DecimalFormat("###.00");
|
|
DecimalFormat df2 = new DecimalFormat("###.00");
|
|
|
|
|
|
double weight = Double.parseDouble(weightStr);
|
|
double weight = Double.parseDouble(weightStr);
|
|
Integer heightCM = Integer.parseInt(heightStr);
|
|
Integer heightCM = Integer.parseInt(heightStr);
|
|
heightStr = df2.format(heightCM/100d);
|
|
|
|
|
|
heightStr = df2.format(heightCM / 100d);
|
|
|
|
|
|
double height = Double.parseDouble(heightStr);
|
|
double height = Double.parseDouble(heightStr);
|
|
double bmi = weight / (height * height);
|
|
double bmi = weight / (height * height);
|
|
@ -374,4 +407,139 @@ public class CommonUtil {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 如果是外网那是发送到内网的服务器
|
|
|
|
*
|
|
|
|
* @param request
|
|
|
|
* @param response
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String toNeiWang(HttpServletRequest request, HttpServletResponse response) {
|
|
|
|
try {
|
|
|
|
logger.info(" picture to neiwang start");
|
|
|
|
MultipartRequest multipartRequest = (MultipartRequest) request;
|
|
|
|
String url = neiwangWlyy + request.getRequestURI();//uri请求路径 http://172.19.103.88/wlyy/upload/chat
|
|
|
|
String responseEntity = null;
|
|
|
|
logger.info(" picture to neiwang url:" + url);
|
|
|
|
for (Map.Entry<String, List<MultipartFile>> one : multipartRequest.getMultiFileMap().entrySet()) {
|
|
|
|
MultipartFile multipartFile = one.getValue().get(0);
|
|
|
|
logger.info(" picture to neiwang file size:" + multipartFile.getSize());
|
|
|
|
logger.info(" picture to neiwang file name:" + multipartFile.getName());
|
|
|
|
responseEntity = request(url, multipartFile);
|
|
|
|
}
|
|
|
|
logger.info(" picture to neiwang responseEntity:" + responseEntity);
|
|
|
|
logger.info(" picture to neiwang end");
|
|
|
|
return responseEntity;
|
|
|
|
} catch (Exception e) {
|
|
|
|
try {
|
|
|
|
logger.error(" picture to neiwang error");
|
|
|
|
Map<Object, Object> map = new HashMap<Object, Object>();
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
map.put("status", "-1");
|
|
|
|
map.put("msg", "上传失败");
|
|
|
|
return mapper.writeValueAsString(map);
|
|
|
|
} catch (JsonProcessingException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 如果是外网那是发送到内网的服务器
|
|
|
|
*
|
|
|
|
* @param files
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String toNeiWang(String files) throws FileNotFoundException {
|
|
|
|
//转发到内网服务器
|
|
|
|
String[] fileArray = files.split(",");
|
|
|
|
String fileUrls = "";
|
|
|
|
for (String file : fileArray) {
|
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
|
File f = new File(file);
|
|
|
|
if (f.exists()) {
|
|
|
|
String fileName = f.getName();
|
|
|
|
InputStream in = new FileInputStream(f);
|
|
|
|
JSONObject result=JSONObject.fromObject(request(request, in, fileName));
|
|
|
|
logger.info("fileName :"+fileName);
|
|
|
|
logger.info("result :"+result.toString());
|
|
|
|
if (result != null) {
|
|
|
|
//1.3.7去掉前缀
|
|
|
|
fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",")
|
|
|
|
+ result.get("groupName").toString().replaceAll("\"", "") + "/"
|
|
|
|
+ result.get("remoteFileName").toString().replaceAll("\"", "");
|
|
|
|
f.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fileUrls;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String request(String remote_url, MultipartFile file) {
|
|
|
|
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
|
|
|
|
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 + request.getRequestURI();//uri请求路径 http://172.19.103.88/wlyy/upload/chat
|
|
|
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|