|
@ -1,24 +1,34 @@
|
|
|
package com.yihu.wlyy.web.common;
|
|
|
|
|
|
import java.beans.Encoder;
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
|
|
|
import javax.servlet.ServletRequest;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.sound.sampled.AudioFileFormat;
|
|
|
import javax.sound.sampled.AudioFormat;
|
|
|
import javax.sound.sampled.AudioInputStream;
|
|
|
import javax.sound.sampled.AudioSystem;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.yihu.wlyy.util.CommonUtil;
|
|
|
import com.yihu.wlyy.util.*;
|
|
|
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
|
|
|
import com.yihu.wlyy.web.third.gateway.vo.UploadModel;
|
|
|
import com.yihu.wlyy.web.third.gateway.vo.base.BaseResultModel;
|
|
|
import com.yihu.wlyy.web.third.gateway.vo.base.ResultOneModel;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
import org.apache.commons.codec.EncoderException;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
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.impl.client.HttpClients;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@ -27,14 +37,13 @@ import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.util.FileCopyUtils;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
|
|
import com.yihu.wlyy.util.DateUtil;
|
|
|
import com.yihu.wlyy.util.ImageCompress;
|
|
|
import com.yihu.wlyy.util.SystemConf;
|
|
|
import com.yihu.wlyy.web.BaseController;
|
|
|
import org.springframework.web.multipart.MultipartRequest;
|
|
|
|
|
|
@Controller
|
|
|
@RequestMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
@ -47,6 +56,10 @@ public class FileUploadController extends BaseController {
|
|
|
private CommonUtil CommonUtil;
|
|
|
@Value("${fastDFS.fastdfs_file_url}")
|
|
|
private String fastdfs_file_url;
|
|
|
@Value("${neiwang.enable}")
|
|
|
private Boolean isneiwang = false; //如果不是内网项目要转到到内网wlyy在上传
|
|
|
@Value("${neiwang.wlyy}")
|
|
|
private String neiwangWlyy; //内网的项目地址
|
|
|
|
|
|
/**
|
|
|
* 患者头像上传
|
|
@ -58,32 +71,36 @@ public class FileUploadController extends BaseController {
|
|
|
@RequestMapping(value = "patientPhoto", method = RequestMethod.POST/* , headers = "Accept=image/png" */)
|
|
|
@ResponseBody
|
|
|
public String patientPhoto(HttpServletRequest request, HttpServletResponse response, String photo) {
|
|
|
try {
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
String fileName = null;
|
|
|
String firstPhoto = null;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
// 上传文件
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
fileName = mf.getOriginalFilename();
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
// 重命名文件
|
|
|
firstPhoto = photo.substring(0, photo.lastIndexOf(".")) + "_small." + fileExt;
|
|
|
File uploadFile = new File(SystemConf.getInstance().getTempPath() + File.separator + firstPhoto);
|
|
|
// 拷贝文件流到指定文件路径
|
|
|
FileCopyUtils.copy(mf.getBytes(), uploadFile);
|
|
|
if (isneiwang) {
|
|
|
try {
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
String fileName = null;
|
|
|
String firstPhoto = null;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
// 上传文件
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
fileName = mf.getOriginalFilename();
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
// 重命名文件
|
|
|
firstPhoto = photo.substring(0, photo.lastIndexOf(".")) + "_small." + fileExt;
|
|
|
File uploadFile = new File(SystemConf.getInstance().getTempPath() + File.separator + firstPhoto);
|
|
|
// 拷贝文件流到指定文件路径
|
|
|
FileCopyUtils.copy(mf.getBytes(), uploadFile);
|
|
|
}
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", String.join(",", firstPhoto));
|
|
|
System.out.println("图片上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
return error(-1, "上传失败");
|
|
|
}
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", String.join(",", firstPhoto));
|
|
|
System.out.println("图片上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
return error(-1, "上传失败");
|
|
|
} else {
|
|
|
return toNeiWang(request, response);
|
|
|
}
|
|
|
}
|
|
|
|
|
@ -97,65 +114,69 @@ public class FileUploadController extends BaseController {
|
|
|
@RequestMapping(value = "image", method = RequestMethod.POST/* , headers = "Accept=image/png" */)
|
|
|
@ResponseBody
|
|
|
public String image(HttpServletRequest request, HttpServletResponse response) {
|
|
|
// 圖片列表
|
|
|
List<File> images = new ArrayList<File>();
|
|
|
List<String> tempPaths = new ArrayList<String>();
|
|
|
// 文件保存的临时路径
|
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
|
// 拼接年月日路径
|
|
|
String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
|
|
|
try {
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
// 创建文件夹
|
|
|
File file = new File(tempPath + datePath);
|
|
|
if (!file.exists()) {
|
|
|
file.mkdirs();
|
|
|
}
|
|
|
String fileName = null;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
// 上传文件
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
fileName = mf.getOriginalFilename();
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
// 重命名文件
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
|
|
|
File uploadFile = new File(tempPath + datePath + newFileName);
|
|
|
// 拷贝文件流到指定文件路径
|
|
|
FileCopyUtils.copy(mf.getBytes(), uploadFile);
|
|
|
// 生成缩略图
|
|
|
ImageCompress.compress(uploadFile.getAbsolutePath(), uploadFile.getAbsolutePath() + "_small", 300, 300);
|
|
|
// 添加到上传成功数组中
|
|
|
images.add(uploadFile);
|
|
|
tempPaths.add(datePath + newFileName);
|
|
|
}
|
|
|
String urls = "";
|
|
|
for (String image : tempPaths) {
|
|
|
if (urls.length() == 0) {
|
|
|
urls = image;
|
|
|
} else {
|
|
|
urls += "," + image;
|
|
|
}
|
|
|
}
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", urls);
|
|
|
System.out.println("图片上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
if (isneiwang) {
|
|
|
// 圖片列表
|
|
|
List<File> images = new ArrayList<File>();
|
|
|
List<String> tempPaths = new ArrayList<String>();
|
|
|
// 文件保存的临时路径
|
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
|
// 拼接年月日路径
|
|
|
String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
|
|
|
try {
|
|
|
// 清除垃圾图片
|
|
|
for (File file : images) {
|
|
|
FileUtils.forceDelete(file);
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
// 创建文件夹
|
|
|
File file = new File(tempPath + datePath);
|
|
|
if (!file.exists()) {
|
|
|
file.mkdirs();
|
|
|
}
|
|
|
String fileName = null;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
// 上传文件
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
fileName = mf.getOriginalFilename();
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
// 重命名文件
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
|
|
|
File uploadFile = new File(tempPath + datePath + newFileName);
|
|
|
// 拷贝文件流到指定文件路径
|
|
|
FileCopyUtils.copy(mf.getBytes(), uploadFile);
|
|
|
// 生成缩略图
|
|
|
ImageCompress.compress(uploadFile.getAbsolutePath(), uploadFile.getAbsolutePath() + "_small", 300, 300);
|
|
|
// 添加到上传成功数组中
|
|
|
images.add(uploadFile);
|
|
|
tempPaths.add(datePath + newFileName);
|
|
|
}
|
|
|
String urls = "";
|
|
|
for (String image : tempPaths) {
|
|
|
if (urls.length() == 0) {
|
|
|
urls = image;
|
|
|
} else {
|
|
|
urls += "," + image;
|
|
|
}
|
|
|
}
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", urls);
|
|
|
System.out.println("图片上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
try {
|
|
|
// 清除垃圾图片
|
|
|
for (File file : images) {
|
|
|
FileUtils.forceDelete(file);
|
|
|
}
|
|
|
} catch (Exception e2) {
|
|
|
error(e2);
|
|
|
}
|
|
|
} catch (Exception e2) {
|
|
|
error(e2);
|
|
|
return error(-1, "上传失败");
|
|
|
}
|
|
|
return error(-1, "上传失败");
|
|
|
} else {
|
|
|
return toNeiWang(request, response);
|
|
|
}
|
|
|
}
|
|
|
|
|
@ -169,93 +190,97 @@ public class FileUploadController extends BaseController {
|
|
|
@RequestMapping(value = "chat", method = RequestMethod.POST)
|
|
|
@ResponseBody
|
|
|
public String chatFile(HttpServletRequest request, HttpServletResponse response) {
|
|
|
List<String> tempPaths = new ArrayList<String>();
|
|
|
try {
|
|
|
String fastUrl = fastdfs_file_url;
|
|
|
String type = request.getParameter("type");
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
String fileName = null;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
if (isneiwang) {
|
|
|
List<String> tempPaths = new ArrayList<String>();
|
|
|
try {
|
|
|
String fastUrl = fastdfs_file_url;
|
|
|
String type = request.getParameter("type");
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
String fileName = null;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
|
|
|
// 上传文件
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
fileName = mf.getOriginalFilename();
|
|
|
// 上传文件
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
fileName = mf.getOriginalFilename();
|
|
|
|
|
|
if("3".equals(type)){
|
|
|
String tempPath = CommonUtil.saveVoiceToDisk(mf.getInputStream(),fileName);
|
|
|
String map3Path = tempPath.substring(0,tempPath.lastIndexOf("."));
|
|
|
CommonUtil.changeToMp3(tempPath,map3Path);
|
|
|
File tempFile = new File(tempPath);
|
|
|
File mp3File = new File(map3Path);
|
|
|
ObjectNode objectNode = fastDFSUtil.upload(new FileInputStream(mp3File),"mp3","");
|
|
|
//1.3.7 去掉前缀,返回相对路径
|
|
|
if ("3".equals(type)) {
|
|
|
String tempPath = CommonUtil.saveVoiceToDisk(mf.getInputStream(), fileName);
|
|
|
String map3Path = tempPath.substring(0, tempPath.lastIndexOf("."));
|
|
|
CommonUtil.changeToMp3(tempPath, map3Path);
|
|
|
File tempFile = new File(tempPath);
|
|
|
File mp3File = new File(map3Path);
|
|
|
ObjectNode objectNode = fastDFSUtil.upload(new FileInputStream(mp3File), "mp3", "");
|
|
|
//1.3.7 去掉前缀,返回相对路径
|
|
|
// tempPaths.add(fastUrl + objectNode.get("groupName").toString().replaceAll("\"","")
|
|
|
// + "/" + objectNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
tempPaths.add(objectNode.get("groupName").toString().replaceAll("\"","")
|
|
|
+ "/" + objectNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
if(tempFile!=null){
|
|
|
tempFile.delete();
|
|
|
}
|
|
|
if(mp3File!=null){
|
|
|
mp3File.delete();
|
|
|
}
|
|
|
}else if("4".equals(type)){
|
|
|
String tempPath = CommonUtil.saveVoiceToDisk(mf.getInputStream(),fileName);
|
|
|
String pngPath = tempPath.substring(0,tempPath.lastIndexOf("."))+".png";
|
|
|
File tempFile = new File(tempPath);
|
|
|
File pngFile = new File(pngPath);
|
|
|
long times = CommonUtil.getVideoTimeAndImg(tempPath,pngPath);
|
|
|
if(times<1000){
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", -1);
|
|
|
json.put("success",true);
|
|
|
json.put("msg", "视频录制时间太短!");
|
|
|
json.put("times", times);
|
|
|
return json.toString();
|
|
|
}
|
|
|
ObjectNode imgNode = fastDFSUtil.upload(new FileInputStream(pngPath),"png","");
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
ObjectNode videoNode = fastDFSUtil.upload(new FileInputStream(tempPath),fileExt,"");
|
|
|
tempPaths.add(objectNode.get("groupName").toString().replaceAll("\"", "")
|
|
|
+ "/" + objectNode.get("remoteFileName").toString().replaceAll("\"", ""));
|
|
|
if (tempFile != null) {
|
|
|
tempFile.delete();
|
|
|
}
|
|
|
if (mp3File != null) {
|
|
|
mp3File.delete();
|
|
|
}
|
|
|
} else if ("4".equals(type)) {
|
|
|
String tempPath = CommonUtil.saveVoiceToDisk(mf.getInputStream(), fileName);
|
|
|
String pngPath = tempPath.substring(0, tempPath.lastIndexOf(".")) + ".png";
|
|
|
File tempFile = new File(tempPath);
|
|
|
File pngFile = new File(pngPath);
|
|
|
long times = CommonUtil.getVideoTimeAndImg(tempPath, pngPath);
|
|
|
if (times < 1000) {
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", -1);
|
|
|
json.put("success", true);
|
|
|
json.put("msg", "视频录制时间太短!");
|
|
|
json.put("times", times);
|
|
|
return json.toString();
|
|
|
}
|
|
|
ObjectNode imgNode = fastDFSUtil.upload(new FileInputStream(pngPath), "png", "");
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
ObjectNode videoNode = fastDFSUtil.upload(new FileInputStream(tempPath), fileExt, "");
|
|
|
|
|
|
//1.3.7 去掉前缀,返回相对路径
|
|
|
//1.3.7 去掉前缀,返回相对路径
|
|
|
// tempPaths.add(fastUrl + imgNode.get("groupName").toString().replaceAll("\"","")
|
|
|
// + "/" + imgNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
// tempPaths.add(fastUrl + videoNode.get("groupName").toString().replaceAll("\"","")
|
|
|
// + "/" + videoNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
tempPaths.add(imgNode.get("groupName").toString().replaceAll("\"","")
|
|
|
+ "/" + imgNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
tempPaths.add(videoNode.get("groupName").toString().replaceAll("\"","")
|
|
|
+ "/" + videoNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
tempPaths.add(times+"");
|
|
|
if(tempFile!=null){
|
|
|
tempFile.delete();
|
|
|
}
|
|
|
if(pngFile!=null){
|
|
|
pngFile.delete();
|
|
|
}
|
|
|
} else{
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream() ,fileExt,"");
|
|
|
//1.3.7 去掉前缀,返回相对路径
|
|
|
tempPaths.add(imgNode.get("groupName").toString().replaceAll("\"", "")
|
|
|
+ "/" + imgNode.get("remoteFileName").toString().replaceAll("\"", ""));
|
|
|
tempPaths.add(videoNode.get("groupName").toString().replaceAll("\"", "")
|
|
|
+ "/" + videoNode.get("remoteFileName").toString().replaceAll("\"", ""));
|
|
|
tempPaths.add(times + "");
|
|
|
if (tempFile != null) {
|
|
|
tempFile.delete();
|
|
|
}
|
|
|
if (pngFile != null) {
|
|
|
pngFile.delete();
|
|
|
}
|
|
|
} else {
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream(), fileExt, "");
|
|
|
//1.3.7 去掉前缀,返回相对路径
|
|
|
// tempPaths.add(fastUrl + objectNode.get("groupName").toString().replaceAll("\"","")
|
|
|
// + "/" + objectNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
tempPaths.add(objectNode.get("groupName").toString().replaceAll("\"","")
|
|
|
+ "/" + objectNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
tempPaths.add(objectNode.get("groupName").toString().replaceAll("\"", "")
|
|
|
+ "/" + objectNode.get("remoteFileName").toString().replaceAll("\"", ""));
|
|
|
}
|
|
|
}
|
|
|
String urls = String.join(",", tempPaths);
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("success", true);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", urls);
|
|
|
System.out.println("附件上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
return error(-1, "上传失败");
|
|
|
}
|
|
|
String urls = String.join(",", tempPaths);
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("success",true);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", urls);
|
|
|
System.out.println("附件上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
return error(-1, "上传失败");
|
|
|
} else {
|
|
|
return toNeiWang(request, response);
|
|
|
}
|
|
|
}
|
|
|
|
|
@ -269,104 +294,254 @@ public class FileUploadController extends BaseController {
|
|
|
@RequestMapping(value = "voice", method = RequestMethod.POST)
|
|
|
@ResponseBody
|
|
|
public String voice(HttpServletRequest request, HttpServletResponse response) {
|
|
|
// 圖片列表
|
|
|
List<File> voices = new ArrayList<File>();
|
|
|
List<String> tempPaths = new ArrayList<String>();
|
|
|
// 文件保存的临时路径
|
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
|
// 拼接年月日路径
|
|
|
String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
|
|
|
try {
|
|
|
if (isneiwang) {
|
|
|
// 圖片列表
|
|
|
List<File> voices = new ArrayList<File>();
|
|
|
List<String> tempPaths = new ArrayList<String>();
|
|
|
// 文件保存的临时路径
|
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
|
// 拼接年月日路径
|
|
|
String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
|
|
|
try {
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
// 创建文件夹
|
|
|
File file = new File(tempPath + datePath);
|
|
|
if (!file.exists()) {
|
|
|
file.mkdirs();
|
|
|
}
|
|
|
String fileName = null;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
// 上传文件
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
fileName = mf.getOriginalFilename();
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
// 重命名文件
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
|
|
|
File uploadFile = new File(tempPath + datePath + newFileName);
|
|
|
// 拷贝文件流到指定文件路径
|
|
|
FileCopyUtils.copy(mf.getBytes(), uploadFile);
|
|
|
// 添加到上传成功数组中
|
|
|
voices.add(uploadFile);
|
|
|
tempPaths.add(datePath + newFileName);
|
|
|
}
|
|
|
String urls = "";
|
|
|
for (String voice : tempPaths) {
|
|
|
if (urls.length() == 0) {
|
|
|
urls = voice;
|
|
|
} else {
|
|
|
urls += "," + voice;
|
|
|
}
|
|
|
}
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", urls);
|
|
|
System.out.println("语音上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
try {
|
|
|
// 清除垃圾图片
|
|
|
for (File file : voices) {
|
|
|
FileUtils.forceDelete(file);
|
|
|
}
|
|
|
} catch (Exception e2) {
|
|
|
error(e2);
|
|
|
}
|
|
|
return error(-1, "上传失败");
|
|
|
}
|
|
|
} else {
|
|
|
return toNeiWang(request, response);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 语音上传
|
|
|
*
|
|
|
* @param request
|
|
|
* @param response
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "test", method = RequestMethod.POST)
|
|
|
@ResponseBody
|
|
|
public String test(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
if (isneiwang) {
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
// 创建文件夹
|
|
|
File file = new File(tempPath + datePath);
|
|
|
if (!file.exists()) {
|
|
|
file.mkdirs();
|
|
|
}
|
|
|
String fileName = null;
|
|
|
List<String> tempPaths = new ArrayList<String>();
|
|
|
String fastUrl = fastdfs_file_url;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
// 上传文件
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
fileName = mf.getOriginalFilename();
|
|
|
String fileName = mf.getOriginalFilename();
|
|
|
String tempPath = CommonUtil.saveVoiceToDisk(mf.getInputStream(), fileName);
|
|
|
String pngPath = tempPath.substring(0, tempPath.lastIndexOf(".")) + ".png";
|
|
|
File tempFile = new File(tempPath);
|
|
|
File pngFile = new File(pngPath);
|
|
|
long times = CommonUtil.getVideoTimeAndImg(tempPath, pngPath);
|
|
|
ObjectNode imgNode = fastDFSUtil.upload(new FileInputStream(pngPath), "png", "");
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
// 重命名文件
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
|
|
|
File uploadFile = new File(tempPath + datePath + newFileName);
|
|
|
// 拷贝文件流到指定文件路径
|
|
|
FileCopyUtils.copy(mf.getBytes(), uploadFile);
|
|
|
// 添加到上传成功数组中
|
|
|
voices.add(uploadFile);
|
|
|
tempPaths.add(datePath + newFileName);
|
|
|
}
|
|
|
String urls = "";
|
|
|
for (String voice : tempPaths) {
|
|
|
if (urls.length() == 0) {
|
|
|
urls = voice;
|
|
|
} else {
|
|
|
urls += "," + voice;
|
|
|
ObjectNode videoNode = fastDFSUtil.upload(new FileInputStream(tempPath), fileExt, "");
|
|
|
tempPaths.add(fastUrl + imgNode.get("groupName").toString().replaceAll("\"", "")
|
|
|
+ "/" + imgNode.get("remoteFileName").toString().replaceAll("\"", ""));
|
|
|
tempPaths.add(fastUrl + videoNode.get("groupName").toString().replaceAll("\"", "")
|
|
|
+ "/" + videoNode.get("remoteFileName").toString().replaceAll("\"", ""));
|
|
|
tempPaths.add(times + "");
|
|
|
if (tempFile != null) {
|
|
|
tempFile.delete();
|
|
|
}
|
|
|
}
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", urls);
|
|
|
System.out.println("语音上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
try {
|
|
|
// 清除垃圾图片
|
|
|
for (File file : voices) {
|
|
|
FileUtils.forceDelete(file);
|
|
|
if (pngFile != null) {
|
|
|
pngFile.delete();
|
|
|
}
|
|
|
} catch (Exception e2) {
|
|
|
error(e2);
|
|
|
}
|
|
|
return error(-1, "上传失败");
|
|
|
String urls = String.join(",", tempPaths);
|
|
|
return urls;
|
|
|
} else {
|
|
|
return toNeiWang(request, response);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 语音上传
|
|
|
* 测试
|
|
|
*
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
* @throws IllegalStateException
|
|
|
*/
|
|
|
@RequestMapping(value = "/test_cwd", method = RequestMethod.POST)
|
|
|
@ApiOperation(value = "单文件上传")
|
|
|
@ResponseBody
|
|
|
public String uploadFile(
|
|
|
@ApiParam(value = "文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file1,
|
|
|
HttpServletRequest request,
|
|
|
HttpServletResponse response
|
|
|
) {
|
|
|
try {
|
|
|
if (isneiwang) {
|
|
|
// 圖片列表
|
|
|
List<File> images = new ArrayList<File>();
|
|
|
List<String> tempPaths = new ArrayList<String>();
|
|
|
// 文件保存的临时路径
|
|
|
String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
|
|
|
// 拼接年月日路径
|
|
|
String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
|
|
|
try {
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
// 创建文件夹
|
|
|
File file = new File(tempPath + datePath);
|
|
|
if (!file.exists()) {
|
|
|
file.mkdirs();
|
|
|
}
|
|
|
String fileName = null;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
// 上传文件
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
fileName = mf.getOriginalFilename();
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
// 重命名文件
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
|
|
|
File uploadFile = new File(tempPath + datePath + newFileName);
|
|
|
// 拷贝文件流到指定文件路径
|
|
|
FileCopyUtils.copy(mf.getBytes(), uploadFile);
|
|
|
// 生成缩略图
|
|
|
ImageCompress.compress(uploadFile.getAbsolutePath(), uploadFile.getAbsolutePath() + "_small", 300, 300);
|
|
|
// 添加到上传成功数组中
|
|
|
images.add(uploadFile);
|
|
|
tempPaths.add(datePath + newFileName);
|
|
|
}
|
|
|
String urls = "";
|
|
|
for (String image : tempPaths) {
|
|
|
if (urls.length() == 0) {
|
|
|
urls = image;
|
|
|
} else {
|
|
|
urls += "," + image;
|
|
|
}
|
|
|
}
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", urls);
|
|
|
System.out.println("图片上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
try {
|
|
|
// 清除垃圾图片
|
|
|
for (File file : images) {
|
|
|
FileUtils.forceDelete(file);
|
|
|
}
|
|
|
} catch (Exception e2) {
|
|
|
error(e2);
|
|
|
}
|
|
|
return error(-1, "上传失败");
|
|
|
}
|
|
|
} else {
|
|
|
return toNeiWang(request, response);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
// return new ResultOneModel(BaseResultModel.statusEm.file_upload_error.getCode(), BaseResultModel.statusEm.file_upload_error.getMessage() + "," + e.getMessage(), new com.alibaba.fastjson.JSONObject());
|
|
|
return e.getMessage();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 如果是外网那是发送到内网的服务器
|
|
|
*
|
|
|
* @param request
|
|
|
* @param response
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping(value = "test", method = RequestMethod.POST)
|
|
|
@ResponseBody
|
|
|
public String test(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
List<String> tempPaths = new ArrayList<String>();
|
|
|
String fastUrl = fastdfs_file_url;
|
|
|
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
MultipartFile mf = entity.getValue();
|
|
|
String fileName = mf.getOriginalFilename();
|
|
|
String tempPath = CommonUtil.saveVoiceToDisk(mf.getInputStream(),fileName);
|
|
|
String pngPath = tempPath.substring(0,tempPath.lastIndexOf("."))+".png";
|
|
|
File tempFile = new File(tempPath);
|
|
|
File pngFile = new File(pngPath);
|
|
|
long times = CommonUtil.getVideoTimeAndImg(tempPath,pngPath);
|
|
|
ObjectNode imgNode = fastDFSUtil.upload(new FileInputStream(pngPath),"png","");
|
|
|
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
ObjectNode videoNode = fastDFSUtil.upload(new FileInputStream(tempPath),fileExt,"");
|
|
|
tempPaths.add(fastUrl + imgNode.get("groupName").toString().replaceAll("\"","")
|
|
|
+ "/" + imgNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
tempPaths.add(fastUrl + videoNode.get("groupName").toString().replaceAll("\"","")
|
|
|
+ "/" + videoNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
tempPaths.add(times+"");
|
|
|
if(tempFile!=null){
|
|
|
tempFile.delete();
|
|
|
private String toNeiWang(HttpServletRequest request, HttpServletResponse response) {
|
|
|
try {
|
|
|
MultipartRequest multipartRequest= (MultipartRequest) request;
|
|
|
String url = neiwangWlyy + request.getRequestURI();//uri请求路径 http://172.19.103.88/wlyy/upload/chat
|
|
|
String responseEntity =null;
|
|
|
for(Map.Entry<String,List<MultipartFile >> one:multipartRequest.getMultiFileMap().entrySet()){
|
|
|
responseEntity = request(url,one.getValue().get(0));
|
|
|
}
|
|
|
return responseEntity;
|
|
|
} catch (Exception e) {
|
|
|
return error(-1, "上传失败");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private String request(String remote_url, MultipartFile file) {
|
|
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
System.out.println(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"));
|
|
|
}
|
|
|
if(pngFile!=null){
|
|
|
pngFile.delete();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
httpClient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
String urls = String.join(",", tempPaths);
|
|
|
return urls;
|
|
|
return result;
|
|
|
}
|
|
|
}
|