|
@ -8,8 +8,10 @@ import java.util.*;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
import com.yihu.wlyy.util.CommonUtil;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.http.MediaType;
|
|
@ -30,6 +32,10 @@ import com.yihu.wlyy.web.BaseController;
|
|
|
@RequestMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
|
@Api(description = "文件上传")
|
|
|
public class FileUploadController extends BaseController {
|
|
|
|
|
|
@ApiParam
|
|
|
FastDFSUtil fastDFSUtil;
|
|
|
|
|
|
/**
|
|
|
* 患者头像上传
|
|
|
*
|
|
@ -41,6 +47,7 @@ public class FileUploadController extends BaseController {
|
|
|
@ResponseBody
|
|
|
public String patientPhoto(HttpServletRequest request, HttpServletResponse response, String photo) {
|
|
|
try {
|
|
|
String fastUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
String fileName = null;
|
|
@ -56,12 +63,11 @@ public class FileUploadController extends BaseController {
|
|
|
// 拷贝文件流到指定文件路径
|
|
|
FileCopyUtils.copy(mf.getBytes(), uploadFile);
|
|
|
}
|
|
|
String urls = CommonUtil.copyTempImage(firstPhoto);
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("msg", "上传成功");
|
|
|
// 图片标识对象的HTTP链接
|
|
|
json.put("urls", urls);
|
|
|
json.put("urls", String.join(",", firstPhoto));
|
|
|
System.out.println("图片上传:" + json.toString());
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
@ -152,38 +158,23 @@ public class FileUploadController extends BaseController {
|
|
|
@RequestMapping(value = "chat", method = RequestMethod.POST)
|
|
|
@ResponseBody
|
|
|
public String chatFile(HttpServletRequest request, HttpServletResponse response) {
|
|
|
// 圖片列表
|
|
|
List<File> files = 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 {
|
|
|
String fastUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
|
|
|
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);
|
|
|
// 添加到上传成功数组中
|
|
|
files.add(uploadFile);
|
|
|
tempPaths.add(datePath + newFileName);
|
|
|
ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream(),fileExt,"");
|
|
|
tempPaths.add(fastUrl + objectNode.get("groupName").toString().replaceAll("\"","")
|
|
|
+ "/" + objectNode.get("remoteFileName").toString().replaceAll("\"",""));
|
|
|
}
|
|
|
String urls = CommonUtil.copyTempImage(String.join(",", tempPaths));
|
|
|
String urls = String.join(",", tempPaths);
|
|
|
JSONObject json = new JSONObject();
|
|
|
json.put("status", 200);
|
|
|
json.put("msg", "上传成功");
|
|
@ -193,14 +184,6 @@ public class FileUploadController extends BaseController {
|
|
|
return json.toString();
|
|
|
} catch (Exception e) {
|
|
|
error(e);
|
|
|
try {
|
|
|
// 清除垃圾图片
|
|
|
for (File file : files) {
|
|
|
FileUtils.forceDelete(file);
|
|
|
}
|
|
|
} catch (Exception e2) {
|
|
|
error(e2);
|
|
|
}
|
|
|
return error(-1, "上传失败");
|
|
|
}
|
|
|
}
|