chenweida 7 år sedan
förälder
incheckning
76f5327e67

+ 55 - 18
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/util/CommonUtil.java

@ -9,14 +9,17 @@ import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
@ -29,6 +32,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -44,6 +48,8 @@ public class CommonUtil {
    private String neiwangWlyy;  //内网的项目地址
    @Value("${neiwang.enable}")
    private Boolean isneiwang = false;  //如果不是内网项目要转到到内网wlyy在上传
    @Autowired
    private HttpClientUtil httpClientUtil;
    /**
     * 获取图片全路径
@ -149,7 +155,7 @@ public class CommonUtil {
            }
            return fileUrls;
        } else {
            String fileUrls = toNeiWang(voices,"");
            String fileUrls = toNeiWang(voices, "");
            return fileUrls;
        }
    }
@ -246,13 +252,12 @@ public class CommonUtil {
            return fileUrls;
        } else {
            String fileUrls = toNeiWang(files,tempPath);
            String fileUrls = toNeiWang(files, tempPath);
            return fileUrls;
        }
    }
    public void changeToMp3(String sourcePath, String targetPath) {
        File source = new File(sourcePath);
        File target = new File(targetPath);
@ -332,7 +337,6 @@ public class CommonUtil {
    }
    public String getSubString(String content, int min, int max) {
        if (StringUtils.isBlank(content)) {
            return "";
@ -427,7 +431,7 @@ public class CommonUtil {
                MultipartFile multipartFile = one.getValue().get(0);
                logger.info(" picture to neiwang file size:" + multipartFile.getSize());
                logger.info(" picture to neiwang file name:" + multipartFile.getName());
                responseEntity = request(url, multipartFile,type);
                responseEntity = request(url, multipartFile, type);
            }
            logger.info(" picture to neiwang responseEntity:" + responseEntity);
            logger.info(" picture to neiwang end");
@ -451,37 +455,61 @@ public class CommonUtil {
     * 如果是外网那是发送到内网的服务器
     *
     * @param files
     *
     * @return
     */
    public String toNeiWang(String files,String tempPath) throws FileNotFoundException {
        logger.info(" toNeiWang start files :"+tempPath+files);
    public String toNeiWang(String files, String tempPath) throws FileNotFoundException {
        logger.info(" toNeiWang start files :" + tempPath + files);
        //转发到内网服务器
        String[] fileArray = files.split(",");
        String fileUrls = "";
        for (String file : fileArray) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            File f = new File(tempPath+file);
            logger.info(" toNeiWang File exists :"+f.exists());
            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());
                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;
                }
            }
        }
        logger.info(" toNeiWang end files :"+fileUrls);
        logger.info(" toNeiWang end files :" + fileUrls);
        return fileUrls;
    }
    public String request(String remote_url, MultipartFile file,String type) {
    private String request(HttpServletRequest request, String path) {
        String url = neiwangWlyy + "/wlyy/upload/commonUpload";//uri请求路径 http://172.19.103.88/wlyy/upload/chat
        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) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        logger.info(file.getOriginalFilename());
        String result = "";
@ -491,7 +519,9 @@ public class CommonUtil {
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
            builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
            builder.addTextBody("type",type); //发送类型
            if (!org.springframework.util.StringUtils.isEmpty(type)) {
                builder.addTextBody("type", type); //发送类型
            }
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);// 执行提交
@ -525,7 +555,9 @@ public class CommonUtil {
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("file", in, ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
            builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
            builder.addTextBody("type",request.getParameter("type")); //发送类型
            if (!org.springframework.util.StringUtils.isEmpty(request.getParameter("type"))) {
                builder.addTextBody("type", request.getParameter("type")); //发送类型
            }
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);// 执行提交
@ -544,6 +576,11 @@ public class CommonUtil {
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

+ 49 - 27
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/web/common/FileUploadController.java

@ -51,7 +51,7 @@ import java.util.*;
@Api(description = "文件上传")
public class FileUploadController extends BaseController {
    private Logger logger= LoggerFactory.getLogger(FileUploadController.class);
    private Logger logger = LoggerFactory.getLogger(FileUploadController.class);
    @ApiParam
    FastDFSUtil fastDFSUtil;
    @Autowired
@ -62,7 +62,6 @@ public class FileUploadController extends BaseController {
    private Boolean isneiwang = false;  //如果不是内网项目要转到到内网wlyy在上传
    /**
     * 患者头像上传
     *
@ -102,7 +101,7 @@ public class FileUploadController extends BaseController {
                return error(-1, "上传失败");
            }
        } else {
            return  CommonUtil.toNeiWang(request, response);
            return CommonUtil.toNeiWang(request, response);
        }
    }
@ -163,7 +162,7 @@ public class FileUploadController extends BaseController {
                json.put("msg", "上传成功");
                // 图片标识对象的HTTP链接
                json.put("urls", urls);
               logger.info("图片上传:" + json.toString());
                logger.info("图片上传:" + json.toString());
                return json.toString();
            } catch (Exception e) {
                error(e);
@ -178,7 +177,7 @@ public class FileUploadController extends BaseController {
                return error(-1, "上传失败");
            }
        } else {
            return  CommonUtil.toNeiWang(request, response);
            return CommonUtil.toNeiWang(request, response);
        }
    }
@ -275,14 +274,14 @@ public class FileUploadController extends BaseController {
                json.put("msg", "上传成功");
                // 图片标识对象的HTTP链接
                json.put("urls", urls);
               logger.info("附件上传:" + json.toString());
                logger.info("附件上传:" + json.toString());
                return json.toString();
            } catch (Exception e) {
                error(e);
                return error(-1, "上传失败");
            }
        } else {
            return  CommonUtil.toNeiWang(request, response);
            return CommonUtil.toNeiWang(request, response);
        }
    }
@ -341,7 +340,7 @@ public class FileUploadController extends BaseController {
                json.put("msg", "上传成功");
                // 图片标识对象的HTTP链接
                json.put("urls", urls);
               logger.info("语音上传:" + json.toString());
                logger.info("语音上传:" + json.toString());
                return json.toString();
            } catch (Exception e) {
                error(e);
@ -356,7 +355,7 @@ public class FileUploadController extends BaseController {
                return error(-1, "上传失败");
            }
        } else {
            return  CommonUtil.toNeiWang(request, response);
            return CommonUtil.toNeiWang(request, response);
        }
    }
@ -402,7 +401,7 @@ public class FileUploadController extends BaseController {
            String urls = String.join(",", tempPaths);
            return urls;
        } else {
            return  CommonUtil.toNeiWang(request, response);
            return CommonUtil.toNeiWang(request, response);
        }
    }
@ -470,7 +469,7 @@ public class FileUploadController extends BaseController {
                    json.put("msg", "上传成功");
                    // 图片标识对象的HTTP链接
                    json.put("urls", urls);
                   logger.info("图片上传:" + json.toString());
                    logger.info("图片上传:" + json.toString());
                    return json.toString();
                } catch (Exception e) {
                    error(e);
@ -485,7 +484,7 @@ public class FileUploadController extends BaseController {
                    return error(-1, "上传失败");
                }
            } else {
                return  CommonUtil.toNeiWang(request, response);
                return CommonUtil.toNeiWang(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
@ -499,22 +498,45 @@ public class FileUploadController extends BaseController {
    @ApiOperation("公共的文件上传")
    public String commonUpload(HttpServletRequest request) {
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            // 文件保存的临时路径
            FastDFSUtil fastDFSUtil = new FastDFSUtil();
            String paths = request.getParameter("filePaths");
            String fileUrls = "";
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            String fileName = null;
            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                // 上传文件
                MultipartFile mf = entity.getValue();
                fileName = mf.getOriginalFilename();
                InputStream in = mf.getInputStream();
                ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
                if (result != null) {
                    fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",")
                            + result.get("groupName").toString().replaceAll("\"", "") + "/"
                            + result.get("remoteFileName").toString().replaceAll("\"", "");
            if (StringUtils.isBlank(paths)) {
                //为空是文件上传
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                // 文件保存的临时路径
                FastDFSUtil fastDFSUtil = new FastDFSUtil();
                Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
                String fileName = null;
                for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                    // 上传文件
                    MultipartFile mf = entity.getValue();
                    fileName = mf.getOriginalFilename();
                    InputStream in = mf.getInputStream();
                    ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
                    in.close();
                    if (result != null) {
                        fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",")
                                + result.get("groupName").toString().replaceAll("\"", "") + "/"
                                + result.get("remoteFileName").toString().replaceAll("\"", "");
                    }
                }
            } else {
                String[] pathArr = paths.split(",");
                for (String path : pathArr) {
                    //传路径自己去路径上传
                    File file = new File(path);
                    String fileName = null;
                    if (file.exists()) {
                        fileName = file.getName();
                        InputStream in = new FileInputStream(file);
                        ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
                        in.close();
                        if (result != null) {
                            fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",")
                                    + result.get("groupName").toString().replaceAll("\"", "") + "/"
                                    + result.get("remoteFileName").toString().replaceAll("\"", "");
                        }
                    }
                }
            }
            return fileUrls;