Browse Source

文件上传修改

lyr 8 years ago
parent
commit
bfa069d7ac

+ 243 - 267
patient-co-wlyy/src/main/java/com/yihu/wlyy/web/common/FileUploadController.java

@ -3,15 +3,12 @@ package com.yihu.wlyy.web.common;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yihu.wlyy.util.CommonUtil;
import io.swagger.annotations.Api;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
@ -33,269 +30,248 @@ import com.yihu.wlyy.web.BaseController;
@RequestMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "文件上传")
public class FileUploadController extends BaseController {
	/**
	 * 患者头像上传
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@RequestMapping(value = "patientPhoto", method = RequestMethod.POST/* , headers = "Accept=image/png" */)
	@ResponseBody
	public String patientPhoto(HttpServletRequest request, HttpServletResponse response,String photo) {
		// 圖片列表
		//List<File> images = new ArrayList<File>();
		//List<String> tempPaths = new ArrayList<String>();
		// 文件保存的临时路径
		//String tempPath = SystemConf.getInstance().getImagePath() + 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;
			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().getImagePath() + File.separator+firstPhoto);
				// 拷贝文件流到指定文件路径
				FileCopyUtils.copy(mf.getBytes(), uploadFile);
				// 生成缩略图
				//ImageCompress.compress(uploadFile.getAbsolutePath(), uploadFile.getAbsolutePath() + "_small", 300, 300);
				// 添加到上传成功数组中
				//images.add(uploadFile);
				//tempPaths.add(datePath + newFileName);
			}
			String urls = firstPhoto;
			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);
			return error(-1, "上传失败");
		}
	}
	/** 
	 * 图片上传 
	 * @return 
	 * @throws IOException  
	 * @throws IllegalStateException  
	 */
	@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);
			try {
				// 清除垃圾图片
				for (File file : images) {
					FileUtils.forceDelete(file);
				}
			} catch (Exception e2) {
				error(e2);
			}
			return error(-1, "上传失败");
		}
	}
    /**
     * 患者头像上传
     *
     * @return
     * @throws IOException
     * @throws IllegalStateException
     */
    @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().getImagePath() + File.separator + firstPhoto);
                // 拷贝文件流到指定文件路径
                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);
            System.out.println("图片上传:" + json.toString());
            return json.toString();
        } catch (Exception e) {
            error(e);
            return error(-1, "上传失败");
        }
    }
	/** 
	 * 聊天附件上传
	 * @return 
	 * @throws IOException  
	 * @throws IllegalStateException  
	 */
	@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().getChatPath() + 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);
				// 添加到上传成功数组中
				files.add(uploadFile);
				tempPaths.add(datePath + newFileName);
			}
			String urls = "";
			for (String temp : tempPaths) {
				if (urls.length() == 0) {
					urls = SystemConf.getInstance().getChatServer() + temp;
				} else {
					urls += "," + SystemConf.getInstance().getChatServer() + temp;
				}
			}
			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 : files) {
					FileUtils.forceDelete(file);
				}
			} catch (Exception e2) {
				error(e2);
			}
			return error(-1, "上传失败");
		}
	}
	/**
	 * 语音上传
	 * @param request
	 * @param response
	 * @return
	 */
	@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 {
			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, "上传失败");
		}
	}
    /**
     * 图片上传
     *
     * @return
     * @throws IOException
     * @throws IllegalStateException
     */
    @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);
            try {
                // 清除垃圾图片
                for (File file : images) {
                    FileUtils.forceDelete(file);
                }
            } catch (Exception e2) {
                error(e2);
            }
            return error(-1, "上传失败");
        }
    }
    /**
     * 聊天附件上传
     *
     * @return
     * @throws IOException
     * @throws IllegalStateException
     */
    @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 {
            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);
            }
            String urls = CommonUtil.copyTempImage(String.join(",", tempPaths));
            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 : files) {
                    FileUtils.forceDelete(file);
                }
            } catch (Exception e2) {
                error(e2);
            }
            return error(-1, "上传失败");
        }
    }
    /**
     * 语音上传
     *
     * @param request
     * @param response
     * @return
     */
    @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 {
            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, "上传失败");
        }
    }
}

+ 3 - 2
patient-co-wlyy/src/main/java/com/yihu/wlyy/web/doctor/consult/DoctorConsultController.java

@ -505,8 +505,9 @@ public class DoctorConsultController extends WeixinBaseController {
                        JSONObject json = new JSONObject();
                        json.put("first", "问诊回复提醒");
                        json.put("toUser", p.getCode());
                        json.put("consultcontent", ct.getSymptoms());
                        String replycontent = content.length() > 15 ? content.substring(0, 15) + "..." : content;
                        String symp = ct.getSymptoms();
                        json.put("consultcontent", StringUtils.isNotEmpty(symp) && symp.length() > 50 ? (symp.substring(0, 50) + "...")  : content);
                        String replycontent = content.length() > 50 ? content.substring(0, 50) + "..." : content;
                        if (type == 2) {
                            replycontent = "[图片]";
                        } else if (type == 3) {

+ 3 - 2
patient-co-wlyy/src/main/java/com/yihu/wlyy/web/doctor/consult/DoctorConsultPublicController.java

@ -233,8 +233,9 @@ public class DoctorConsultPublicController extends BaseController {
				json.put("isPublic", 1);
				json.put("first", "您的公共咨询有新的回复");
				json.put("toUser", p.getCode());
				json.put("consultcontent", cp.getContent());
				String replycontent = content.length() > 15 ? content.substring(0, 15) + "..." : content;
				String symp = cp.getContent();
				json.put("consultcontent", StringUtils.isNotEmpty(symp) && symp.length() > 50 ? (symp.substring(0, 50) + "...")  : content);
				String replycontent = content.length() > 50 ? content.substring(0, 50) + "..." : content;
				json.put("consult", consult);
				json.put("replycontent", replycontent);
				json.put("doctorName", doctor.getName());

+ 6 - 6
patient-co-wlyy/src/main/resources/system.properties

@ -50,9 +50,9 @@ chat_server=http://weixin.xmtyw.cn/res/chat/
sign_check_upload=http://172.19.103.87:8011/wlyy_service
# IM配置
im_list_get=http://172.19.103.76:3000/
im_group_server=http://120.41.252.108:3031/api/v1/chats/gm
msg_push_server=http://192.168.131.102:3000/api/v1/chats/sm
im_list_get=http://172.19.103.29:3000/
im_group_server=http://172.19.103.29:3000/api/v1/chats/gm
msg_push_server=http://172.19.103.29:3000/api/v1/chats/sm
# 微信基本配置
appId=wxd03f859efdf0873d
@ -103,9 +103,9 @@ fastdfs_file_url=http://172.19.103.54:80/
#sign_check_upload=http://172.19.103.85:8011/wlyy_service
#
## IM配置
#im_list_get=http://172.19.103.76:3000/
#im_group_server=http://120.41.252.108:3031/api/v1/chats/gm
#msg_push_server=http://192.168.131.102:3000/api/v1/chats/sm
#im_list_get=http://172.19.103.88:3000/
#im_group_server=http://172.19.103.88:3000/api/v1/chats/gm
#msg_push_server=http://172.19.103.88:3000/api/v1/chats/sm
#
## 微信基本配置
#appId=wx1f129f7b51701428