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

@@ -227,7 +227,7 @@ jeecg:
#webapp文件路径 #webapp文件路径
webapp: /opt/webapp webapp: /opt/webapp
shiro: shiro:
excludeUrls: /sys/user/**,/test/jeecgDemo/demo3,/test/jeecgDemo/redisDemo/**,/bigscreen/category/**,/bigscreen/visual/**,/bigscreen/map/**,/jmreport/bigscreen2/** excludeUrls: /sys/common/upload,/sys/user/**,/test/jeecgDemo/demo3,/test/jeecgDemo/redisDemo/**,/bigscreen/category/**,/bigscreen/visual/**,/bigscreen/map/**,/jmreport/bigscreen2/**
#阿里云oss存储和大鱼短信秘钥配置 #阿里云oss存储和大鱼短信秘钥配置
oss: oss:
accessKey: ?? accessKey: ??

View File

@@ -229,7 +229,7 @@ jeecg:
#webapp文件路径 #webapp文件路径
webapp: /opt/jeecg-boot/webapp webapp: /opt/jeecg-boot/webapp
shiro: shiro:
excludeUrls: /test/jeecgDemo/demo3,/test/jeecgDemo/redisDemo/**,/bigscreen/category/**,/bigscreen/visual/**,/bigscreen/map/**,/jmreport/bigscreen2/**,/api/getUserInfo,/api/sys/common/**,/sys/common/** excludeUrls: /sys/common/upload,/test/jeecgDemo/demo3,/test/jeecgDemo/redisDemo/**,/bigscreen/category/**,/bigscreen/visual/**,/bigscreen/map/**,/jmreport/bigscreen2/**,/api/getUserInfo,/api/sys/common/**,/sys/common/**
#阿里云oss存储和大鱼短信秘钥配置 #阿里云oss存储和大鱼短信秘钥配置
oss: oss:
accessKey: ?? accessKey: ??

View File

@@ -229,7 +229,7 @@ jeecg:
#webapp文件路径 #webapp文件路径
webapp: /opt/jeecg-boot/webapp webapp: /opt/jeecg-boot/webapp
shiro: shiro:
excludeUrls: /test/jeecgDemo/demo3,/test/jeecgDemo/redisDemo/**,/bigscreen/category/**,/bigscreen/visual/**,/bigscreen/map/**,/jmreport/bigscreen2/**,/api/getUserInfo,/api/sys/common/**,/sys/common/** excludeUrls: /sys/common/upload,/test/jeecgDemo/demo3,/test/jeecgDemo/redisDemo/**,/bigscreen/category/**,/bigscreen/visual/**,/bigscreen/map/**,/jmreport/bigscreen2/**,/api/getUserInfo,/api/sys/common/**,/sys/common/**
#阿里云oss存储和大鱼短信秘钥配置 #阿里云oss存储和大鱼短信秘钥配置
oss: oss:
accessKey: ?? accessKey: ??

View File

@@ -72,255 +72,265 @@ public class ImportRecordServiceImpl extends ServiceImpl<ImportRecordMapper, Imp
/** /**
* https://esarad.esa.int/ * https://esarad.esa.int/
*/ */
@Override // @Override
public void esaradCrawler() { // public void esaradCrawler() {
try { // try {
String crawlerRunning = String.valueOf(redisUtil.get(esaradKey)); // String crawlerRunning = String.valueOf(redisUtil.get(esaradKey));
if (StringUtils.equals(crawlerRunning, "T")) { // if (StringUtils.equals(crawlerRunning, "T")) {
throw new RuntimeException("爬虫任务执行中"); // throw new RuntimeException("爬虫任务执行中");
} // }
redisUtil.set(esaradKey, "T", 24 * 60 * 60); // redisUtil.set(esaradKey, "T", 24 * 60 * 60);
//
List<ImportRecord> tableData = new ArrayList<>(); // List<ImportRecord> tableData = new ArrayList<>();
//
Document doc = Jsoup.connect("https://esarad.esa.int").get(); // Document doc = Jsoup.connect("https://esarad.esa.int").get();
Element table = doc.getElementById("dtReports"); // Element table = doc.getElementById("dtReports");
if (Objects.isNull(table)) { // if (Objects.isNull(table)) {
redisUtil.del(esaradKey); // redisUtil.del(esaradKey);
throw new RuntimeException("爬虫获取数据失败"); // throw new RuntimeException("爬虫获取数据失败");
} // }
// Get the tbody element within the table // // Get the tbody element within the table
Element tbody = table.select("tbody").first(); // Select the first tbody element // Element tbody = table.select("tbody").first(); // Select the first tbody element
//
if (tbody == null) { // if (tbody == null) {
redisUtil.del(esaradKey); // redisUtil.del(esaradKey);
throw new RuntimeException("爬虫获取数据失败"); // throw new RuntimeException("爬虫获取数据失败");
} // }
// Create a list to store the row maps // // Create a list to store the row maps
//
// Extract headers from the first row // // Extract headers from the first row
Elements headers = table.select("thead").first().select("tr").first().select("th"); // Elements headers = table.select("thead").first().select("tr").first().select("th");
List<String> headerNames = new ArrayList<>(); // List<String> headerNames = new ArrayList<>();
for (Element header : headers) { // for (Element header : headers) {
headerNames.add(header.text()); // headerNames.add(header.text());
} // }
//
// Select all rows in the tbody // // Select all rows in the tbody
Elements rows = tbody.select("tr"); // Elements rows = tbody.select("tr");
//
for (int j = 0; j < rows.size(); j++) { // for (int j = 0; j < rows.size(); j++) {
if (j > 10) { // if (j > 10) {
break; // break;
} // }
Element row = rows.get(j); // Element row = rows.get(j);
//
// Select all cells in the row // // Select all cells in the row
ImportRecord importRecord = new ImportRecord(); // ImportRecord importRecord = new ImportRecord();
//
Elements cells = row.select("td"); // Elements cells = row.select("td");
//
if (cells.size() == headerNames.size()) { // Ensure the number of cells matches the number of headers // if (cells.size() == headerNames.size()) { // Ensure the number of cells matches the number of headers
//
for (int i = 0; i < cells.size(); i++) { // for (int i = 0; i < cells.size(); i++) {
//
String header = headerNames.get(i); // String header = headerNames.get(i);
String value = cells.get(i).text(); // String value = cells.get(i).text();
switch (header) { // switch (header) {
case "Radiation Test Method": // case "Radiation Test Method":
importRecord.setTestMethod(value); // importRecord.setTestMethod(value);
break; // break;
case "EPPL Familiy": // case "EPPL Familiy":
importRecord.setCategory(value); // importRecord.setCategory(value);
break; // break;
case "EPPL Group": // case "EPPL Group":
importRecord.setSubCategory(value); // importRecord.setSubCategory(value);
break; // break;
case "DUT Manufacturer": // case "DUT Manufacturer":
importRecord.setManufacturer(value); // importRecord.setManufacturer(value);
break; // break;
case "Function": // case "Function":
importRecord.setFunctionType(value); // importRecord.setFunctionType(value);
break; // break;
case "Report Date": // case "Report Date":
importRecord.setReportDate(value); // importRecord.setReportDate(value);
break; // break;
case "Report Source": // case "Report Source":
importRecord.setReportSource(value); // importRecord.setReportSource(value);
break; // break;
case "Technology": // case "Technology":
importRecord.setTechnology(value); // importRecord.setTechnology(value);
break; // break;
case "Id": // case "Id":
importRecord.setReportId(value); // importRecord.setReportId(value);
break; // break;
case "DUT part type": // case "DUT part type":
importRecord.setCode(value); // importRecord.setCode(value);
break; // break;
case "Radiation Test Type": // case "Radiation Test Type":
importRecord.setRadiationTestType(value); // importRecord.setRadiationTestType(value);
break; // break;
} // }
//
} // }
//
// Add the map to the list // // Add the map to the list
tableData.add(importRecord); // tableData.add(importRecord);
} // }
} // }
//
// Print the list of maps // // Print the list of maps
for (ImportRecord rowMap : tableData) { // for (ImportRecord rowMap : tableData) {
rowMap.setFileUrl("https://esarad.esa.int/?id=" + rowMap.getReportId() + "&handler=DownloadDb"); // rowMap.setFileUrl("https://esarad.esa.int/?id=" + rowMap.getReportId() + "&handler=DownloadDb");
} // }
saveEsaradFiles(tableData, esaradKey); // saveEsaradFiles(tableData, esaradKey);
//
} catch (Exception e) { // } catch (Exception e) {
redisUtil.del(esaradKey); // redisUtil.del(esaradKey);
throw new RuntimeException(e); // throw new RuntimeException(e);
} // }
} // }
/** /**
* https://radhome.gsfc.nasa.gov/radhome/RadDataBase/RadDataBase.html * 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 @Override
public void radhomeCrawler() { 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; package org.jeecg.modules.system.controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result; import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant; import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.constant.enums.FileTypeEnum;
import org.jeecg.common.exception.JeecgBootException; import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.CommonUtils; import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.filter.SsrfFileTypeFilter; import org.jeecg.common.util.filter.SsrfFileTypeFilter;
import org.jeecg.common.util.oConvertUtils; 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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.AntPathMatcher; import org.springframework.util.AntPathMatcher;
import org.springframework.util.FileCopyUtils; 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.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
/** /**
@@ -36,39 +40,54 @@ import java.io.*;
@RequestMapping("/sys/common") @RequestMapping("/sys/common")
public class CommonController { public class CommonController {
@Autowired
IOssFileService ossFileService;
@Value(value = "${jeecg.path.upload}") @Value(value = "${jeecg.path.upload}")
private String uploadpath; private String uploadpath;
/** /**
* 本地local miniominio 阿里alioss * 本地local miniominio 阿里alioss
*/ */
@Value(value="${jeecg.uploadType}") @Value(value = "${jeecg.uploadType}")
private String uploadType; private String uploadType;
/** /**
* @Author 政辉 * 把指定URL后的字符串全部截断当成参数
* 这么做是为了防止URL中包含中文或者特殊字符/等)时,匹配不了的问题
*
* @param request
* @return * @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") @GetMapping("/403")
public Result<?> noauth() { public Result<?> noauth() {
return Result.error("没有权限,请联系管理员授权,后刷新缓存!"); return Result.error("没有权限,请联系管理员授权,后刷新缓存!");
} }
/** /**
* 文件上传统一方法 * 文件上传统一方法
*
* @param request * @param request
* @param response * @param response
* @return * @return
*/ */
@PostMapping(value = "/upload") @PostMapping(value = "/upload")
public Result<?> upload(HttpServletRequest request, HttpServletResponse response) throws Exception { public Result<?> upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
Result<?> result = new Result<>(); Result<OssFile> result = new Result<>();
String savePath = ""; String savePath = "";
String bizPath = request.getParameter("biz"); String bizPath = request.getParameter("biz");
//LOWCOD-2580 sys/common/upload接口存在任意文件上传漏洞 //LOWCOD-2580 sys/common/upload接口存在任意文件上传漏洞
if (oConvertUtils.isNotEmpty(bizPath)) { 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格式非法"); throw new JeecgBootException("上传目录bizPath格式非法");
} }
} }
@@ -76,92 +95,57 @@ public class CommonController {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获取上传文件对象 // 获取上传文件对象
MultipartFile file = multipartRequest.getFile("file"); MultipartFile file = multipartRequest.getFile("file");
if(oConvertUtils.isEmpty(bizPath)){ if (oConvertUtils.isEmpty(bizPath)) {
if(CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)){ if (CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)) {
//未指定目录,则用阿里云默认目录 upload //未指定目录,则用阿里云默认目录 upload
bizPath = "upload"; bizPath = "upload";
//result.setMessage("使用阿里云文件上传时,必须添加目录!"); //result.setMessage("使用阿里云文件上传时,必须添加目录!");
//result.setSuccess(false); //result.setSuccess(false);
//return result; //return result;
}else{ } else {
bizPath = ""; bizPath = "";
} }
} }
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) {
//update-begin-author:liusq date:20221102 for: 过滤上传文件类型 //update-begin-author:liusq date:20221102 for: 过滤上传文件类型
SsrfFileTypeFilter.checkUploadFileType(file); SsrfFileTypeFilter.checkUploadFileType(file);
//update-end-author:liusq date:20221102 for: 过滤上传文件类型 //update-end-author:liusq date:20221102 for: 过滤上传文件类型
//update-begin-author:lvdandan date:20200928 for:修改JEditor编辑器本地上传 //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编辑器本地上传 //update-begin-author:lvdandan date:20200928 for:修改JEditor编辑器本地上传
/** 富文本编辑器及markdown本地上传时采用返回链接方式 /** 富文本编辑器及markdown本地上传时采用返回链接方式
//针对jeditor编辑器如何使 lcaol模式采用 base64格式存储 //针对jeditor编辑器如何使 lcaol模式采用 base64格式存储
String jeditor = request.getParameter("jeditor"); String jeditor = request.getParameter("jeditor");
if(oConvertUtils.isNotEmpty(jeditor)){ if(oConvertUtils.isNotEmpty(jeditor)){
result.setMessage(CommonConstant.UPLOAD_TYPE_LOCAL); result.setMessage(CommonConstant.UPLOAD_TYPE_LOCAL);
result.setSuccess(true); result.setSuccess(true);
return result; return result;
}else{ }else{
savePath = this.uploadLocal(file,bizPath); savePath = this.uploadLocal(file,bizPath);
} }
*/ */
}else{ } else {
//update-begin-author:taoyan date:20200814 for:文件上传改造 //update-begin-author:taoyan date:20200814 for:文件上传改造
savePath = CommonUtils.upload(file, bizPath, uploadType); 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:文件上传改造 //update-end-author:taoyan date:20200814 for:文件上传改造
} }
if(oConvertUtils.isNotEmpty(savePath)){ if (oConvertUtils.isNotEmpty(savePath)) {
result.setMessage(savePath); result.setMessage(savePath);
result.setSuccess(true); result.setSuccess(true);
}else { } else {
result.setMessage("上传失败!"); result.setMessage("上传失败!");
result.setSuccess(false); result.setSuccess(false);
} }
return result; 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") // @PostMapping(value = "/upload2")
// public Result<?> upload2(HttpServletRequest request, HttpServletResponse response) { // public Result<?> upload2(HttpServletRequest request, HttpServletResponse response) {
// Result<?> result = new Result<>(); // Result<?> result = new Result<>();
@@ -200,71 +184,46 @@ public class CommonController {
// } // }
/** /**
* 预览图片&下载文件 * 本地文件上传
* 请求地址http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
* *
* @param request * @param mf 文件
* @param response * @param bizPath 自定义路径
* @return
*/ */
@GetMapping(value = "/static/**") private String uploadLocal(MultipartFile mf, String bizPath) {
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 { try {
imgPath = imgPath.replace("..", "").replace("../",""); String ctxPath = uploadpath;
if (imgPath.endsWith(SymbolConstant.COMMA)) { String fileName = null;
imgPath = imgPath.substring(0, imgPath.length() - 1); 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); String orgName = mf.getOriginalFilename();
//update-end---author:liusq ---date:20230912 for检查下载文件类型-------------- orgName = CommonUtils.getFileName(orgName);
if (orgName.indexOf(SymbolConstant.SPOT) != -1) {
String filePath = uploadpath + File.separator + imgPath; fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
File file = new File(filePath); } else {
if(!file.exists()){ fileName = orgName + "_" + System.currentTimeMillis();
response.setStatus(404);
log.error("文件["+imgPath+"]不存在..");
return;
//throw new RuntimeException();
} }
// 设置强制下载不打开 String savePath = file.getPath() + File.separator + fileName;
response.setContentType("application/force-download"); File savefile = new File(savePath);
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1")); FileCopyUtils.copy(mf.getBytes(), savefile);
inputStream = new BufferedInputStream(new FileInputStream(filePath)); String dbpath = null;
outputStream = response.getOutputStream(); if (oConvertUtils.isNotEmpty(bizPath)) {
byte[] buf = new byte[1024]; dbpath = bizPath + File.separator + fileName;
int len; } else {
while ((len = inputStream.read(buf)) > 0) { dbpath = fileName;
outputStream.write(buf, 0, len);
} }
response.flushBuffer(); if (dbpath.contains(SymbolConstant.DOUBLE_BACKSLASH)) {
dbpath = dbpath.replace(SymbolConstant.DOUBLE_BACKSLASH, SymbolConstant.SINGLE_SLASH);
}
return dbpath;
} catch (IOException e) { } catch (IOException e) {
log.error("预览文件失败" + e.getMessage()); log.error(e.getMessage(), e);
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);
}
}
} }
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 * @param modelAndView
* @return * @return
* @功能pdf预览Iframe
*/ */
@RequestMapping("/pdf/pdfPreviewIframe") @RequestMapping("/pdf/pdfPreviewIframe")
public ModelAndView pdfPreviewIframe(ModelAndView modelAndView) { public ModelAndView pdfPreviewIframe(ModelAndView modelAndView) {
@@ -336,16 +363,4 @@ public class CommonController {
return modelAndView; 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);
}
} }