更新
This commit is contained in:
parent
c9108cd74a
commit
c5e324aeed
62
pom.xml
62
pom.xml
@ -11,6 +11,7 @@
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
<version>2.1.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
@ -41,31 +42,37 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>2.9.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>2.9.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.68</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-log4j2</artifactId>
|
||||
<version>2.1.6.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.9</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
@ -90,11 +97,66 @@
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>3.7.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>2.0.9</version>
|
||||
</dependency>
|
||||
|
||||
<!--ureport2 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.bstek.ureport</groupId>
|
||||
<artifactId>ureport2-console</artifactId>
|
||||
<version>2.2.9</version>
|
||||
</dependency>
|
||||
|
||||
<!-- mysql驱动 -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.16</version>
|
||||
</dependency>
|
||||
<!-- oracle驱动 -->
|
||||
<!--<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>11.2.0.3</version>
|
||||
</dependency>-->
|
||||
<!-- mssql驱动 -->
|
||||
<!--<dependency>
|
||||
<groupId>com.microsoft.sqlserver</groupId>
|
||||
<artifactId>sqljdbc4</artifactId>
|
||||
<version>4.0</version>
|
||||
</dependency>-->
|
||||
<!-- postgresql驱动 -->
|
||||
<!--<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.13</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.1.2</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-generator</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- 其他依赖... -->
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.14</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>hb-minio</finalName>
|
||||
|
@ -2,13 +2,19 @@ package com.hb.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class CorsConfig {
|
||||
|
||||
@Bean
|
||||
/**
|
||||
* 前后端的跨域设置是在后端工程里使用跨域拦截器进行配置的,而添加的自定义登录拦截器是在跨域拦截器之前执行的,导致跨域拦截器失效
|
||||
*/
|
||||
/*@Bean
|
||||
public WebMvcConfigurer corsConfigurer() {
|
||||
return new WebMvcConfigurer() {
|
||||
@Override
|
||||
@ -29,5 +35,21 @@ public class CorsConfig {
|
||||
|
||||
}
|
||||
};
|
||||
}*/
|
||||
|
||||
// 让跨域配置在登录拦截器之前执行。而Filter的执行顺序大于自定义拦截器,所以改为在Filter里实现跨域的配置
|
||||
// 当前跨域请求最大有效时长。这里默认1天
|
||||
private static final long MAX_AGE = 24 * 60 * 60;
|
||||
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
CorsConfiguration corsConfiguration = new CorsConfiguration();
|
||||
corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
|
||||
corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
|
||||
corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
|
||||
corsConfiguration.setMaxAge(MAX_AGE);
|
||||
source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
}
|
||||
|
122
src/main/java/com/hb/config/DefaultImageProvider.java
Normal file
122
src/main/java/com/hb/config/DefaultImageProvider.java
Normal file
@ -0,0 +1,122 @@
|
||||
package com.hb.config;
|
||||
|
||||
import com.bstek.ureport.exception.ReportComputeException;
|
||||
import com.bstek.ureport.provider.image.ImageProvider;
|
||||
import com.hb.util.MinioUtil;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author Jacky.gao
|
||||
* @since 2017年3月6日
|
||||
*/
|
||||
@Component
|
||||
public class DefaultImageProvider implements ImageProvider, ApplicationContextAware {
|
||||
private ApplicationContext applicationContext;
|
||||
private String baseWebPath;
|
||||
|
||||
@Autowired
|
||||
private MinioUtil minioUtil;
|
||||
|
||||
@Override
|
||||
public InputStream getImage(String path) {
|
||||
try {
|
||||
if (path.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX) || path.startsWith("/WEB-INF")) {
|
||||
return applicationContext.getResource(path).getInputStream();
|
||||
} else if (path.startsWith("http://") || path.startsWith("https://")) {
|
||||
return getImageStream(path);
|
||||
} else if (path.startsWith("minio:")) {
|
||||
return getMinioImageStream(path);
|
||||
} else if (path.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
|
||||
String imagePath = path.split(ResourceUtils.FILE_URL_PREFIX)[1];
|
||||
return new FileInputStream(imagePath);
|
||||
} else {
|
||||
path = baseWebPath + path;
|
||||
return new FileInputStream(path);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new ReportComputeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean support(String path) {
|
||||
if (path.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
|
||||
return true;
|
||||
} else if (baseWebPath != null && (path.startsWith("/") || path.startsWith("/WEB-INF"))) {
|
||||
return true;
|
||||
} else if (path.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
|
||||
return true;
|
||||
} else if ((path.startsWith("http://") || path.startsWith("https://"))) {
|
||||
return true;
|
||||
} else if ((path.startsWith("minio:"))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (applicationContext instanceof WebApplicationContext) {
|
||||
WebApplicationContext context = (WebApplicationContext) applicationContext;
|
||||
baseWebPath = context.getServletContext().getRealPath("/");
|
||||
}
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public InputStream getImageStream(String url) {
|
||||
try {
|
||||
url = urlEncodeChinese(url);
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.setReadTimeout(5000);
|
||||
connection.setConnectTimeout(5000);
|
||||
connection.setRequestMethod("GET");
|
||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
return inputStream;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("获取网络图片出现异常,图片路径为:" + url);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String urlEncodeChinese(String url) {
|
||||
try {
|
||||
Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url);
|
||||
String tmp = "";
|
||||
while (matcher.find()) {
|
||||
tmp = matcher.group();
|
||||
url = url.replaceAll(tmp, URLEncoder.encode(tmp, "UTF-8"));
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return url.replace(" ", "%20");
|
||||
}
|
||||
|
||||
private InputStream getMinioImageStream(String path) throws Exception {
|
||||
path = path.substring(path.indexOf(":") + 1, path.length());
|
||||
String[] pathArr = path.split(":");
|
||||
String bucket = pathArr[0];
|
||||
String fileDirName = pathArr[1];
|
||||
return minioUtil.download(bucket, fileDirName);
|
||||
}
|
||||
|
||||
}
|
@ -14,7 +14,7 @@ public class InterceptorConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
//定义排除swagger访问的路径配置
|
||||
String[] swaggerExcludes=new String[]{"/swagger-ui.html","/swagger-resources/**","/webjars/**", "/getPicture"};
|
||||
String[] swaggerExcludes=new String[]{"/swagger-ui.html","/swagger-resources/**","/webjars/**", "/ureport/**"};
|
||||
|
||||
registry.addInterceptor(new MyInterceptor())
|
||||
// addPathPatterns 用于添加拦截规则
|
||||
|
21
src/main/java/com/hb/config/MyUReportPropertyConfigurer.java
Normal file
21
src/main/java/com/hb/config/MyUReportPropertyConfigurer.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.hb.config;
|
||||
|
||||
import com.bstek.ureport.UReportPropertyPlaceholderConfigurer;
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* 继承UReportPropertyPlaceholderConfigurer, 装载application.yml
|
||||
*
|
||||
* @author liangwen
|
||||
* 2023年5月24日
|
||||
*/
|
||||
public class MyUReportPropertyConfigurer extends UReportPropertyPlaceholderConfigurer {
|
||||
|
||||
public MyUReportPropertyConfigurer(String path) {
|
||||
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
|
||||
yaml.setResources(new ClassPathResource(path));
|
||||
this.setProperties(yaml.getObject());
|
||||
}
|
||||
|
||||
}
|
42
src/main/java/com/hb/config/UreportConfig.java
Normal file
42
src/main/java/com/hb/config/UreportConfig.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.hb.config;
|
||||
|
||||
import com.bstek.ureport.console.UReportServlet;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* UReport 配置
|
||||
* @author liangwen
|
||||
* 2023年5月24日
|
||||
*/
|
||||
@Configuration
|
||||
@ImportResource("classpath:ureport-console-context.xml")
|
||||
public class UreportConfig implements EnvironmentAware {
|
||||
|
||||
private Environment environment;
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MyUReportPropertyConfigurer propertyrConfigurer(){
|
||||
String activeProfile = environment.getProperty("spring.profiles.active");
|
||||
if(StringUtils.isNotBlank(activeProfile)){
|
||||
activeProfile = "-" + activeProfile;
|
||||
}
|
||||
return new MyUReportPropertyConfigurer("application" + activeProfile + ".yml");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletRegistrationBean<UReportServlet> buildUreportServlet(){
|
||||
return new ServletRegistrationBean<UReportServlet>(new UReportServlet(), "/ureport/*");
|
||||
}
|
||||
|
||||
}
|
@ -8,6 +8,7 @@ import java.util.*;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.hb.listener.ProgressListener;
|
||||
import com.hb.util.DateUtil;
|
||||
import com.hb.util.ServletUtils;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@ -16,14 +17,18 @@ import io.swagger.annotations.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.tomcat.util.http.fileupload.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.hb.domain.AjaxResult;
|
||||
import com.hb.util.MinioUtil;
|
||||
|
||||
@Api(tags = "Minio操作接口")
|
||||
@RestController
|
||||
@CrossOrigin(origins = "*")
|
||||
@RequestMapping("/ames/minio")
|
||||
public class MinioController {
|
||||
|
||||
@Autowired
|
||||
@ -38,7 +43,7 @@ public class MinioController {
|
||||
|
||||
@ApiOperation("上传一个文件")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "uploadfile", value = "MultipartFile文件对象", dataType="__file", paramType="form", required = true),
|
||||
@ApiImplicitParam(name = "uploadfile", value = "MultipartFile文件对象", dataType = "__file", paramType = "form", required = true),
|
||||
@ApiImplicitParam(name = "bucket", value = "桶名称", required = true),
|
||||
@ApiImplicitParam(name = "objectName", value = "文件夹路径,例:20231215/002938", required = true),
|
||||
@ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer YOUR_TOKEN_HERE", required = true)
|
||||
@ -58,20 +63,67 @@ public class MinioController {
|
||||
|
||||
@ApiOperation("上传一个文件2")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "uploadfile", value = "MultipartFile文件对象", dataType="__file", paramType="form", required = true),
|
||||
@ApiImplicitParam(name = "uploadfile", value = "MultipartFile文件对象", dataType = "__file", paramType = "form", required = true),
|
||||
@ApiImplicitParam(name = "bucket", value = "桶名称", required = true),
|
||||
@ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer YOUR_TOKEN_HERE", required = true)
|
||||
})
|
||||
@RequestMapping(value = "/uploadfile2", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public AjaxResult fileupload2(@RequestParam MultipartFile uploadfile, @RequestParam String bucket) throws Exception {
|
||||
minioUtil.createBucket(bucket);
|
||||
String fileDir = buildDir();
|
||||
String fileDirName = buildDirName(fileDir, uploadfile.getOriginalFilename());
|
||||
minioUtil.uploadFile2(uploadfile, bucket, fileDirName);
|
||||
return AjaxResult.success();
|
||||
public AjaxResult fileupload2(@RequestParam MultipartFile uploadfile, @RequestParam String bucket) {
|
||||
AjaxResult ajaxResult = new AjaxResult();
|
||||
try {
|
||||
minioUtil.createBucket(bucket);
|
||||
String fileDir = buildDir();
|
||||
String fileDirName = buildDirName(fileDir, uploadfile.getOriginalFilename());
|
||||
minioUtil.uploadFile2(uploadfile, bucket, fileDirName);
|
||||
ajaxResult.put("code", 200);
|
||||
ajaxResult.put("bucket", bucket);
|
||||
ajaxResult.put("fileDirName", fileDirName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ajaxResult.put("code", 500);
|
||||
ajaxResult.put("msg", "上传失败");
|
||||
}
|
||||
return ajaxResult;
|
||||
}
|
||||
|
||||
/*@ApiOperation("上传一个文件3")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "uploadfile", value = "MultipartFile文件对象", dataType = "__file", paramType = "form", required = true),
|
||||
@ApiImplicitParam(name = "bucket", value = "桶名称", required = true),
|
||||
@ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer YOUR_TOKEN_HERE", required = true)
|
||||
})
|
||||
@RequestMapping(value = "/uploadfile3", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public AjaxResult fileupload3(@RequestParam MultipartFile uploadfile, @RequestParam String bucket) {
|
||||
AjaxResult ajaxResult = new AjaxResult();
|
||||
try {
|
||||
minioUtil.createBucket(bucket);
|
||||
String fileDir = buildDir();
|
||||
String fileDirName = buildDirName(fileDir, uploadfile.getOriginalFilename());
|
||||
String putURL = minioUtil.getOneURL(fileDirName, bucket);
|
||||
|
||||
// 使用RestTemplate上传文件并获取进度
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
ProgressListener progressListener = new ProgressListener(uploadfile.getSize(), uploadfile);
|
||||
restTemplate.execute(putURL, HttpMethod.PUT, progressListener, progressListener);
|
||||
|
||||
// 获取上传进度
|
||||
float progress = progressListener.getProgress();
|
||||
|
||||
ajaxResult.put("code", 200);
|
||||
ajaxResult.put("bucket", bucket);
|
||||
ajaxResult.put("fileDirName", fileDirName);
|
||||
ajaxResult.put("progress", progress);
|
||||
} catch (Exception e) {
|
||||
ajaxResult.put("code", 500);
|
||||
ajaxResult.put("msg", "上传失败");
|
||||
}
|
||||
return ajaxResult;
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
@ApiOperation("递归列出一个桶中的所有文件和目录")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "bucket", value = "桶名称", required = true),
|
||||
@ -92,40 +144,45 @@ public class MinioController {
|
||||
@RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public void downloadFile(@RequestParam String bucket, @RequestParam String objectName,
|
||||
HttpServletResponse response) throws Exception {
|
||||
InputStream stream = minioUtil.download(bucket, objectName);
|
||||
ServletOutputStream output = response.getOutputStream();
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
IOUtils.copy(stream, output);
|
||||
HttpServletResponse response) {
|
||||
try {
|
||||
InputStream stream = minioUtil.download(bucket, objectName);
|
||||
ServletOutputStream output = response.getOutputStream();
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
IOUtils.copy(stream, output);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(e.getMessage())));
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOperation("下载一个文件")
|
||||
@ApiOperation("预览文件")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "bucket", value = "桶名称", required = true),
|
||||
@ApiImplicitParam(name = "objectName", value = "文件全路径,例:20231215/002938/gh_03ace048af5d_344 (1) (2).jpg", required = true),
|
||||
@ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer YOUR_TOKEN_HERE", required = true)
|
||||
})
|
||||
@RequestMapping(value = "/getPicture", method = RequestMethod.GET)
|
||||
@RequestMapping(value = "/previewFile", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public void getLocalPicture(@RequestParam String bucket, @RequestParam String objectName,
|
||||
HttpServletResponse response) {
|
||||
public void previewFile(@RequestParam String bucket, @RequestParam String objectName,
|
||||
HttpServletResponse response) {
|
||||
try {
|
||||
String contentType = minioUtil.getContenType(bucket, objectName);
|
||||
// 判断文件类型是否为图片
|
||||
boolean isImage = contentType != null && contentType.startsWith("image/");
|
||||
/*boolean isImage = contentType != null && contentType.startsWith("image/");
|
||||
if (!isImage) {
|
||||
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error("文件类型不是图片,无法查看!")));
|
||||
} else {
|
||||
} else {*/
|
||||
InputStream stream = minioUtil.download(bucket, objectName);
|
||||
ServletOutputStream output = response.getOutputStream();
|
||||
//response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
|
||||
//response.setContentType("application/octet-stream");
|
||||
//response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType(contentType);
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
IOUtils.copy(stream, output);
|
||||
}
|
||||
}catch (Exception e) {
|
||||
//}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(e.getMessage())));
|
||||
}
|
||||
|
@ -21,8 +21,16 @@ public class MyInterceptor extends HandlerInterceptorAdapter {
|
||||
//在请求处理之前进行拦截处理
|
||||
//System.out.println("测试拦截器------preHandle------" + request.getRequestURL());
|
||||
//返回true表示继续执行请求,返回false表示停止执行请求;
|
||||
String token = request.getHeader("Authorization");
|
||||
if (StringUtils.hasText(token)){
|
||||
String token = null;
|
||||
String authorization = request.getHeader("Authorization");
|
||||
if (StringUtils.hasText(authorization)) {
|
||||
token = authorization;
|
||||
}
|
||||
String requestToken = (String) request.getParameter("token");
|
||||
if (StringUtils.hasText(requestToken)) {
|
||||
token = requestToken;
|
||||
}
|
||||
if (StringUtils.hasText(token)) {
|
||||
//验证token的合法性,根据自己的业务逻辑补充
|
||||
String result = HttpUtils.sendPost(Constants.VALIDATE_URL, "token=" + token);
|
||||
if (!StringUtils.isEmpty(result)) {
|
||||
|
53
src/main/java/com/hb/listener/ProgressListener.java
Normal file
53
src/main/java/com/hb/listener/ProgressListener.java
Normal file
@ -0,0 +1,53 @@
|
||||
package com.hb.listener;
|
||||
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.web.client.RequestCallback;
|
||||
import org.springframework.web.client.ResponseExtractor;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class ProgressListener implements RequestCallback, ResponseExtractor<Void> {
|
||||
|
||||
private long totalBytes;
|
||||
private long bytesRead;
|
||||
private MultipartFile file;
|
||||
|
||||
public ProgressListener(long totalBytes, MultipartFile file) {
|
||||
this.totalBytes = totalBytes;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doWithRequest(ClientHttpRequest request) throws IOException {
|
||||
// 将文件分块上传
|
||||
InputStream inputStream = file.getInputStream();
|
||||
OutputStream outputStream = request.getBody();
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
this.bytesRead += bytesRead;
|
||||
float progress = (float) this.bytesRead / totalBytes;
|
||||
System.out.println("上传进度:" + progress);
|
||||
// 可以在这里将进度信息保存到数据库或返回给前端
|
||||
// 将分块数据写入请求体
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
outputStream.flush();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Void extractData(org.springframework.http.client.ClientHttpResponse response) throws IOException {
|
||||
// 不需要实现任何逻辑
|
||||
return null;
|
||||
}
|
||||
|
||||
public float getProgress() {
|
||||
return (float) bytesRead / totalBytes;
|
||||
}
|
||||
|
||||
}
|
@ -17,178 +17,159 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Component
|
||||
public class MinioUtil {
|
||||
@Autowired
|
||||
@Autowired
|
||||
private MinioClient minioClient;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 创建一个桶
|
||||
*/
|
||||
public void createBucket(String bucket) throws Exception {
|
||||
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
|
||||
if (!found) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 创建一个桶
|
||||
*/
|
||||
public void createBucket(String bucket) throws Exception {
|
||||
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
|
||||
if (!found) {
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传一个文件
|
||||
*/
|
||||
public void uploadFile(InputStream stream, String bucket, String objectName) throws Exception {
|
||||
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
|
||||
.stream(stream, -1, 10485760).build());
|
||||
}
|
||||
/**
|
||||
* 上传一个文件
|
||||
*/
|
||||
public void uploadFile(InputStream stream, String bucket, String objectName) throws Exception {
|
||||
minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName).stream(stream, -1, 10485760).build());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 列出所有的桶
|
||||
*/
|
||||
public List<String> listBuckets() throws Exception {
|
||||
List<Bucket> list = minioClient.listBuckets();
|
||||
List<String> names = new ArrayList<>();
|
||||
list.forEach(b -> {
|
||||
names.add(b.name());
|
||||
});
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出所有的桶
|
||||
*/
|
||||
public List<String> listBuckets() throws Exception {
|
||||
List<Bucket> list = minioClient.listBuckets();
|
||||
List<String> names = new ArrayList<>();
|
||||
list.forEach(b -> {
|
||||
names.add(b.name());
|
||||
});
|
||||
return names;
|
||||
}
|
||||
/**
|
||||
* 列出一个桶中的所有文件和目录
|
||||
*/
|
||||
public List<Fileinfo> listFiles(String bucket) throws Exception {
|
||||
Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).recursive(true).build());
|
||||
|
||||
/**
|
||||
* 列出一个桶中的所有文件和目录
|
||||
*/
|
||||
public List<Fileinfo> listFiles(String bucket) throws Exception {
|
||||
Iterable<Result<Item>> results = minioClient.listObjects(
|
||||
ListObjectsArgs.builder().bucket(bucket).recursive(true).build());
|
||||
List<Fileinfo> infos = new ArrayList<>();
|
||||
results.forEach(r -> {
|
||||
Fileinfo info = new Fileinfo();
|
||||
try {
|
||||
Item item = r.get();
|
||||
info.setFilename(item.objectName());
|
||||
info.setDirectory(item.isDir());
|
||||
infos.add(info);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return infos;
|
||||
}
|
||||
|
||||
List<Fileinfo> infos = new ArrayList<>();
|
||||
results.forEach(r->{
|
||||
Fileinfo info = new Fileinfo();
|
||||
try {
|
||||
Item item = r.get();
|
||||
info.setFilename(item.objectName());
|
||||
info.setDirectory(item.isDir());
|
||||
infos.add(info);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return infos;
|
||||
}
|
||||
/**
|
||||
* 下载一个文件
|
||||
*/
|
||||
public InputStream download(String bucket, String objectName) throws Exception {
|
||||
InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
|
||||
return stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载一个文件
|
||||
*/
|
||||
public InputStream download(String bucket, String objectName) throws Exception {
|
||||
InputStream stream = minioClient.getObject(
|
||||
GetObjectArgs.builder().bucket(bucket).object(objectName).build());
|
||||
return stream;
|
||||
}
|
||||
/**
|
||||
* 删除一个桶
|
||||
*/
|
||||
public void deleteBucket(String bucket) throws Exception {
|
||||
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个桶
|
||||
*/
|
||||
public void deleteBucket(String bucket) throws Exception {
|
||||
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
/**
|
||||
* 删除一个对象
|
||||
*/
|
||||
public void deleteObject(String bucket, String objectName) throws Exception {
|
||||
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个对象
|
||||
*/
|
||||
public void deleteObject(String bucket, String objectName) throws Exception {
|
||||
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
|
||||
}
|
||||
/**
|
||||
* 生成直传链接
|
||||
*
|
||||
* @param fileDirName
|
||||
* @param bucket
|
||||
* @return
|
||||
*/
|
||||
public String getOneURL(String fileDirName, String bucket) throws Exception {
|
||||
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
|
||||
if (!found) {
|
||||
throw new RuntimeException("桶不存在!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成直传链接
|
||||
* @param fileDirName
|
||||
* @param bucket
|
||||
* @return
|
||||
*/
|
||||
public String getOneURL(String fileDirName, String bucket) throws Exception{
|
||||
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
|
||||
if (!found) {
|
||||
throw new RuntimeException("桶不存在!");
|
||||
}
|
||||
Map<String, String> reqParams = new HashMap<String, String>();
|
||||
reqParams.put("response-content-type", "application/json");
|
||||
return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.PUT) //这里必须是PUT,如果是GET的话就是文件访问地址了。如果是POST上传会报错.
|
||||
.bucket(bucket) // 存储桶
|
||||
.object(fileDirName) // 文件名
|
||||
.expiry(60 * 60 * 24).extraQueryParams(reqParams).build());
|
||||
}
|
||||
|
||||
Map<String, String> reqParams = new HashMap<String, String>();
|
||||
reqParams.put("response-content-type", "application/json");
|
||||
return minioClient.getPresignedObjectUrl(
|
||||
GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.PUT) //这里必须是PUT,如果是GET的话就是文件访问地址了。如果是POST上传会报错.
|
||||
.bucket(bucket) // 存储桶
|
||||
.object(fileDirName) // 文件名
|
||||
.expiry(60 * 60 * 24)
|
||||
.extraQueryParams(reqParams)
|
||||
.build());
|
||||
}
|
||||
public String getMinioURL(String bucket, String fileDirName) throws Exception {
|
||||
String key = bucket + ":" + fileDirName.replaceAll("/", ":");
|
||||
String url = (String) redisTemplate.opsForValue().get(key);
|
||||
if (StringUtils.isEmpty(url)) {
|
||||
int timeout = 60 * 60 * 24 * 7;
|
||||
GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucket).object(fileDirName).expiry(timeout, TimeUnit.SECONDS) //生成的预签名url可访问的有效时间,最大期限7天
|
||||
.build();
|
||||
url = minioClient.getPresignedObjectUrl(build);
|
||||
redisTemplate.opsForValue().set(key, url);
|
||||
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getMinioURL(String bucket, String fileDirName) throws Exception{
|
||||
String key = bucket + ":" + fileDirName.replaceAll("/", ":");
|
||||
String url = (String) redisTemplate.opsForValue().get(key);
|
||||
if (StringUtils.isEmpty(url)) {
|
||||
int timeout = 60 * 60 * 24 * 7;
|
||||
GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.GET)
|
||||
.bucket(bucket)
|
||||
.object(fileDirName)
|
||||
.expiry(timeout, TimeUnit.SECONDS) //生成的预签名url可访问的有效时间,最大期限7天
|
||||
.build();
|
||||
url = minioClient.getPresignedObjectUrl(build);
|
||||
redisTemplate.opsForValue().set(key, url);
|
||||
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
public List<String> getMinioURLs(String bucket, String[] fileDirNames) throws Exception {
|
||||
List<String> urlList = new ArrayList<>();
|
||||
for (String fileDirName : fileDirNames) {
|
||||
GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucket).object(fileDirName).expiry(60 * 60 * 24 * 7) //生成的预签名url可访问的有效时间,最大期限7天
|
||||
.build();
|
||||
String url = minioClient.getPresignedObjectUrl(build);
|
||||
urlList.add(url);
|
||||
}
|
||||
return urlList;
|
||||
}
|
||||
|
||||
public List<String> getMinioURLs(String bucket, String[] fileDirNames) throws Exception{
|
||||
List<String> urlList = new ArrayList<>();
|
||||
for (String fileDirName : fileDirNames) {
|
||||
GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.GET)
|
||||
.bucket(bucket)
|
||||
.object(fileDirName)
|
||||
.expiry(60 * 60 * 24 * 7) //生成的预签名url可访问的有效时间,最大期限7天
|
||||
.build();
|
||||
String url = minioClient.getPresignedObjectUrl(build);
|
||||
urlList.add(url);
|
||||
}
|
||||
return urlList;
|
||||
}
|
||||
public String getContenType(String bucket, String objectName) throws Exception {
|
||||
// 创建StatObjectArgs对象
|
||||
StatObjectArgs statObjectArgs = StatObjectArgs.builder().bucket(bucket).object(objectName).build();
|
||||
|
||||
public String getContenType(String bucket, String objectName) throws Exception{
|
||||
// 创建StatObjectArgs对象
|
||||
StatObjectArgs statObjectArgs = StatObjectArgs.builder()
|
||||
.bucket(bucket)
|
||||
.object(objectName)
|
||||
.build();
|
||||
// 获取对象信息
|
||||
StatObjectResponse statObjectResponse = minioClient.statObject(statObjectArgs);
|
||||
|
||||
// 获取对象信息
|
||||
StatObjectResponse statObjectResponse = minioClient.statObject(statObjectArgs);
|
||||
if (statObjectResponse == null) {
|
||||
throw new RuntimeException("文件不存在!");
|
||||
}
|
||||
|
||||
if (statObjectResponse == null) {
|
||||
throw new RuntimeException("文件不存在!");
|
||||
}
|
||||
// 获取文件类型
|
||||
String contentType = statObjectResponse.contentType();
|
||||
System.out.println("文件类型:" + contentType);
|
||||
return contentType;
|
||||
}
|
||||
|
||||
// 获取文件类型
|
||||
String contentType = statObjectResponse.contentType();
|
||||
System.out.println("文件类型:" + contentType);
|
||||
return contentType;
|
||||
}
|
||||
public void uploadFile2(MultipartFile file, String bucket, String fileDirName) throws Exception {
|
||||
|
||||
public void uploadFile2(MultipartFile file, String bucket, String fileDirName) throws Exception{
|
||||
Long fileSize = file.getSize();
|
||||
String fileType = file.getContentType();
|
||||
String fileName = file.getOriginalFilename();
|
||||
|
||||
Long fileSize = file.getSize();
|
||||
String fileType = file.getContentType();
|
||||
String fileName = file.getOriginalFilename();
|
||||
|
||||
minioClient.putObject(PutObjectArgs.builder()
|
||||
.bucket(bucket) // 存储桶
|
||||
.object(fileDirName) // 文件名
|
||||
.stream(file.getInputStream(), fileSize, -1) // 文件内容
|
||||
.contentType(fileType) // 文件类型
|
||||
.build());
|
||||
}
|
||||
minioClient.putObject(PutObjectArgs.builder().bucket(bucket) // 存储桶
|
||||
.object(fileDirName) // 文件名
|
||||
.stream(file.getInputStream(), fileSize, -1) // 文件内容
|
||||
.contentType(fileType) // 文件类型
|
||||
.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
57
src/main/resources/application-dev.yml
Normal file
57
src/main/resources/application-dev.yml
Normal file
@ -0,0 +1,57 @@
|
||||
spring:
|
||||
datasource:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
druid:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://8.134.76.66:3306/hb-iot?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||
username: root
|
||||
password: hbdbadmin@
|
||||
initial-size: 10
|
||||
max-active: 100
|
||||
min-idle: 10
|
||||
max-wait: 60000
|
||||
pool-prepared-statements: true
|
||||
max-pool-prepared-statement-per-connection-size: 20
|
||||
time-between-eviction-runs-millis: 60000
|
||||
min-evictable-idle-time-millis: 300000
|
||||
#Oracle需要打开注释
|
||||
#validation-query: SELECT 1 FROM DUAL
|
||||
test-while-idle: true
|
||||
test-on-borrow: false
|
||||
test-on-return: false
|
||||
stat-view-servlet:
|
||||
enabled: true
|
||||
url-pattern: /druid/*
|
||||
#login-username: admin
|
||||
#login-password: admin
|
||||
filter:
|
||||
stat:
|
||||
log-slow-sql: true
|
||||
slow-sql-millis: 1000
|
||||
merge-sql: false
|
||||
wall:
|
||||
config:
|
||||
multi-statement-allow: true
|
||||
|
||||
|
||||
##多数据源的配置,需要引用hb-dynamic-datasource
|
||||
#dynamic:
|
||||
# datasource:
|
||||
# slave1:
|
||||
# driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
|
||||
# url: jdbc:sqlserver://localhost:1433;DatabaseName=hb_security
|
||||
# username: sa
|
||||
# password: 123456
|
||||
# slave2:
|
||||
# driver-class-name: org.postgresql.Driver
|
||||
# url: jdbc:postgresql://localhost:5432/hb_security
|
||||
# username: hb
|
||||
# password: 123456
|
||||
|
||||
#ureport配置
|
||||
ureport:
|
||||
disableHttpSessionReportCache: false
|
||||
disableFileProvider: false
|
||||
fileStoreDir: D:/ureportfiles
|
||||
# fileStoreDir: /mnt/soft/ureportfiles
|
||||
debug: true
|
@ -1,18 +1,29 @@
|
||||
server:
|
||||
port: 8888
|
||||
ssl:
|
||||
# 证书文件名
|
||||
key-store: C:\Users\ccy\Desktop\cret\server.pkcs12
|
||||
# 密钥口令
|
||||
key-store-password: hb123456
|
||||
# 证书类型
|
||||
key-store-type: pkcs12
|
||||
|
||||
|
||||
logging:
|
||||
config: classpath:log4j2.xml
|
||||
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
servlet:
|
||||
multipart:
|
||||
max-file-size: 100MB
|
||||
max-request-size: 1000MB
|
||||
max-file-size: 102400MB
|
||||
max-request-size: 102400MB
|
||||
|
||||
# redis 配置
|
||||
redis:
|
||||
# 地址
|
||||
host: 192.168.12.71
|
||||
host: 192.168.15.12
|
||||
# 端口,默认为6379
|
||||
port: 6379
|
||||
# 数据库索引
|
||||
@ -34,7 +45,7 @@ spring:
|
||||
|
||||
#minio配置
|
||||
minio:
|
||||
url: https://192.168.12.71:9000 #对象存储服务的URL
|
||||
url: https://192.168.15.12:9000 #对象存储服务的URL
|
||||
accessKey: QVoCEhy0xek0pquP #Access key账户
|
||||
secretKey: g89JlYE4Nnii66g6JmGRoSF7S4bc0hm6 #Secret key密码
|
||||
|
||||
|
BIN
src/main/resources/server.pkcs12
Normal file
BIN
src/main/resources/server.pkcs12
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user