This commit is contained in:
ls
2024-10-21 13:37:48 +08:00
parent 90054a0cf0
commit d5cfcd286c
5 changed files with 412 additions and 387 deletions

View File

@@ -72,255 +72,265 @@ public class ImportRecordServiceImpl extends ServiceImpl<ImportRecordMapper, Imp
/**
* https://esarad.esa.int/
*/
@Override
public void esaradCrawler() {
try {
String crawlerRunning = String.valueOf(redisUtil.get(esaradKey));
if (StringUtils.equals(crawlerRunning, "T")) {
throw new RuntimeException("爬虫任务执行中");
}
redisUtil.set(esaradKey, "T", 24 * 60 * 60);
List<ImportRecord> tableData = new ArrayList<>();
Document doc = Jsoup.connect("https://esarad.esa.int").get();
Element table = doc.getElementById("dtReports");
if (Objects.isNull(table)) {
redisUtil.del(esaradKey);
throw new RuntimeException("爬虫获取数据失败");
}
// Get the tbody element within the table
Element tbody = table.select("tbody").first(); // Select the first tbody element
if (tbody == null) {
redisUtil.del(esaradKey);
throw new RuntimeException("爬虫获取数据失败");
}
// Create a list to store the row maps
// Extract headers from the first row
Elements headers = table.select("thead").first().select("tr").first().select("th");
List<String> headerNames = new ArrayList<>();
for (Element header : headers) {
headerNames.add(header.text());
}
// Select all rows in the tbody
Elements rows = tbody.select("tr");
for (int j = 0; j < rows.size(); j++) {
if (j > 10) {
break;
}
Element row = rows.get(j);
// Select all cells in the row
ImportRecord importRecord = new ImportRecord();
Elements cells = row.select("td");
if (cells.size() == headerNames.size()) { // Ensure the number of cells matches the number of headers
for (int i = 0; i < cells.size(); i++) {
String header = headerNames.get(i);
String value = cells.get(i).text();
switch (header) {
case "Radiation Test Method":
importRecord.setTestMethod(value);
break;
case "EPPL Familiy":
importRecord.setCategory(value);
break;
case "EPPL Group":
importRecord.setSubCategory(value);
break;
case "DUT Manufacturer":
importRecord.setManufacturer(value);
break;
case "Function":
importRecord.setFunctionType(value);
break;
case "Report Date":
importRecord.setReportDate(value);
break;
case "Report Source":
importRecord.setReportSource(value);
break;
case "Technology":
importRecord.setTechnology(value);
break;
case "Id":
importRecord.setReportId(value);
break;
case "DUT part type":
importRecord.setCode(value);
break;
case "Radiation Test Type":
importRecord.setRadiationTestType(value);
break;
}
}
// Add the map to the list
tableData.add(importRecord);
}
}
// Print the list of maps
for (ImportRecord rowMap : tableData) {
rowMap.setFileUrl("https://esarad.esa.int/?id=" + rowMap.getReportId() + "&handler=DownloadDb");
}
saveEsaradFiles(tableData, esaradKey);
} catch (Exception e) {
redisUtil.del(esaradKey);
throw new RuntimeException(e);
}
}
// @Override
// public void esaradCrawler() {
// try {
// String crawlerRunning = String.valueOf(redisUtil.get(esaradKey));
// if (StringUtils.equals(crawlerRunning, "T")) {
// throw new RuntimeException("爬虫任务执行中");
// }
// redisUtil.set(esaradKey, "T", 24 * 60 * 60);
//
// List<ImportRecord> tableData = new ArrayList<>();
//
// Document doc = Jsoup.connect("https://esarad.esa.int").get();
// Element table = doc.getElementById("dtReports");
// if (Objects.isNull(table)) {
// redisUtil.del(esaradKey);
// throw new RuntimeException("爬虫获取数据失败");
// }
// // Get the tbody element within the table
// Element tbody = table.select("tbody").first(); // Select the first tbody element
//
// if (tbody == null) {
// redisUtil.del(esaradKey);
// throw new RuntimeException("爬虫获取数据失败");
// }
// // Create a list to store the row maps
//
// // Extract headers from the first row
// Elements headers = table.select("thead").first().select("tr").first().select("th");
// List<String> headerNames = new ArrayList<>();
// for (Element header : headers) {
// headerNames.add(header.text());
// }
//
// // Select all rows in the tbody
// Elements rows = tbody.select("tr");
//
// for (int j = 0; j < rows.size(); j++) {
// if (j > 10) {
// break;
// }
// Element row = rows.get(j);
//
// // Select all cells in the row
// ImportRecord importRecord = new ImportRecord();
//
// Elements cells = row.select("td");
//
// if (cells.size() == headerNames.size()) { // Ensure the number of cells matches the number of headers
//
// for (int i = 0; i < cells.size(); i++) {
//
// String header = headerNames.get(i);
// String value = cells.get(i).text();
// switch (header) {
// case "Radiation Test Method":
// importRecord.setTestMethod(value);
// break;
// case "EPPL Familiy":
// importRecord.setCategory(value);
// break;
// case "EPPL Group":
// importRecord.setSubCategory(value);
// break;
// case "DUT Manufacturer":
// importRecord.setManufacturer(value);
// break;
// case "Function":
// importRecord.setFunctionType(value);
// break;
// case "Report Date":
// importRecord.setReportDate(value);
// break;
// case "Report Source":
// importRecord.setReportSource(value);
// break;
// case "Technology":
// importRecord.setTechnology(value);
// break;
// case "Id":
// importRecord.setReportId(value);
// break;
// case "DUT part type":
// importRecord.setCode(value);
// break;
// case "Radiation Test Type":
// importRecord.setRadiationTestType(value);
// break;
// }
//
// }
//
// // Add the map to the list
// tableData.add(importRecord);
// }
// }
//
// // Print the list of maps
// for (ImportRecord rowMap : tableData) {
// rowMap.setFileUrl("https://esarad.esa.int/?id=" + rowMap.getReportId() + "&handler=DownloadDb");
// }
// saveEsaradFiles(tableData, esaradKey);
//
// } catch (Exception e) {
// redisUtil.del(esaradKey);
// throw new RuntimeException(e);
// }
// }
/**
* https://radhome.gsfc.nasa.gov/radhome/RadDataBase/RadDataBase.html
*/
// @Override
// public void radhomeCrawler() {
// try {
// String crawlerRunning = String.valueOf(redisUtil.get(radhomeKey));
// if (StringUtils.equals(crawlerRunning, "T")) {
// throw new RuntimeException("爬虫任务执行中");
// }
// redisUtil.set(radhomeKey, "T", 24 * 60 * 60);
//
// OkHttpClient client = new OkHttpClient();
// String url = "https://radhome.gsfc.nasa.gov/radhome/dev/parts.cfc?method=getParts";
// FormBody formBody = new FormBody.Builder().add("_search", "false").add("nd", System.currentTimeMillis() + "").add("rows", "10").add("page", "1").add("sidx", "partnumber").add("sord", "asc").build();
//
// Request request = new Request.Builder().url(url).post(formBody).build();
//
// client.newCall(request).enqueue(new Callback() {
// @Override
// public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
// String jsonResponse = response.body().string();
// JSONObject jsonObject = JSON.parseObject(jsonResponse);
// Long total = jsonObject.getLong("RECORDS");
// JSONArray list = jsonObject.getJSONArray("ROWS");
// System.out.println("total count " + total);
// System.out.println("total list " + list.get(0));
// List<ImportRecord> tableData = new ArrayList<>();
//
// for (int i = 0; i < list.size(); i++) {
//
// if (i > 10) {
// break;
// }
// JSONArray row = (JSONArray) list.get(i);
// String fileNames = String.valueOf(row.get(4));
// ImportRecord map = new ImportRecord();
// String fileUrls = fixFileNames(fileNames);
// map.setFileUrl(fileUrls);
//
// map.setCode(String.valueOf(row.get(0)));
// map.setFunctionType(String.valueOf(row.get(1)));
// map.setManufacturer(String.valueOf(row.get(2)));
// map.setReportDate(String.valueOf(row.get(3)));
// map.setTestMethod(String.valueOf(row.get(5)));
// map.setCategory(String.valueOf(row.get(6)));
// map.setReportId(map.getCode().replaceAll(" ", ""));
// tableData.add(map);
// }
// saveRadhomeFiles(tableData, radhomeKey);
// }
//
// @Override
// public void onFailure(@NotNull Call call, @NotNull IOException e) {
// e.printStackTrace();
// redisUtil.del(radhomeKey);
// }
// });
// } catch (Exception e) {
// redisUtil.del(radhomeKey);
// throw new RuntimeException(e);
// }
// }
// private String fixFileNames(String fileNames) {
// String[] split = StringUtils.split(fileNames, ";");
// List<String> result = new ArrayList<>();
// for (String s : split) {
// if (!StringUtils.startsWith(s, "http")) {
// result.add("https://radhome.gsfc.nasa.gov/radhome/papers/" + s);
// } else {
// result.add(s);
// }
// }
// return StringUtils.join(result, ";");
// }
//
// private void saveRadhomeFiles(List<ImportRecord> fileList, String type) {
// ThreadUtil.execute(() -> {
// try {
// for (ImportRecord record : fileList) {
// String fileUploadResult = "";
// String fileUrl = record.getFileUrl();
// if (fileUrl.contains(";")) {
// String[] split = fileUrl.split(";");
// List<String> result = new ArrayList<>();
// for (String s : split) {
// byte[] fileBytes = HttpUtil.downloadBytes(s);
// InputStream inputStream = new ByteArrayInputStream(fileBytes);
// result.add(MinioUtil.upload(inputStream, "radhome/" + s.substring(s.lastIndexOf("/") + 1)));
// fileUploadResult = StringUtils.join(result, ";");
// }
// } else {
// byte[] fileBytes = HttpUtil.downloadBytes(fileUrl);
// InputStream inputStream = new ByteArrayInputStream(fileBytes);
// fileUploadResult = MinioUtil.upload(inputStream, "radhome/" + fileUrl.substring(fileUrl.lastIndexOf("/") + 1));
// }
// System.out.println(fileUploadResult);
// if (StringUtils.isNotBlank(fileUploadResult)) {
// record.setFileUrl(fileUploadResult);
// save(record);
// }
// }
// redisUtil.del(type);
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
// }
//
// private void saveEsaradFiles(List<ImportRecord> fileList, String type) {
// ThreadUtil.execute(() -> {
// try {
// for (ImportRecord record : fileList) {
// ImportRecord dbData = getOne(Wrappers.<ImportRecord>lambdaQuery().eq(ImportRecord::getReportSource, record.getReportId()));
// if (Objects.nonNull(dbData)) {
// continue;
// }
// String resultStr = "";
//
// String dest = FileUtil.getTmpDirPath() + "esarad-" + record.getReportId() + "/";
// FileUtil.mkdir(dest);
// long fileSize = HttpUtil.downloadFile(record.getFileUrl(), dest);
//
// if (fileSize > 0) {
// List<File> files = FileUtil.loopFiles(dest);
// for (File file : files) {
// resultStr = MinioUtil.upload(IoUtil.toStream(file), "esarad/" + record.getReportId() + "-" + URLUtil.decode(file.getName(), Charset.defaultCharset()) );
// }
// }
//
// System.out.println(resultStr);
// if (StringUtils.isNotBlank(resultStr)) {
// record.setFileUrl(resultStr);
// save(record);
// }
// }
// redisUtil.del(type);
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
// }
@Override
public void esaradCrawler() {
}
@Override
public void radhomeCrawler() {
try {
String crawlerRunning = String.valueOf(redisUtil.get(radhomeKey));
if (StringUtils.equals(crawlerRunning, "T")) {
throw new RuntimeException("爬虫任务执行中");
}
redisUtil.set(radhomeKey, "T", 24 * 60 * 60);
OkHttpClient client = new OkHttpClient();
String url = "https://radhome.gsfc.nasa.gov/radhome/dev/parts.cfc?method=getParts";
FormBody formBody = new FormBody.Builder().add("_search", "false").add("nd", System.currentTimeMillis() + "").add("rows", "10").add("page", "1").add("sidx", "partnumber").add("sord", "asc").build();
Request request = new Request.Builder().url(url).post(formBody).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
String jsonResponse = response.body().string();
JSONObject jsonObject = JSON.parseObject(jsonResponse);
Long total = jsonObject.getLong("RECORDS");
JSONArray list = jsonObject.getJSONArray("ROWS");
System.out.println("total count " + total);
System.out.println("total list " + list.get(0));
List<ImportRecord> tableData = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (i > 10) {
break;
}
JSONArray row = (JSONArray) list.get(i);
String fileNames = String.valueOf(row.get(4));
ImportRecord map = new ImportRecord();
String fileUrls = fixFileNames(fileNames);
map.setFileUrl(fileUrls);
map.setCode(String.valueOf(row.get(0)));
map.setFunctionType(String.valueOf(row.get(1)));
map.setManufacturer(String.valueOf(row.get(2)));
map.setReportDate(String.valueOf(row.get(3)));
map.setTestMethod(String.valueOf(row.get(5)));
map.setCategory(String.valueOf(row.get(6)));
map.setReportId(map.getCode().replaceAll(" ", ""));
tableData.add(map);
}
saveRadhomeFiles(tableData, radhomeKey);
}
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
redisUtil.del(radhomeKey);
}
});
} catch (Exception e) {
redisUtil.del(radhomeKey);
throw new RuntimeException(e);
}
}
private String fixFileNames(String fileNames) {
String[] split = StringUtils.split(fileNames, ";");
List<String> result = new ArrayList<>();
for (String s : split) {
if (!StringUtils.startsWith(s, "http")) {
result.add("https://radhome.gsfc.nasa.gov/radhome/papers/" + s);
} else {
result.add(s);
}
}
return StringUtils.join(result, ";");
}
private void saveRadhomeFiles(List<ImportRecord> fileList, String type) {
ThreadUtil.execute(() -> {
try {
for (ImportRecord record : fileList) {
String fileUploadResult = "";
String fileUrl = record.getFileUrl();
if (fileUrl.contains(";")) {
String[] split = fileUrl.split(";");
List<String> result = new ArrayList<>();
for (String s : split) {
byte[] fileBytes = HttpUtil.downloadBytes(s);
InputStream inputStream = new ByteArrayInputStream(fileBytes);
result.add(MinioUtil.upload(inputStream, "radhome/" + s.substring(s.lastIndexOf("/") + 1)));
fileUploadResult = StringUtils.join(result, ";");
}
} else {
byte[] fileBytes = HttpUtil.downloadBytes(fileUrl);
InputStream inputStream = new ByteArrayInputStream(fileBytes);
fileUploadResult = MinioUtil.upload(inputStream, "radhome/" + fileUrl.substring(fileUrl.lastIndexOf("/") + 1));
}
System.out.println(fileUploadResult);
if (StringUtils.isNotBlank(fileUploadResult)) {
record.setFileUrl(fileUploadResult);
save(record);
}
}
redisUtil.del(type);
} catch (Exception e) {
e.printStackTrace();
}
});
}
private void saveEsaradFiles(List<ImportRecord> fileList, String type) {
ThreadUtil.execute(() -> {
try {
for (ImportRecord record : fileList) {
ImportRecord dbData = getOne(Wrappers.<ImportRecord>lambdaQuery().eq(ImportRecord::getReportSource, record.getReportId()));
if (Objects.nonNull(dbData)) {
continue;
}
String resultStr = "";
String dest = FileUtil.getTmpDirPath() + "esarad-" + record.getReportId() + "/";
FileUtil.mkdir(dest);
long fileSize = HttpUtil.downloadFile(record.getFileUrl(), dest);
if (fileSize > 0) {
List<File> files = FileUtil.loopFiles(dest);
for (File file : files) {
resultStr = MinioUtil.upload(IoUtil.toStream(file), "esarad/" + record.getReportId() + "-" + URLUtil.decode(file.getName(), Charset.defaultCharset()) );
}
}
System.out.println(resultStr);
if (StringUtils.isNotBlank(resultStr)) {
record.setFileUrl(resultStr);
save(record);
}
}
redisUtil.del(type);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}

View File

@@ -1,26 +1,30 @@
package org.jeecg.modules.system.controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.constant.enums.FileTypeEnum;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.filter.SsrfFileTypeFilter;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.oss.entity.OssFile;
import org.jeecg.modules.oss.service.IOssFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.*;
/**
@@ -36,39 +40,54 @@ import java.io.*;
@RequestMapping("/sys/common")
public class CommonController {
@Autowired
IOssFileService ossFileService;
@Value(value = "${jeecg.path.upload}")
private String uploadpath;
/**
* 本地local miniominio 阿里alioss
*/
@Value(value="${jeecg.uploadType}")
@Value(value = "${jeecg.uploadType}")
private String uploadType;
/**
* @Author 政辉
* 把指定URL后的字符串全部截断当成参数
* 这么做是为了防止URL中包含中文或者特殊字符/等)时,匹配不了的问题
*
* @param request
* @return
*/
private static String extractPathFromPattern(final HttpServletRequest request) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
}
/**
* @return
* @Author 政辉
*/
@GetMapping("/403")
public Result<?> noauth() {
public Result<?> noauth() {
return Result.error("没有权限,请联系管理员授权,后刷新缓存!");
}
/**
* 文件上传统一方法
*
* @param request
* @param response
* @return
*/
@PostMapping(value = "/upload")
public Result<?> upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
Result<?> result = new Result<>();
Result<OssFile> result = new Result<>();
String savePath = "";
String bizPath = request.getParameter("biz");
//LOWCOD-2580 sys/common/upload接口存在任意文件上传漏洞
if (oConvertUtils.isNotEmpty(bizPath)) {
if(bizPath.contains(SymbolConstant.SPOT_SINGLE_SLASH) || bizPath.contains(SymbolConstant.SPOT_DOUBLE_BACKSLASH)){
if (bizPath.contains(SymbolConstant.SPOT_SINGLE_SLASH) || bizPath.contains(SymbolConstant.SPOT_DOUBLE_BACKSLASH)) {
throw new JeecgBootException("上传目录bizPath格式非法");
}
}
@@ -76,92 +95,57 @@ public class CommonController {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获取上传文件对象
MultipartFile file = multipartRequest.getFile("file");
if(oConvertUtils.isEmpty(bizPath)){
if(CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)){
if (oConvertUtils.isEmpty(bizPath)) {
if (CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)) {
//未指定目录,则用阿里云默认目录 upload
bizPath = "upload";
//result.setMessage("使用阿里云文件上传时,必须添加目录!");
//result.setSuccess(false);
//return result;
}else{
} else {
bizPath = "";
}
}
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) {
//update-begin-author:liusq date:20221102 for: 过滤上传文件类型
SsrfFileTypeFilter.checkUploadFileType(file);
//update-end-author:liusq date:20221102 for: 过滤上传文件类型
//update-begin-author:lvdandan date:20200928 for:修改JEditor编辑器本地上传
savePath = this.uploadLocal(file,bizPath);
savePath = this.uploadLocal(file, bizPath);
//update-begin-author:lvdandan date:20200928 for:修改JEditor编辑器本地上传
/** 富文本编辑器及markdown本地上传时采用返回链接方式
//针对jeditor编辑器如何使 lcaol模式采用 base64格式存储
String jeditor = request.getParameter("jeditor");
if(oConvertUtils.isNotEmpty(jeditor)){
result.setMessage(CommonConstant.UPLOAD_TYPE_LOCAL);
result.setSuccess(true);
return result;
}else{
savePath = this.uploadLocal(file,bizPath);
}
*/
}else{
//针对jeditor编辑器如何使 lcaol模式采用 base64格式存储
String jeditor = request.getParameter("jeditor");
if(oConvertUtils.isNotEmpty(jeditor)){
result.setMessage(CommonConstant.UPLOAD_TYPE_LOCAL);
result.setSuccess(true);
return result;
}else{
savePath = this.uploadLocal(file,bizPath);
}
*/
} else {
//update-begin-author:taoyan date:20200814 for:文件上传改造
savePath = CommonUtils.upload(file, bizPath, uploadType);
OssFile ossFile = new OssFile();
ossFile.setFileName(file.getOriginalFilename());
ossFile.setUrl(savePath);
ossFileService.save(ossFile);
result.setResult(ossFile);
//update-end-author:taoyan date:20200814 for:文件上传改造
}
if(oConvertUtils.isNotEmpty(savePath)){
if (oConvertUtils.isNotEmpty(savePath)) {
result.setMessage(savePath);
result.setSuccess(true);
}else {
} else {
result.setMessage("上传失败!");
result.setSuccess(false);
}
return result;
}
/**
* 本地文件上传
* @param mf 文件
* @param bizPath 自定义路径
* @return
*/
private String uploadLocal(MultipartFile mf,String bizPath){
try {
String ctxPath = uploadpath;
String fileName = null;
File file = new File(ctxPath + File.separator + bizPath + File.separator );
if (!file.exists()) {
// 创建文件根目录
file.mkdirs();
}
// 获取文件名
String orgName = mf.getOriginalFilename();
orgName = CommonUtils.getFileName(orgName);
if(orgName.indexOf(SymbolConstant.SPOT)!=-1){
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
}else{
fileName = orgName+ "_" + System.currentTimeMillis();
}
String savePath = file.getPath() + File.separator + fileName;
File savefile = new File(savePath);
FileCopyUtils.copy(mf.getBytes(), savefile);
String dbpath = null;
if(oConvertUtils.isNotEmpty(bizPath)){
dbpath = bizPath + File.separator + fileName;
}else{
dbpath = fileName;
}
if (dbpath.contains(SymbolConstant.DOUBLE_BACKSLASH)) {
dbpath = dbpath.replace(SymbolConstant.DOUBLE_BACKSLASH, SymbolConstant.SINGLE_SLASH);
}
return dbpath;
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return "";
}
// @PostMapping(value = "/upload2")
// public Result<?> upload2(HttpServletRequest request, HttpServletResponse response) {
// Result<?> result = new Result<>();
@@ -200,71 +184,46 @@ public class CommonController {
// }
/**
* 预览图片&下载文件
* 请求地址http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
* 本地文件上传
*
* @param request
* @param response
* @param mf 文件
* @param bizPath 自定义路径
* @return
*/
@GetMapping(value = "/static/**")
public void view(HttpServletRequest request, HttpServletResponse response) {
// ISO-8859-1 ==> UTF-8 进行编码转换
String imgPath = extractPathFromPattern(request);
if(oConvertUtils.isEmpty(imgPath) || CommonConstant.STRING_NULL.equals(imgPath)){
return;
}
// 其余处理略
InputStream inputStream = null;
OutputStream outputStream = null;
private String uploadLocal(MultipartFile mf, String bizPath) {
try {
imgPath = imgPath.replace("..", "").replace("../","");
if (imgPath.endsWith(SymbolConstant.COMMA)) {
imgPath = imgPath.substring(0, imgPath.length() - 1);
String ctxPath = uploadpath;
String fileName = null;
File file = new File(ctxPath + File.separator + bizPath + File.separator);
if (!file.exists()) {
// 创建文件根目录
file.mkdirs();
}
//update-begin---author:liusq ---date:20230912 for检查下载文件类型--------------
SsrfFileTypeFilter.checkDownloadFileType(imgPath);
//update-end---author:liusq ---date:20230912 for检查下载文件类型--------------
String filePath = uploadpath + File.separator + imgPath;
File file = new File(filePath);
if(!file.exists()){
response.setStatus(404);
log.error("文件["+imgPath+"]不存在..");
return;
//throw new RuntimeException();
// 获取文件名
String orgName = mf.getOriginalFilename();
orgName = CommonUtils.getFileName(orgName);
if (orgName.indexOf(SymbolConstant.SPOT) != -1) {
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
} else {
fileName = orgName + "_" + System.currentTimeMillis();
}
// 设置强制下载不打开
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
inputStream = new BufferedInputStream(new FileInputStream(filePath));
outputStream = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
String savePath = file.getPath() + File.separator + fileName;
File savefile = new File(savePath);
FileCopyUtils.copy(mf.getBytes(), savefile);
String dbpath = null;
if (oConvertUtils.isNotEmpty(bizPath)) {
dbpath = bizPath + File.separator + fileName;
} else {
dbpath = fileName;
}
response.flushBuffer();
if (dbpath.contains(SymbolConstant.DOUBLE_BACKSLASH)) {
dbpath = dbpath.replace(SymbolConstant.DOUBLE_BACKSLASH, SymbolConstant.SINGLE_SLASH);
}
return dbpath;
} catch (IOException e) {
log.error("预览文件失败" + e.getMessage());
response.setStatus(404);
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
log.error(e.getMessage(), e);
}
return "";
}
// /**
@@ -326,9 +285,77 @@ public class CommonController {
// }
/**
* @功能pdf预览Iframe
* 预览图片&下载文件
* 请求地址http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
*
* @param request
* @param response
*/
@GetMapping(value = "/static/**")
public void view(HttpServletRequest request, HttpServletResponse response) {
// ISO-8859-1 ==> UTF-8 进行编码转换
String imgPath = extractPathFromPattern(request);
if (oConvertUtils.isEmpty(imgPath) || CommonConstant.STRING_NULL.equals(imgPath)) {
return;
}
// 其余处理略
InputStream inputStream = null;
OutputStream outputStream = null;
try {
imgPath = imgPath.replace("..", "").replace("../", "");
if (imgPath.endsWith(SymbolConstant.COMMA)) {
imgPath = imgPath.substring(0, imgPath.length() - 1);
}
//update-begin---author:liusq ---date:20230912 for检查下载文件类型--------------
SsrfFileTypeFilter.checkDownloadFileType(imgPath);
//update-end---author:liusq ---date:20230912 for检查下载文件类型--------------
String filePath = uploadpath + File.separator + imgPath;
File file = new File(filePath);
if (!file.exists()) {
response.setStatus(404);
log.error("文件[" + imgPath + "]不存在..");
return;
//throw new RuntimeException();
}
// 设置强制下载不打开
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
inputStream = new BufferedInputStream(new FileInputStream(filePath));
outputStream = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
} catch (IOException e) {
log.error("预览文件失败" + e.getMessage());
response.setStatus(404);
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
/**
* @param modelAndView
* @return
* @功能pdf预览Iframe
*/
@RequestMapping("/pdf/pdfPreviewIframe")
public ModelAndView pdfPreviewIframe(ModelAndView modelAndView) {
@@ -336,16 +363,4 @@ public class CommonController {
return modelAndView;
}
/**
* 把指定URL后的字符串全部截断当成参数
* 这么做是为了防止URL中包含中文或者特殊字符/等)时,匹配不了的问题
* @param request
* @return
*/
private static String extractPathFromPattern(final HttpServletRequest request) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
}
}