泛微E9表单建模查询列表批量文件下载

前言

此代码仅为示例代码,本地做了简单测试可以实现,可能涉及sql注入的风险,使用过程中,请自行处理,做加密传输等操作。

1、系统检查

1.1、检查系统设置

是否配置了图片存放目录和文件存放目录,没有需要配置。

截图仅为参考,实际路径根据客户环境填写。

image-20230524160618942

2、配置

2.1、查询列表代码块

对应建模查询列表配置代码块

image-20230524160835903

插入代码如下:

修改modeid,详见【查看模块ID】

修改fieldName:改为对应建模主表的附件字段的数据库字段名

<script type="text/javascript">
/*
* 请在下面编写JS代码
*/
     function batchDownloadFile() {
        //模块ID  
        var modeid = "1010";
        //附件字段名 
        var fieldName = "xgfj";
        //用户勾选数据id 格式为1,2,3,4,5
        var ids = ModeList.getCheckedID();
        //非空检验
        if ("" == ids) {
            antd.message.error("请选择需要下载的数据");
        } else {
            //出于性能考虑,每次建议仅能下载10条数据
            if (ids.split(",").length > 50) {
                antd.message.error("最多勾选50条数据");
            } else {
                //点击进行批量下载
                var url = "/api/weavernorth/downloads/fileDownload?ids=" + ids + "&modeid=" + modeid + "&fieldName=" + fieldName;
                window.location.href = url;
            }
        }
    }
</script>

<style type="text/css">
/*
* 请在下方编辑CSS
*/

</style>

2.2、模块配置页面扩展

image-20230524163157571

image-20230524162508201

**链接目标地址信息:**javascript: batchDownloadFile();

image-20230524163223144

2.3、查询列表开启快捷按钮

image-20230524164249856

3、查看模块ID

找到查询列表对应模块,基础中的id就是模块ID

image-20230524161254134

4、后端代码实现

FiledownloadAction.java

package com.engine.web.weaver;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.engine.web.wn.util.FileUtilWn;
import com.engine.web.wn.util.FormmodeUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.system.SystemComInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.io.File;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import static com.engine.web.wn.util.FileUtilWn.getRealDoc;

/**
 * @Classname FiledownloadAction
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/5/24 10:39
 * @Created by 
 */

public class FiledownloadAction {
    private static final Logger log = LoggerFactory.getLogger(FiledownloadAction.class);

    @GET
    @Path("/fileDownload")
    @Produces({"application/octet-stream"})
    public Response fileDownload(@Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception {
        SystemComInfo systemComInfo = new SystemComInfo();
        String filesystem = systemComInfo.getFilesystem();
        String ids = request.getParameter("ids");
        String modeid = request.getParameter("modeid");
        String fieldName = request.getParameter("fieldName");
        Map<String, String> modeInfo = FormmodeUtil.getModeInfo(modeid);
        RecordSet rs = new RecordSet();
        String sql = "select " + fieldName + " from " + modeInfo.get("tablename") + " where id in (" + ids + ")";
        log.info("执行的sql=" + sql);
        rs.executeQuery(sql);
        String docids = "";

        String zipfilename = System.currentTimeMillis() + "";
        //系统文件还原到的路径 也是压缩文件夹的路径
        String zippath = filesystem + File.separator + "batchfile" + File.separator + zipfilename;
        log.info("beforepath=" + zippath);
        FileUtil.mkdir(zippath);
        while (rs.next()) {
            docids += Util.null2String(rs.getString(fieldName)) + ",";
        }
        if (docids.endsWith(",")) {
            docids = docids.substring(0, docids.lastIndexOf(","));
        }
        log.info("docids=" + docids);
        if (!"".equals(docids)) {
            List<String> list_imgid = FileUtilWn.getImageFileIdByDocId(docids);
            for (int i = 0; i < list_imgid.size(); i++) {
                getRealDoc(Util.getIntValue(list_imgid.get(i), 0), zippath);
            }
        }
        //文件夹不为空
        if (!FileUtil.isDirEmpty(new File(zippath))) {
            File zipFile = ZipUtil.zip(zippath);
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            zipfilename = zipFile.getName().replace(",", ",")
                    .replace("?", "?").replace("!", "!")
                    .replace("[", "【").replace("]", "】")
                    .replace("(", "(").replace(")", ")");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipfilename, "UTF-8"));
            FileUtilWn.writeBytes(zipFile, response.getOutputStream());
            //删除目标文件夹
            FileUtil.del(new File(zippath));
            //删除压缩包文件
            FileUtil.del(zipFile);
        }
        return Response.status(Response.Status.OK).build();
    }

    public static void main(String[] args) {
        String beforepath = "D://123";
        ZipUtil.zip(beforepath);
        FileUtil.del(new File(beforepath));
    }

}

