前言
此代码仅为示例代码,本地做了简单测试可以实现,可能涉及sql注入的风险,使用过程中,请自行处理,做加密传输等操作。
1、系统检查
1.1、检查系统设置
是否配置了图片存放目录和文件存放目录,没有需要配置。
截图仅为参考,实际路径根据客户环境填写。
2、配置
2.1、查询列表代码块
对应建模查询列表配置代码块
插入代码如下:
修改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、模块配置页面扩展
**链接目标地址信息:**javascript: batchDownloadFile();
2.3、查询列表开启快捷按钮
3、查看模块ID
找到查询列表对应模块,基础中的id就是模块ID
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;
}
}