Merge branch 'mzcDev'

This commit is contained in:
mzc5649
2021-03-29 15:20:36 +08:00
3 changed files with 750 additions and 767 deletions
@@ -14,6 +14,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
@@ -120,7 +121,45 @@ public class CommonUtils {
} }
return url; return url;
} }
/**
* 本地文件上传
* @param mf 文件
* @param bizPath 自定义路径
* @return
*/
public static String uploadLocal(MultipartFile mf,String bizPath, String uploadpath){
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(".")!=-1){
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
}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("\\")) {
dbpath = dbpath.replace("\\", "/");
}
return dbpath;
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return "";
}
/** 当前系统数据库类型 */ /** 当前系统数据库类型 */
private static String DB_TYPE = ""; private static String DB_TYPE = "";
public static String getDatabaseType() { public static String getDatabaseType() {
@@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j;
import com.jero.common.util.filter.StrAttackFilter; import com.jero.common.util.filter.StrAttackFilter;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.Null;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLDecoder; import java.net.URLDecoder;
@@ -110,7 +111,7 @@ public class MinioUtil {
* @param objectName * @param objectName
* @return * @return
*/ */
public static InputStream getMinioFile(String bucketName,String objectName){ public static InputStream getMinioFile(String bucketName, String objectName){
InputStream inputStream = null; InputStream inputStream = null;
try { try {
initMinio(minioUrl, minioName, minioPass); initMinio(minioUrl, minioName, minioPass);
@@ -2,14 +2,14 @@ package com.jero.modules.system.controller;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jero.common.util.*;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import com.jero.common.api.vo.Result; import com.jero.common.api.vo.Result;
import com.jero.common.constant.CommonConstant; import com.jero.common.constant.CommonConstant;
import com.jero.common.system.api.ISysBaseAPI; import com.jero.common.system.api.ISysBaseAPI;
import com.jero.common.util.CommonUtils;
import com.jero.common.util.RestUtil;
import com.jero.common.util.TokenUtils;
import com.jero.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@@ -24,6 +24,7 @@ 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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
@@ -44,6 +45,9 @@ public class CommonController {
@Autowired @Autowired
private ISysBaseAPI sysBaseAPI; private ISysBaseAPI sysBaseAPI;
@Resource
private IOSSFileService ossFileService;
@Value(value = "${jero.path.upload}") @Value(value = "${jero.path.upload}")
private String uploadpath; private String uploadpath;
@@ -74,41 +78,28 @@ public class CommonController {
String savePath = ""; String savePath = "";
String bizPath = request.getParameter("biz"); String bizPath = request.getParameter("biz");
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)){
//未指定目录,则用阿里云默认目录 upload
bizPath = "upload";
//result.setMessage("使用阿里云文件上传时,必须添加目录!");
//result.setSuccess(false);
//return result;
}else{
bizPath = ""; bizPath = "";
}
} }
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
//update-begin-author:lvdandan date:20200928 for:修改JEditor编辑器本地上传 // 本地上传
savePath = this.uploadLocal(file,bizPath); savePath = CommonUtils.uploadLocal(file,bizPath,uploadpath);
//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{ }else{
//update-begin-author:taoyan date:20200814 for:文件上传改造 // minio上传
savePath = CommonUtils.upload(file, bizPath, uploadType); savePath = CommonUtils.upload(file, bizPath, uploadType);
//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);
//上传成功 进行数据库存储
OSSFile ossFile = new OSSFile();
String fileName = file.getOriginalFilename();
fileName = CommonUtils.getFileName(fileName);
ossFile.setFileName(fileName);
ossFile.setUrl(savePath);
ossFileService.save(ossFile);
}else { }else {
result.setMessage("上传失败!"); result.setMessage("上传失败!");
result.setSuccess(false); result.setSuccess(false);
@@ -116,83 +107,6 @@ public class CommonController {
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(".")!=-1){
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
}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("\\")) {
dbpath = dbpath.replace("\\", "/");
}
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<>();
// try {
// String ctxPath = uploadpath;
// String fileName = null;
// String bizPath = "files";
// String tempBizPath = request.getParameter("biz");
// if(oConvertUtils.isNotEmpty(tempBizPath)){
// bizPath = tempBizPath;
// }
// String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date());
// File file = new File(ctxPath + File.separator + bizPath + File.separator + nowday);
// if (!file.exists()) {
// file.mkdirs();// 创建文件根目录
// }
// MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// MultipartFile mf = multipartRequest.getFile("file");// 获取上传文件对象
// String orgName = mf.getOriginalFilename();// 获取文件名
// fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
// String savePath = file.getPath() + File.separator + fileName;
// File savefile = new File(savePath);
// FileCopyUtils.copy(mf.getBytes(), savefile);
// String dbpath = bizPath + File.separator + nowday + File.separator + fileName;
// if (dbpath.contains("\\")) {
// dbpath = dbpath.replace("\\", "/");
// }
// result.setMessage(dbpath);
// result.setSuccess(true);
// } catch (IOException e) {
// result.setSuccess(false);
// result.setMessage(e.getMessage());
// log.error(e.getMessage(), e);
// }
// return result;
// }
/** /**
* 预览图片&下载文件 * 预览图片&下载文件
* 请求地址:http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg} * 请求地址:http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
@@ -200,11 +114,11 @@ public class CommonController {
* @param request * @param request
* @param response * @param response
*/ */
@GetMapping(value = "/static/**") @GetMapping(value = "/download/**")
public void view(HttpServletRequest request, HttpServletResponse response) { public void view(HttpServletRequest request, HttpServletResponse response) {
// ISO-8859-1 ==> UTF-8 进行编码转换 // ISO-8859-1 ==> UTF-8 进行编码转换
String imgPath = extractPathFromPattern(request); String imgPath = extractPathFromPattern(request);
if(oConvertUtils.isEmpty(imgPath) || imgPath=="null"){ if(oConvertUtils.isEmpty(imgPath) || imgPath == "null"){
return; return;
} }
// 其余处理略 // 其余处理略
@@ -215,15 +129,44 @@ public class CommonController {
if (imgPath.endsWith(",")) { if (imgPath.endsWith(",")) {
imgPath = imgPath.substring(0, imgPath.length() - 1); imgPath = imgPath.substring(0, imgPath.length() - 1);
} }
String filePath = uploadpath + File.separator + imgPath; String fileName = "";
File file = new File(filePath); LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
if(!file.exists()){ if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
response.setStatus(404); //本地下载
throw new RuntimeException("文件不存在.."); String filePath = uploadpath + File.separator + imgPath;
File file = new File(filePath);
if(!file.exists()){
response.setStatus(404);
throw new RuntimeException("文件不存在..");
}
// 查询数据表数据是否存在
queryWrapper.eq(OSSFile::getUrl,imgPath);
OSSFile ossFile = ossFileService.getOne(queryWrapper);
if( null == ossFile){
throw new RuntimeException("文件不存在..");
}
// 文件名称
fileName = file.getName();
inputStream = new BufferedInputStream(new FileInputStream(filePath));
}else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
// minio 下载
queryWrapper.eq(OSSFile::getUrl,imgPath.replaceFirst("/","//"));
OSSFile ossFile = ossFileService.getOne(queryWrapper);
// 查询该文件数据是否存在
if( null == ossFile){
throw new RuntimeException("文件不存在");
}
// 通过MinioUtil查询时 只需要 桶后面的路径
String minioUrl = MinioUtil.getMinioUrl();
// Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径
minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
String url = ossFile.getUrl().replace(minioUrl, "");
// 文件名称
fileName = ossFile.getFileName();
inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
} }
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
response.setContentType("application/force-download");// 设置强制下载不打开 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(); outputStream = response.getOutputStream();
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len; int len;