Commit c6adb2a7 by 宋祥

1.批量下载文件

parent 43c7eb17
......@@ -10,30 +10,33 @@ import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@EnableScheduling
@SpringBootApplication(scanBasePackages = "com.baosight")
@ServletComponentScan("com.baosight.iplat4j.core.web.servlet")
@ImportResource(locations = {"classpath*:spring/framework/platApplicationContext*.xml","classpath*:spring/framework/applicationContext*.xml"})
@ImportResource(locations = {"classpath*:spring/framework/platApplicationContext*.xml",
"classpath*:spring/framework/applicationContext*.xml"})
@EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class})
public class HggpApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(HggpApplication.class);
app.run(args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(HggpApplication.class);
}
@Bean("multipartResolver")
public CommonsMultipartResolver multipartResolver(){
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
resolver.setMaxInMemorySize(10000);
return resolver;
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(HggpApplication.class);
app.run(args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(HggpApplication.class);
}
@Bean("multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
resolver.setMaxInMemorySize(10000);
return resolver;
}
}
......@@ -34,11 +34,16 @@ public class OSConstant {
/**
* 程序运行目录
*/
public final static String DOWN_DIR = USER_DIR + SEPARATOR + "download";
public final static String DOWN_DIR = USER_DIR + SEPARATOR + "file" + SEPARATOR + "download";
/**
* 二维码目录
*/
public final static String QRCODE_DIR = USER_DIR + SEPARATOR + "qrcode";
public final static String QRCODE_DIR = USER_DIR + SEPARATOR + "file" + SEPARATOR + "qrcode";
/**
* 压缩包目录
*/
public final static String ZIP_DIR = USER_DIR + SEPARATOR + "file" + SEPARATOR + "zip";
}
......@@ -22,6 +22,11 @@ public class S3Constant {
public static String ENDPOINT = PlatApplicationContext.getProperty("iplat4j.admin.objectStorage.s3.endpoint");
/**
* resource
*/
public static String RESOURCE = PlatApplicationContext.getProperty("iplat4j.admin.objectStorage.s3.resource");
/**
* bucket名称
*/
public static String BUCKET_NAME = PlatApplicationContext.getProperty("iplat4j.admin.objectStorage.s3.bucket");
......
package com.baosight.hggp.core.utils;
import com.baosight.hggp.core.constant.CommonConstant;
import com.baosight.hggp.core.constant.OSConstant;
import com.baosight.hggp.core.constant.S3Constant;
import com.baosight.hggp.core.tools.Iplat4jTools;
import com.baosight.hggp.util.DateUtils;
import com.baosight.hggp.util.FileUtils;
import com.baosight.hggp.util.MapUtils;
import com.baosight.hggp.util.StringUtils;
......@@ -10,6 +13,7 @@ import com.baosight.iplat4j.core.exception.PlatException;
import com.baosight.iplat4j.core.ioc.spring.PlatApplicationContext;
import com.baosight.iplat4j.eu.dm.util.PlatFileUploader;
import java.util.List;
import java.util.Map;
/**
......@@ -73,4 +77,42 @@ public class Iplat4jUtils {
return document;
}
/**
* 压缩文件
*
* @param docIds
* @param zipName
* @return
*/
public static String compressFile(List<String> docIds, String zipName) throws Exception {
String zipFolderPath = OSConstant.ZIP_DIR + OSConstant.SEPARATOR + DateUtils.shortDate()
+ OSConstant.SEPARATOR + zipName;
FileUtils.createDirs(zipFolderPath);
String zipFilePath = zipFolderPath + ".zip";
if (CommonConstant.FileLocation.S3.equalsIgnoreCase(S3Constant.FILE_LOCATION)) {
for (String docId : docIds) {
Teudm05 dbDm05 = S3Utils.buildUrl(docId);
// 文件路径:压缩包路径+文件名称
String localFilePath = zipFolderPath + OSConstant.SEPARATOR + dbDm05.getResCname();
FileUtils.downloadUrlFile(dbDm05.getUrl(), localFilePath);
}
} else {
for (String docId : docIds) {
Map dbDm02 = Iplat4jUtils.getDoc02ById(docId);
String returnUrl = MapUtils.getString(dbDm02, "url");
String docName = MapUtils.getString(dbDm02, "docName");
// 文件路径:压缩包路径+文件名称
String localFilePath = zipFolderPath + OSConstant.SEPARATOR + docName;
FileUtils.fileCopy(returnUrl, localFilePath);
}
}
// 开始压缩
FileUtils.zip(zipFolderPath, zipFilePath);
// 压缩完删除本地文件
FileUtils.deleteFiles(zipFolderPath);
// 替换成HTTP地址
return S3Constant.RESOURCE + zipFilePath.replace(OSConstant.USER_DIR, "")
.replace(OSConstant.SEPARATOR, "/");
}
}
......@@ -17,6 +17,7 @@ import com.baosight.iplat4j.core.service.impl.ServiceEPBase;
import com.baosight.iplat4j.core.service.soa.XServiceManager;
import org.apache.commons.collections.CollectionUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -161,7 +162,7 @@ public class ServiceHGCW999 extends ServiceEPBase {
* @param docId 文件ID
*/
@OperationLogAnnotation(operModul = "附件清单",operType = "删除",operDesc = "删除附件文件")
public void delectDoc(String docId){
public void delectDoc(String docId) throws IOException {
Map<String,Object> map = new HashMap<>();
map.put("docId",docId);
List<HGDS002> list = this.dao.query(HGDS002.QUERY,map);
......
......@@ -14,6 +14,7 @@ import com.baosight.iplat4j.core.exception.PlatException;
import com.baosight.iplat4j.core.service.impl.ServiceEPBase;
import com.baosight.iplat4j.core.service.soa.XServiceManager;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -133,7 +134,7 @@ public class ServiceHGPZ002A extends ServiceEPBase {
* @param docId 文件ID
*/
@OperationLogAnnotation(operModul = "附件清单",operType = "删除",operDesc = "删除附件文件")
public void delectDoc(String docId){
public void delectDoc(String docId) throws IOException {
Map<String,Object> map = new HashMap<>();
map.put("docId",docId);
List<HGDS002> list = this.dao.query(HGDS002.QUERY,map);
......
......@@ -17,6 +17,7 @@ import com.baosight.iplat4j.core.service.impl.ServiceEPBase;
import com.baosight.iplat4j.core.service.soa.XServiceManager;
import com.baosight.iplat4j.core.web.threadlocal.UserSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -127,7 +128,7 @@ public class ServiceHGPZ009A extends ServiceEPBase {
* @param docId 文件ID
*/
@OperationLogAnnotation(operModul = "banner清单",operType = "删除",operDesc = "删除附件文件")
public void delectDoc(String docId){
public void delectDoc(String docId) throws IOException {
Map<String,Object> map = new HashMap<>();
map.put("docId",docId);
List<HGDS002> list = this.dao.query(HGDS002.QUERY,map);
......
......@@ -16,6 +16,7 @@ import com.baosight.iplat4j.core.exception.PlatException;
import com.baosight.iplat4j.core.service.impl.ServiceEPBase;
import com.baosight.iplat4j.core.service.soa.XServiceManager;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -96,7 +97,7 @@ public class ServiceHGSB099 extends ServiceEPBase {
* 删除文件
* @param docId 文件ID
*/
public void delectDoc(String docId){
public void delectDoc(String docId) throws IOException {
Map<String,Object> map = new HashMap<>();
map.put("docId",docId);
List<HGDS002> list = this.dao.query(HGDS002.QUERY,map);
......
......@@ -14,6 +14,7 @@ import com.baosight.iplat4j.core.exception.PlatException;
import com.baosight.iplat4j.core.service.impl.ServiceEPBase;
import com.baosight.iplat4j.core.service.soa.XServiceManager;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -115,7 +116,7 @@ public class ServiceHGXT099 extends ServiceEPBase {
* @param docId 文件ID
*/
@OperationLogAnnotation(operModul = "附件清单",operType = "删除",operDesc = "删除附件文件")
public void delectDoc(String docId){
public void delectDoc(String docId) throws IOException {
Map<String,Object> map = new HashMap<>();
map.put("docId",docId);
List<HGDS002> list = this.dao.query(HGDS002.QUERY,map);
......
package com.baosight.hggp.job;
import com.baosight.hggp.core.constant.OSConstant;
import com.baosight.hggp.util.DateUtil;
import com.baosight.hggp.util.DateUtils;
import com.baosight.hggp.util.FileUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.time.LocalDate;
import lombok.extern.slf4j.Slf4j;
/**
* @author:songx
* @date:2024/10/11,12:26
*/
@Slf4j
@Component
public class ClearZipJob {
/**
* 清理ZIP文件
*/
@Scheduled(cron = "0 15 3 * * ?")
public void clearZipFile() {
try {
File zipFolder = new File(OSConstant.ZIP_DIR);
File[] zipFolderFiles = zipFolder.listFiles();
if (zipFolderFiles.length == 0) {
log.warn("清理ZIP文件:无有效的文件");
}
for (File zipFolderFile : zipFolderFiles) {
String folderName = zipFolderFile.getName();
// 当天以前的文件夹全部删除
if (LocalDate.parse(folderName, DateUtils.SHORT_DATE).isBefore(LocalDate.now())) {
FileUtils.deleteFiles(zipFolderFile);
}
}
log.info("清理ZIP文件成功!!!");
} catch (Exception e) {
log.error("清理ZIP文件失败:{}", e.getMessage(), e);
}
}
}
......@@ -2,6 +2,8 @@ package com.baosight.hggp.util;
import com.baosight.hggp.core.constant.OSConstant;
import org.apache.poi.util.IOUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
......@@ -18,9 +20,12 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
......@@ -30,6 +35,8 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import lombok.extern.slf4j.Slf4j;
......@@ -187,6 +194,40 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
/**
* 删除目录及文件
*
* @param path
* @return
*/
public static boolean deleteFiles(String path) {
return deleteFiles(Paths.get(path));
}
/**
* 删除目录及文件
*
* @param path
* @return
*/
public static boolean deleteFiles(Path path) {
try {
if (Files.isDirectory(path)) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path)) {
for (Path entry : directoryStream) {
deleteFiles(entry);
}
} catch (Exception e) {
log.error("删除文件失败,读取目录子文件失败:{}", e.getMessage(), e);
}
}
} catch (Exception e) {
log.error("删除文件失败:{}", e.getMessage(), e);
return false;
}
return deleteFile(path);
}
/**
* 删除目录及文件
*
* @param file
* @return
*/
......@@ -214,7 +255,25 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
public static boolean deleteFile(String path) {
Objects.requireNonNull(path);
// 路径为文件且不为空则进行删除
return deleteFile(new File(path));
return deleteFile(Paths.get(path));
}
/**
* 删除单个文件或目录
*
* @param path 被删除的文件
* @return
*/
public static boolean deleteFile(Path path) {
Objects.requireNonNull(path);
// 路径为文件且不为空则进行删除
try {
Files.delete(path);
} catch (Exception e) {
log.error("删除文件失败:{}", e.getMessage(), e);
return false;
}
return true;
}
/**
......@@ -266,43 +325,20 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
* @param targetPath
* @throws IOException
*/
public static void fileCope(String filePath, String targetPath) throws IOException {
// 获得流
FileInputStream fileInputStream = null;
//新文件输出流
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
File targetFile = new File(targetPath);
//获取父目录
File parentFile = targetFile.getParentFile();
//判断是否存在
if (!parentFile.exists()) {
// 创建父目录文件夹
parentFile.mkdirs();
}
//判断文件是否存在
if (!targetFile.exists()) {
//创建文件
targetFile.createNewFile();
}
fileOutputStream = new FileOutputStream(targetFile);
public static void fileCopy(String filePath, String targetPath) throws IOException {
File targetFile = new File(targetPath);
creatFiles(targetPath);
try (FileInputStream fis = new FileInputStream(filePath);
FileOutputStream fos = new FileOutputStream(targetFile)) {
byte[] buffer = new byte[1024];
int len;
//将文件流信息读取文件缓存区,如果读取结果不为-1就代表文件没有读取完毕,反之已经读取完毕
while ((len = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
fileOutputStream.flush();
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
fos.flush();
}
} catch (Exception e) {
throw e;
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}
......@@ -464,16 +500,6 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
}
/**
* InputStream
*
* @param url
*/
public static InputStream downloadStreamFromUrl(String url) throws IOException {
URL httpUrl = new URL(url);
return httpUrl.openStream();
}
/**
* 文件下载
*
* @param file
......@@ -491,18 +517,48 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
}
/**
* 下载文件到本地
*
* @param httpUrl
* @param localFilePath
*/
public static void downloadUrlFile(String httpUrl, String localFilePath) throws IOException {
// 创建文件
FileUtils.creatFiles(localFilePath);
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try (InputStream is = connection.getInputStream();
OutputStream os = new FileOutputStream(localFilePath)) {
// 这里也很关键每次读取的大小为5M,不一次性读取完
byte[] buffer = new byte[1024 * 1024 * 1];
int len = 0;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} catch (IOException e) {
throw e;
} finally {
connection.disconnect();
}
}
/**
* 下载文件到前端
*
* @param url
* @param httpUrl
* @param fileName
* @param response
*/
public static void downloadUrlFile(String url, String fileName, boolean isPreview, HttpServletResponse response)
public static void downloadUrlFile(String httpUrl, String fileName, boolean isPreview, HttpServletResponse response)
throws IOException {
try (InputStream is = downloadStreamFromUrl(url);) {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try (InputStream is = connection.getInputStream()) {
downloadFile(is, fileName, isPreview, response);
} catch (Exception e) {
throw e;
} finally {
connection.disconnect();
}
}
......@@ -572,4 +628,41 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
}
}
/**
* 压缩文件夹.
*
* @param sourceFilePath 源文件夹路径
* @param zipFilePath 压缩包路径
*/
public static void zip(String sourceFilePath, String zipFilePath) throws IOException {
File zipFile = new File(zipFilePath);
if (zipFile.exists()) {
zipFile.delete();
}
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
File[] sourceFiles = new File(sourceFilePath).listFiles();
if (null == sourceFiles || sourceFiles.length < 1) {
log.info("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
}
byte[] buffs = new byte[1024 * 10];
for (File sourceFile : sourceFiles) {
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFile.getName());
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
try (FileInputStream fis = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fis, 1024 * 10)) {
int read = 0;
while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
zos.write(buffs, 0, read);
}
} catch (IOException e) {
throw e;
}
}
} catch (IOException e) {
throw e;
}
}
}
package com.baosight.hggp.util;
import com.baosight.iplat4j.core.data.DaoEPBase;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import org.apache.commons.collections.CollectionUtils;
import java.util.Collection;
......@@ -109,6 +111,22 @@ public class ObjectUtils extends org.apache.commons.lang.ObjectUtils {
/**
* 从集合MAP中取KEY
*
* @param inInfo
* @param keyName
* @return
*/
public static <T> List<T> listKey(EiInfo inInfo, String keyName) {
List<Map> items = inInfo.getBlock(EiConstant.resultBlock).getRows();
if (CollectionUtils.isEmpty(items)) {
return null;
}
return items.stream().map(item -> (T) item.get(keyName)).filter(ObjectUtils::isNotBlank).distinct()
.collect(Collectors.toList());
}
/**
* 从集合MAP中取KEY
*
* @param items
* @param keyName
* @return
......
......@@ -469,9 +469,9 @@ $(function () {
readonly: true,
template: function (item) {
let template = '<a style="cursor: pointer;display: inline-flex;justify-content: center;margin:auto 5px" '
+ 'href="' + downloadHref(item.docId) + '">附件下载</a>';
+ 'href="' + downloadHref(item.docId) + '">下载</a>';
template += '<a style="cursor: pointer;display: inline-flex;justify-content: center;margin:auto 5px" '
+ 'onclick="changeFile(\'' + item.docId + '\',\''+item.bizId+'\')" target="_blank">附件变更</a>';
+ 'onclick="changeFile(\'' + item.docId + '\',\''+item.bizId+'\')" target="_blank">变更</a>';
return template;
}
}, {
......@@ -498,14 +498,16 @@ $(function () {
}
};
downKeyUp();
downKeyUp();
// 查询
$("#BTN_DELETE").on("click", deleteFunc);
$("#SAVE1").on("click", saveFunc);
$("#SAVE2").on("click", saveProtFunc);
//确认发布
$("#confirmRelease").on("click", updateRelease);
// 查询
$("#BTN_DELETE").on("click", deleteFunc);
$("#SAVE1").on("click", saveFunc);
$("#SAVE2").on("click", saveProtFunc);
//确认发布
$("#confirmRelease").on("click", updateRelease);
// 批量下载
$("#BATCH_DOWNLOAD").on("click", batchDownload);
});
let query = function () {
......@@ -994,12 +996,30 @@ let isProjectManager = function (parentId) {
default:
$("#RELEASE").attr("disabled", true);
$("#UPLOAD_FILE").attr("disabled", true);
$("#COPY_FILE").attr("disabled", true);
$("#PREVIEW").attr("disabled", true);
$("#COPY_FILE").attr("disabled", true);
$("#PREVIEW").attr("disabled", true);
}
}
}
}, {
async: false
async: false
})
}
/**
* 批量下载
*/
let batchDownload = function () {
var rows = resultGrid.getCheckedRows();
if (rows.length == 0) {
message("请先勾选要下载的数据!");
return;
}
JSUtils.submitGridsData("result", "HGWD001", "batchDownload", false,
function (res) {
if (res.status > -1) {
window.open(res.extAttr.downloadUrl, '_blank');
}
}
);
}
......@@ -71,7 +71,7 @@
serviceName="HGWD099" queryMethod="query" deleteMethod="delete">
<EF:EFColumn ename="id" cname="ID" hidden="true"/>
<EF:EFColumn ename="docId" cname="文件ID" enable="false" width="180" hidden="true"/>
<EF:EFColumn ename="operator" cname="操作" enable="false" width="140" align="center" sort="false"/>
<EF:EFColumn ename="operator" cname="操作" enable="false" width="100" align="center" sort="false"/>
<EF:EFColumn ename="docName" cname="文件名称" enable="false" width="180"/>
<EF:EFColumn ename="docType" cname="文件类型" enable="false" width="110" align="center"/>
<EF:EFColumn ename="docVersion" cname="版本号" enable="false" width="90" align="center" sort="true"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment