123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- package com.yihu.wlyy.web.common;
- import java.beans.Encoder;
- import java.io.*;
- import java.text.SimpleDateFormat;
- import java.util.*;
- 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.fastdfs.FastDFSUtil;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiParam;
- import org.apache.commons.codec.EncoderException;
- import org.apache.commons.io.FileUtils;
- import org.json.JSONObject;
- import org.springframework.http.MediaType;
- 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.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;
- @Controller
- @RequestMapping(value = "/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
- @Api(description = "文件上传")
- public class FileUploadController extends BaseController {
- @ApiParam
- FastDFSUtil fastDFSUtil;
- /**
- * 患者头像上传
- *
- * @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 {
- String fastUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
- 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, "上传失败");
- }
- }
- /**
- * 图片上传
- *
- * @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<String> tempPaths = new ArrayList<String>();
- try {
- String fastUrl = SystemConf.getInstance().getSystemProperties().getProperty("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();
- //byte audioData[] = mf.getBytes();
- //AudioFormat af = getAudioFormat();
- //ByteArrayInputStream bais = new ByteArrayInputStream(audioData);
- //AudioInputStream ais = new AudioInputStream(bais,af, audioData.length / af.getFrameSize());
- //File file = new File("temp.wav");
- //InputStream in = new FileInputStream(file);
- //AudioSystem.write(ais, AudioFileFormat.Type.WAVE,file);
- fileName = mf.getOriginalFilename();
- String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
- if("3".equals(type)){
- fileExt ="wav";
- }
- ObjectNode objectNode = fastDFSUtil.upload(mf.getInputStream() ,fileExt,"");
- tempPaths.add(fastUrl + 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("msg", "上传成功");
- // 图片标识对象的HTTP链接
- json.put("urls", urls);
- System.out.println("附件上传:" + json.toString());
- return json.toString();
- } catch (Exception e) {
- error(e);
- 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, "上传失败");
- }
- }
- }
|