Fanwei E9 Form Modeling Query List Batch File Download

foreword

This code is just a sample code, and it can be realized by a simple local test, which may involve the risk of SQL injection. During use, please handle it by yourself and perform encrypted transmission and other operations.

1. System check

1.1. Check system settings

Whether the image storage directory and the file storage directory are configured, there is no need to configure.

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

image-20230524160618942

2. Configuration

2.1, query list code block

Corresponding modeling query list configuration code block

image-20230524160835903

The insertion code is as follows:

Modify the modeid , see [View Module ID] for details

Modify fieldName : Change to the database field name corresponding to the attachment field of the main modeling table

<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, module configuration page extension

image-20230524163157571

image-20230524162508201

**Link target address information:**javascript: batchDownloadFile();

image-20230524163223144

2.3, query list open shortcut button

image-20230524164249856

3. Check the module ID

Find the module corresponding to the query list, and the id in the foundation is the module ID

image-20230524161254134

4. Back-end code implementation

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;
    }


}

Guess you like

Origin blog.csdn.net/u010048119/article/details/130852004