FileUtilWn.java

package com.engine.weaverhb.util;

import weaver.conn.RecordSet;
import weaver.file.ImageFileManager;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname FileUtil
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/5/24 12:22
 * @Created by 
 */
public class FileUtilWn {
    private static Logger log = LoggerFactory.getLogger(FileUtilWn.class);

    public static List<String> getImageFileIdByDocId(String docids) {
        RecordSet rs = new RecordSet();
        List<String> list = new ArrayList<String>();
        String sql = "select imagefileid from DocImageFile dif  where docid in (" + docids + ")";
        log.info("sql: " + sql);
        rs.executeQuery(sql);
        while (rs.next()) {
            list.add(rs.getString("imagefileid"));
        }
        log.info("list:" + list);
        return list;
    }

    /**
     * 注意传入的为imagefielid ,得到的为磁盘中的全路径(路径+文件名称)
     *
     * @param imageFileId
     * @param unzipPath   解压到的路径
     * @return
     * @throws IOException
     */
    public static String getRealDoc(int imageFileId, String unzipPath) throws IOException {
        String realPath = "";
        ImageFileManager im = new ImageFileManager();
        im.getImageFileInfoById(imageFileId);
        InputStream in = im.getInputStreamById(imageFileId);
        if (in != null) {
            String imageFileName = im.getImageFileName().replaceAll("[\\s\\\\/:\\*\\?\\\"<>\\|]", "&");
            OutputStream os = null;
            try {
                os = new FileOutputStream(new File(unzipPath + File.separator + imageFileName));
                //文件拷贝
                byte flush[] = new byte[1024];
                int len = 0;
                while (0 <= (len = in.read(flush))) {
                    os.write(flush, 0, len);
                }
                os.flush();
                realPath = unzipPath + imageFileName;
                log.info("realPath=" + realPath);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (os != null && in != null) {
                    //关闭流的注意 先打开的后关
                    os.close();
                    in.close();
                }
            }
        } else {
            log.info("读取系统文件失败,InputStream为null");
        }
        return realPath;
    }

    /**
     * @param file 文件
     * @param os   输出流
     */
    public static void writeBytes(File file, OutputStream os) {
        FileInputStream fi = null;
        try {
            fi = new FileInputStream(file);
            byte[] b = new byte[1024];
            int length;
            while ((length = fi.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fi != null) {
                try {
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

FormmodeUtil.java

package com.engine.weaverhb.util;

import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname FormmodeUtil
 * @Description 表单建模工具类
 * @Version 1.0.0
 * @Date 2022/8/25 0025 14:11
 * @Created by 
 */
public class FormmodeUtil {
    private Logger log = LoggerFactory.getLogger(FormmodeUtil.class);

    public static Map<String, String> getModeInfo(String modeid) {
        Map<String, String> map = new HashMap<String, String>();
        RecordSet rs = new RecordSet();
        String sql = "select a.formid,b.id,b.tablename from  Modeinfo a ,workflow_bill b where a.formid =b.id and a.id=?";
        rs.executeQuery(sql, modeid);
        if (rs.next()) {
            map.put("formid", Util.null2String(rs.getString("formid")));
            map.put("billid", Util.null2String(rs.getString("id")));
            map.put("tablename", Util.null2String(rs.getString("tablename")));
        }
        return map;
    }


}
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值