|
@ -9,14 +9,17 @@ import net.sf.json.JSONObject;
|
|
|
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.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
@ -29,6 +32,7 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@ -44,6 +48,8 @@ public class CommonUtil {
|
|
|
private String neiwangWlyy; //内网的项目地址
|
|
|
@Value("${neiwang.enable}")
|
|
|
private Boolean isneiwang = false; //如果不是内网项目要转到到内网wlyy在上传
|
|
|
@Autowired
|
|
|
private HttpClientUtil httpClientUtil;
|
|
|
|
|
|
/**
|
|
|
* 获取图片全路径
|
|
@ -149,7 +155,7 @@ public class CommonUtil {
|
|
|
}
|
|
|
return fileUrls;
|
|
|
} else {
|
|
|
String fileUrls = toNeiWang(voices,"");
|
|
|
String fileUrls = toNeiWang(voices, "");
|
|
|
return fileUrls;
|
|
|
}
|
|
|
}
|
|
@ -246,13 +252,12 @@ public class CommonUtil {
|
|
|
|
|
|
return fileUrls;
|
|
|
} else {
|
|
|
String fileUrls = toNeiWang(files,tempPath);
|
|
|
String fileUrls = toNeiWang(files, tempPath);
|
|
|
return fileUrls;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void changeToMp3(String sourcePath, String targetPath) {
|
|
|
File source = new File(sourcePath);
|
|
|
File target = new File(targetPath);
|
|
@ -332,7 +337,6 @@ public class CommonUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getSubString(String content, int min, int max) {
|
|
|
if (StringUtils.isBlank(content)) {
|
|
|
return "";
|
|
@ -427,7 +431,7 @@ public class CommonUtil {
|
|
|
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,type);
|
|
|
responseEntity = request(url, multipartFile, type);
|
|
|
}
|
|
|
logger.info(" picture to neiwang responseEntity:" + responseEntity);
|
|
|
logger.info(" picture to neiwang end");
|
|
@ -451,37 +455,61 @@ public class CommonUtil {
|
|
|
* 如果是外网那是发送到内网的服务器
|
|
|
*
|
|
|
* @param files
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public String toNeiWang(String files,String tempPath) throws FileNotFoundException {
|
|
|
logger.info(" toNeiWang start files :"+tempPath+files);
|
|
|
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();
|
|
|
File f = new File(tempPath+file);
|
|
|
logger.info(" toNeiWang File exists :"+f.exists());
|
|
|
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());
|
|
|
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);
|
|
|
logger.info(" toNeiWang end files :" + fileUrls);
|
|
|
return fileUrls;
|
|
|
}
|
|
|
|
|
|
public String request(String remote_url, MultipartFile file,String type) {
|
|
|
private String request(HttpServletRequest request, String path) {
|
|
|
String url = neiwangWlyy + "/wlyy/upload/commonUpload";//uri请求路径 http://172.19.103.88/wlyy/upload/chat
|
|
|
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 = "";
|
|
@ -491,7 +519,9 @@ public class CommonUtil {
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
|
|
|
builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
|
|
|
builder.addTextBody("type",type); //发送类型
|
|
|
if (!org.springframework.util.StringUtils.isEmpty(type)) {
|
|
|
builder.addTextBody("type", type); //发送类型
|
|
|
}
|
|
|
HttpEntity entity = builder.build();
|
|
|
httpPost.setEntity(entity);
|
|
|
HttpResponse response = httpClient.execute(httpPost);// 执行提交
|
|
@ -525,7 +555,9 @@ public class CommonUtil {
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
builder.addBinaryBody("file", in, ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
|
|
|
builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
|
|
|
builder.addTextBody("type",request.getParameter("type")); //发送类型
|
|
|
if (!org.springframework.util.StringUtils.isEmpty(request.getParameter("type"))) {
|
|
|
builder.addTextBody("type", request.getParameter("type")); //发送类型
|
|
|
}
|
|
|
HttpEntity entity = builder.build();
|
|
|
httpPost.setEntity(entity);
|
|
|
HttpResponse response = httpClient.execute(httpPost);// 执行提交
|
|
@ -544,6 +576,11 @@ public class CommonUtil {
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
try {
|
|
|
in.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|