LAPTOP-KB9HII50\70708 1 日 前
コミット
cca55578e3

+ 60 - 0
svr/svr-iot/src/main/java/com/yihu/iot/controller/third/ThirdDataInputController.java

@ -1,12 +1,18 @@
package com.yihu.iot.controller.third;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.iot.aop.IntefaceLogRequired;
import com.yihu.iot.dao.device.IotDeviceUploadFileDao;
import com.yihu.iot.datainput.service.DataInputService;
import com.yihu.iot.datainput.service.DataSearchService;
import com.yihu.iot.datainput.util.ConstantUtils;
import com.yihu.iot.service.common.FileUploadService;
import com.yihu.jw.entity.iot.device.IotDeviceUploadFileDO;
import com.yihu.jw.exception.ApiException;
import com.yihu.jw.restmodel.iot.common.UploadVO;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.rm.iot.DataRequestMapping;
@ -20,9 +26,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;
@RestController
@RequestMapping(IotRequestMapping.Common.openThird)
@ -36,6 +46,16 @@ public class ThirdDataInputController {
    private DataInputService dataInputService;
    @Autowired
    private DataSearchService dataSearchService;
//    @Value("${testPattern.sign}")
    private String isClose = "1";
//    @Value("${testPattern.remote_inner_url}")
    private String remote_inner_url = "";
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private IotDeviceUploadFileDao iotDeviceUploadFileDao;
    /**************************社区数据删除**********************************************/
//    {
@ -130,6 +150,46 @@ public class ThirdDataInputController {
        }
    }
    @PostMapping(value = "upload_stream")
    @ApiOperation(value = "文件流上传文件", notes = "文件流上传文件")
    public Envelop uploadStream(@ApiParam(value = "文件", required = true)
                                @RequestParam(value = "file", required = true) MultipartFile file){
        try {
            String originalFileName = file.getOriginalFilename();
            originalFileName = URLDecoder.decode(originalFileName,"UTF-8");
            UploadVO uploadVO = new UploadVO();
            if (isClose.equalsIgnoreCase("1")){
                Map<String, Object> map = fileUploadService.uploadImg(file,originalFileName);
                uploadVO.setFullUri(map.get("accessory").toString());
                uploadVO.setFileName(file.getOriginalFilename());
            }else if(isClose.equals("2")){
                String originalFilename = file.getOriginalFilename();
                String[] fs = originalFilename.split("\\.");
                String type = fs[1];
                //内网上传
                String rs = fileUploadService.request(remote_inner_url,file,type);
                logger.info(rs);
                JSONObject json = JSON.parseObject(rs);
                uploadVO = objectMapper.readValue(json.getJSONObject("obj").toJSONString(),UploadVO.class);
            }else {
                // 得到文件的完整名称  xxx.txt
                String originalFilename = file.getOriginalFilename();
                InputStream inputStream = file.getInputStream();
                uploadVO = fileUploadService.uploadStream(inputStream,originalFilename,"");
            }
            IotDeviceUploadFileDO fileDO = new IotDeviceUploadFileDO();
            fileDO.setCreateTime(new Date());
            fileDO.setStatus("0");
            fileDO.setUrl(uploadVO.getFullUri());
            fileDO.setName(originalFileName);
            iotDeviceUploadFileDao.save(fileDO);
            return Envelop.getSuccess("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            return Envelop.getError("上传失败");
        }
    }
    /************************************************************************/
    @PostMapping(value = IotRequestMapping.ThirdOpen.registedevice)

+ 12 - 0
svr/svr-iot/src/main/java/com/yihu/iot/dao/device/IotDeviceUploadFileDao.java

@ -0,0 +1,12 @@
package com.yihu.iot.dao.device;
import com.yihu.jw.entity.iot.device.IotDeviceUploadFileDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
 * Created by yeshijie on 2025/9/9.
 */
public interface IotDeviceUploadFileDao extends PagingAndSortingRepository<IotDeviceUploadFileDO, Long>,
        JpaSpecificationExecutor<IotDeviceUploadFileDO> {
}

+ 403 - 0
svr/svr-iot/src/main/java/com/yihu/iot/service/common/FileUploadService.java

@ -0,0 +1,403 @@
package com.yihu.iot.service.common;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.fastdfs.FastDFSUtil;
import com.yihu.jw.exception.business.file_upload.*;
import com.yihu.jw.restmodel.iot.common.UploadVO;
import org.apache.commons.lang3.StringUtils;
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.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.*;
/**
 * 文件上传服务
 */
@Service
public class FileUploadService {
    private static Logger logger = LoggerFactory.getLogger(FileUploadService.class);
    @Autowired
    private FastDFSUtil fastDFSHelper;
    @Autowired
    private ObjectMapper objectMapper;
    /**
     * 文件流上传图片
     * @param inputStream
     * @param originalFileName
     * @return
     * @throws Exception
     */
    public UploadVO uploadImg(InputStream inputStream, String originalFileName,long fileSize,String fastdfs_file_url) throws Exception {
        // 得到文件的完整名称  xxx.txt
        if( null == inputStream){
            throw new FileInputStreamEmptyException("图片文件流不可为空!");
        }
        if(StringUtils.isBlank(originalFileName)){
            throw new FileNameEmptyException("图片文件名不可为空!");
        }
        if(StringUtils.isBlank(fastdfs_file_url)){
            throw new FastfdsFileURLEmptyException("fastdfs url不可为空!");
        }
        //得到文件类型
        String fileType = originalFileName.substring(originalFileName.lastIndexOf(".") + 1).toLowerCase();
        if(StringUtils.isBlank(fileType)||!"jpg,jpeg,png".contains(fileType)){
            throw new FileWrongFormatException("图片文件格式不正确,请上传jpg,jpeg,png等任一格式!");
        }
        if (!isFileFlag(fileType)){
            throw new FileWrongFormatException("不符合文件上传格式");
        }
        long max = 5*1024*1024;
        if(fileSize > max){
            throw new FileTooLargeSizeException("图片文件过大,请不要超过5M!");
        }
        String fileName = originalFileName.substring(0, originalFileName.lastIndexOf("."));
        //上传到fastdfs
        ObjectNode objectNode = fastDFSHelper.upload(inputStream, fileType, "");
        //解析返回的objectNode
        UploadVO uploadVO = new UploadVO();
        uploadVO.setFileName(fileName);
        uploadVO.setFileType(fileType);
        uploadVO.setFullUri(objectNode.get("fileId").toString().replaceAll("\"", ""));
        uploadVO.setFullUrl(fastdfs_file_url + objectNode.get("fileId").toString().replaceAll("\"", ""));
        return uploadVO;
    }
     /*
     * 文件流上传附件
     * @param file
     * @return
     * @throws Exception
     */
    public UploadVO uploadAttachment(InputStream inputStream, String originalFileName,long fileSize,String fastdfs_file_url) throws Exception{
        // 得到文件的完整名称  xxx.txt
        if( null == inputStream){
            throw new FileInputStreamEmptyException("附件文件流不可为空!");
        }
        if(StringUtils.isBlank(originalFileName)){
            throw new FileNameEmptyException("图片文件名不可为空!");
        }
        if(StringUtils.isBlank(fastdfs_file_url)){
            throw new FastfdsFileURLEmptyException("fastdfs url不可为空!");
        }
        //得到文件类型
        String fileType = originalFileName.substring(originalFileName.lastIndexOf(".") + 1).toLowerCase();
        if(StringUtils.isBlank(fileType)||!"doc、docx、pdf、xls、xlsx、jpg、jpeg、png".contains(fileType)){
            throw new FileWrongFormatException("附件文件格式不正确,请上传doc、docx、pdf、xls、xlsx、jpg、jpeg、png等任一格式!");
        }
        long max = 5*1024*1024;
        if(fileSize > max){
            throw new FileTooLargeSizeException("附件文件过大,请不要超过5M!");
        }
        String fileName = originalFileName.substring(0, originalFileName.lastIndexOf("."));
        //上传到fastdfs
        ObjectNode objectNode = fastDFSHelper.upload(inputStream, fileType, "");
        //解析返回的objectNode
        UploadVO uploadVO = new UploadVO();
        uploadVO.setFileName(fileName);
        uploadVO.setFileType(fileType);
        uploadVO.setFullUri(objectNode.get("fileId").toString().replaceAll("\"", ""));
        uploadVO.setFullUrl(fastdfs_file_url + objectNode.get("fileId").toString().replaceAll("\"", ""));
        return uploadVO;
    }
    /*
     * 文件流上传文件
     * @param file
     * @return
     * @throws Exception
     */
    public UploadVO uploadStream(InputStream inputStream, String originalFileName,String fastdfs_file_url) throws Exception{
        if( null == inputStream){
            throw new FileInputStreamEmptyException("文件内容不可为空!");
        }
        if(StringUtils.isBlank(originalFileName)){
            throw new FileNameEmptyException("文件名不可为空!");
        }
        if(StringUtils.isBlank(fastdfs_file_url)){
            throw new FastfdsFileURLEmptyException("fastdfs url不可为空!");
        }
        //得到文件类型sentitiveLog.txt
        String fileType = originalFileName.substring(originalFileName.lastIndexOf(".") + 1).toLowerCase();
        String fileName = originalFileName.substring(0, originalFileName.lastIndexOf("."));
        if (!isFileFlag(fileType)){
            throw new FileWrongFormatException("不符合文件上传格式");
        }
//        PDDocument document = PDDocument.load(inputStream);
//        if (containsXSS(document)){
//            throw new FileWrongFormatException("该PDF文件包含XSS攻击脚本!");
//        }
        //上传到fastdfs
        ObjectNode objectNode = fastDFSHelper.upload(inputStream, fileType, "");
        //解析返回的objectNode
        UploadVO uploadVO = new UploadVO();
        uploadVO.setFileName(fileName);
        uploadVO.setFileType(fileType);
        uploadVO.setFullUri(objectNode.get("fileId").toString().replaceAll("\"", ""));
        uploadVO.setFullUrl(fastdfs_file_url + objectNode.get("fileId").toString().replaceAll("\"", ""));
        return uploadVO;
    }
    public static void main(String[] args) {
        String s ="sentitiveLog.txt";
        System.out.println(s.substring(s.lastIndexOf(".") + 1).toLowerCase());
    }
    /**
     * i健康调用文件传输
     * @param multipartFile
     * @return
     * @throws Exception
     */
    public Map<String, Object> uploadImg(MultipartFile multipartFile,String originalFileName) throws Exception {
        Map<String, Object> map = new HashMap<>();
        long size = multipartFile.getSize();
        if(size<=0){
            map.put("uploadStatus",1);//size小于0
            map.put("accessoryUrl",null);//
            return map;
        }
        String fileName = originalFileName;
        String[] fs = fileName.split("\\.");
        String type = fs[1];
        logger.info("uploadImg type:"+type);
        //图片常见格式:bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp
       /* List img = new ArrayList(Arrays.asList("bmp", "jpg", "png", "tif", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd", "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "WMF", "webp"));
        if (!img.contains(type)) {
            map.put("uploadStatus",2);//文件类型不对
            map.put("accessoryUrl",null);//
            return map;
        }*/
       if (!isFileFlag(type)){
           throw new FileWrongFormatException("不符合文件上传格式");
       }
//        PDDocument document = PDDocument.load(multipartFile.getInputStream());
//        if (containsXSS(document)){
//            throw new FileWrongFormatException("该PDF文件包含XSS攻击脚本!");
//        }
        String response = request("https://ijk.xmyys.com.cn/wlyy/upload/chat", multipartFile, type);
        org.json.JSONObject rs = new org.json.JSONObject(response);
        Integer status = (Integer) rs.get("status");
        if (status == 200) {
            String url = rs.get("urls") + "";
            map.put("uploadStatus", 0);//文件类型正确
            map.put("accessory", url);//
            return map;
        }
        throw new Exception();
    }
    public String request(String remote_url, MultipartFile file, String type) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        String result = "";
        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
        if (!org.springframework.util.StringUtils.isEmpty(type)) {
            builder.addTextBody("type", type); //发送类型
        }
        logger.info("type===="+type);
        if (!isFileFlag(type)){
            throw new FileWrongFormatException("不符合文件上传格式");
        }
//        PDDocument document = PDDocument.load(file.getInputStream());
//        if (containsXSS(document)){
//            throw new FileWrongFormatException("该PDF文件包含XSS攻击脚本!");
//        }
        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"));
        }
        httpClient.close();
        return result;
    }
    /*
     * base64上传图片
     * @param jsonData,头像转化后的输入流
     * @return
     * @throws Exception
     */
    public UploadVO uploadImages(String jsonData,String fastdfs_file_url) throws Exception {
        if(StringUtils.isBlank(jsonData)){
            throw new FileInputStreamEmptyException("图片的base64文件文件流不可为空!");
        }
        if(StringUtils.isBlank(fastdfs_file_url)){
            throw new FastfdsFileURLEmptyException("fastdfs url不可为空!");
        }
        String date = URLDecoder.decode(jsonData,"UTF-8");
        String[] fileStreams = date.split(",");
        String is = URLDecoder.decode(fileStreams[1],"UTF-8").replace(" ","+");
        byte[] in = Base64.getDecoder().decode(is);
        String pictureName = fileStreams[0].substring(0,fileStreams[0].length()-1);
        String fileExtension = pictureName.substring(pictureName.lastIndexOf(".") + 1).toLowerCase();
        String description = null;
        if ((pictureName != null) && (pictureName.length() > 0)) {
            int dot = pictureName.lastIndexOf('.');
            if ((dot > -1) && (dot < (pictureName.length()))) {
                description = pictureName.substring(0, dot);
            }
        }
        InputStream inputStream = new ByteArrayInputStream(in);
        ObjectNode objectNode = fastDFSHelper.upload(inputStream, "png", "");
        String groupName = objectNode.get("groupName").toString();
        String remoteFileName = objectNode.get("remoteFileName").toString();
        //解析返回的objectNode
        UploadVO uploadVO = new UploadVO();
        uploadVO.setFileName(remoteFileName);
        uploadVO.setFileType(groupName);
        uploadVO.setFullUri(objectNode.get("fileId").toString().replaceAll("\"", ""));
        uploadVO.setFullUrl(fastdfs_file_url + objectNode.get("fileId").toString().replaceAll("\"", ""));
        //返回文件路径
        return uploadVO;
    }
    /***
     *
     * 将图片转换为Base64<br>
     * 将base64编码字符串解码成img图片
     * @param imgFile
     * @return
     */
    public static String getImgStr(String imgFile){
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {
            // 创建URL
            URL url = new URL(imgFile);
            byte[] by = new byte[1024];
            // 创建链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            InputStream is = conn.getInputStream();
            // 将内容放到内存中
            int len = -1;
            while ((len = is.read(by)) != -1) {
                data.write(by, 0, len);
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        return Base64.getEncoder().encodeToString(data.toByteArray());
    }
    public InputStream getInputStream(String downloadFilePath) throws Exception{
        InputStream inputStream = null;
        //从文件链接里获取文件流
        URL url = new URL(downloadFilePath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(180 * 1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        //得到输入流
        inputStream = conn.getInputStream();
        return inputStream;
    }
    public void deleteFile(String groupName,String fileName) throws  Exception{
        fastDFSHelper.delete(groupName,fileName);
    }
    /*
     * base64上传图片心脏
     * @param jsonData,头像转化后的输入流
     * @return
     * @throws Exception
     */
    public UploadVO uploadImagesBase64(String jsonData,String fastdfs_file_url) throws Exception {
        if(StringUtils.isBlank(jsonData)){
            throw new FileInputStreamEmptyException("图片的base64文件文件流不可为空!");
        }
        if(StringUtils.isBlank(fastdfs_file_url)){
            throw new FastfdsFileURLEmptyException("fastdfs url不可为空!");
        }
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bfile = decoder.decodeBuffer(jsonData);
        InputStream inputStream = new ByteArrayInputStream(bfile);
        ObjectNode objectNode = fastDFSHelper.upload(inputStream, "png", "");
        String groupName = objectNode.get("groupName").toString();
        String remoteFileName = objectNode.get("remoteFileName").toString();
        //解析返回的objectNode
        UploadVO uploadVO = new UploadVO();
        uploadVO.setFileName(remoteFileName);
        uploadVO.setFileType(groupName);
        uploadVO.setFullUri(objectNode.get("fileId").toString().replaceAll("\"", ""));
        uploadVO.setFullUrl(fastdfs_file_url + objectNode.get("fileId").toString().replaceAll("\"", ""));
        //返回文件路径
        return uploadVO;
    }
    public boolean isFileFlag(String type){
        type = type.toLowerCase();
        if (type.contains(".")){
            type = type.substring(type.lastIndexOf("."),type.length()-1);
        }
        logger.info(type);
        List img = new ArrayList(Arrays.asList("jpeg","bmp", "jpg", "png", "tif", "gif", "pcx", "tga", "exif", "fpx","psd",
                "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "WMF", "webp","xls","xlsx","text/plain","mp3","mp4","m4v","avi",
                "ogm","wmv","mpg","webm","ogv","mov","asx","mpeg","image/png","amr","doc","docx","pdf"));
        if (!img.contains(type)) {
            return false;
        }
        return true;
    }
}

+ 7 - 7
svr/svr-iot/src/main/resources/application.yml

@ -81,7 +81,7 @@ spring:
#    jest:
#      uris: http://172.26.0.202:9200
  wlyy:
    url: http://www.xmtyw.cn/wlyy/
    url: https://ijk.xmyys.com.cn/wlyy/
    appid: 915d0345-5b1d-11e6-8344-fa163e8aee61
    appsecret: 915d0345-5b1d-11e6-8344-fa163e8aee57
#hlw:
@ -120,7 +120,7 @@ spring:
#    jest:
#      uris: http://172.26.0.202:9200
  wlyy:
    url: http://www.xmtyw.cn/wlyy/
    url: https://ijk.xmyys.com.cn/wlyy/
    appid: 915d0345-5b1d-11e6-8344-fa163e8aee61
    appsecret: 915d0345-5b1d-11e6-8344-fa163e8aee57
#hlw:
@ -264,7 +264,7 @@ spring:
    jest:
      uris: http://59.61.92.90:9208, http://59.61.92.90:9210
  wlyy:
    url: http://www.xmtyw.cn/wlyy/
    url: https://ijk.xmyys.com.cn/wlyy/
    appid: 915d0345-5b1d-11e6-8344-fa163e8aee61
    appsecret: 915d0345-5b1d-11e6-8344-fa163e8aee57
#hlw:
@ -272,7 +272,7 @@ spring:
fast-dfs:
  tracker-server: 10.95.22.139:22122 #服务器地址
fastDFS:
  fastdfs_file_url: http://www.xmtyw.cn/
  fastdfs_file_url: https://ijk.xmyys.com.cn/
wechat:
  id: xm_zsyy_wx  # base库中,wx_wechat 的id字段
  flag: false #演示环境  true走Mysql数据库  false走Oracle
@ -289,7 +289,7 @@ spring:
    jest:
      uris: http://59.61.92.90:9208,http://59.61.92.90:9210
  wlyy:
    url: http://www.xmtyw.cn/wlyy/
    url: https://ijk.xmyys.com.cn/wlyy/
    appid: 915d0345-5b1d-11e6-8344-fa163e8aee61
    appsecret: 915d0345-5b1d-11e6-8344-fa163e8aee57
#hlw:
@ -297,7 +297,7 @@ spring:
fast-dfs:
  tracker-server: 10.95.22.139:22122 #服务器地址
fastDFS:
  fastdfs_file_url: http://www.xmtyw.cn/
  fastdfs_file_url: https://ijk.xmyys.com.cn/
wechat:
  id: xm_zsyy_wx  # base库中,wx_wechat 的id字段
  flag: false #演示环境  true走Mysql数据库  false走Oracle
@ -314,7 +314,7 @@ spring:
#    jest:
#      uris: http://10.90.32.3:20011,http://10.90.32.3:20012
  wlyy:
    url: https://www.xmtyw.cn/wlyytest/
    url: https://ijk.xmyys.com.cn/wlyytest/
    appid: 915d0345-5b1d-11e6-8344-fa163e8aee62
    appsecret: 915d0345-5b1d-11e6-8344-fa163e8aee62
#hlw: