|
@ -44,13 +44,8 @@ public class CommonUtil {
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(CommonUtil.class);
|
|
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; //内网的项目地址
|
|
|
|
|
|
private String fastdfs_file_url;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private HttpClientUtil httpClientUtil;
|
|
private HttpClientUtil httpClientUtil;
|
|
@ -144,88 +139,26 @@ public class CommonUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public String copyTempVoice(String voices) throws Exception {
|
|
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 serverUrl = fastdfs_file_url;
|
|
|
|
FastDFSUtil fastDFSUtil = new FastDFSUtil();
|
|
String fileUrls = "";
|
|
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+",";
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
logger.info(" toNeiWang end files :" + fileUrls);
|
|
|
|
|
|
|
|
return fileUrls.substring(0,fileUrls.length()-1);
|
|
|
|
|
|
return fileUrls;
|
|
}
|
|
}
|
|
|
|
|
|
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) {
|
|
public String request(String remote_url, MultipartFile file, String type) {
|
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
@ -262,79 +195,34 @@ public class CommonUtil {
|
|
return result;
|
|
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 {
|
|
public String copyTempImage(String files) throws Exception {
|
|
String tempPath = SystemConfEntity.getInstance().getTempPath() + File.separator;
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
// 文件保存的临时路径
|
|
|
|
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;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return fileUrls;
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|