Commit ac993fce by 吴迪

磁盘映射、文件上传

parent 4af53a29
package io.office.common.enumpack;
/**
*
* 
 @description:
*
* @author wudi
* @date 22:08 2021/10/12
*/
public enum ErrorCodeEnum {
SUCCESS(0, "成功"),//成功
FAIL(-1, "失败"),//失败
FILE_IS_NULL(10001,"上传文件不能为空!");
private Integer code;
private String msg;
ErrorCodeEnum(int code) {
this.code = code;
}
ErrorCodeEnum(Integer code, String message) {
this.code = code;
this.msg = message;
}
public Integer code() {
return this.code;
}
public String message() {
return this.msg;
}
}
......@@ -8,6 +8,8 @@
package io.office.common.exception;
import io.office.common.enumpack.ErrorCodeEnum;
/**
* 自定义异常
*
......@@ -41,6 +43,12 @@ public class RRException extends RuntimeException {
this.code = code;
}
public RRException(ErrorCodeEnum errorCodeEnum) {
super(errorCodeEnum.message());
this.code = errorCodeEnum.code();
this.msg = errorCodeEnum.message();
}
public String getMsg() {
return msg;
}
......
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package io.office.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//加了ApiOperation注解的类,才生成接口文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//包下的类,才生成接口文档
//.apis(RequestHandlerSelectors.basePackage("io.renren.controller"))
.paths(PathSelectors.any())
.build()
.securitySchemes(security());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("人人开源")
.description("renren-fast文档")
.termsOfServiceUrl("https://www.renren.io")
.version("3.0.0")
.build();
}
private List<ApiKey> security() {
return newArrayList(
new ApiKey("token", "token", "header")
);
}
}
\ No newline at end of file
///**
// * Copyright (c) 2016-2019 人人开源 All rights reserved.
// *
// * https://www.renren.io
// *
// * 版权所有,侵权必究!
// */
//
//package io.office.config;
//
//import io.swagger.annotations.ApiOperation;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//import springfox.documentation.builders.ApiInfoBuilder;
//import springfox.documentation.builders.PathSelectors;
//import springfox.documentation.builders.RequestHandlerSelectors;
//import springfox.documentation.service.ApiInfo;
//import springfox.documentation.service.ApiKey;
//import springfox.documentation.spi.DocumentationType;
//import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
//
//import java.util.List;
//
//import static com.google.common.collect.Lists.newArrayList;
//
//@Configuration
//@EnableSwagger2
//public class SwaggerConfig implements WebMvcConfigurer {
//
// /* @Bean
// public Docket createRestApi() {
// return new Docket(DocumentationType.SWAGGER_2)
// .apiInfo(apiInfo())
// .select()
// //加了ApiOperation注解的类,才生成接口文档
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// //包下的类,才生成接口文档
// //.apis(RequestHandlerSelectors.basePackage("io.renren.controller"))
// .paths(PathSelectors.any())
// .build()
// .securitySchemes(security());
// }
//
// private ApiInfo apiInfo() {
// return new ApiInfoBuilder()
// .title("人人开源")
// .description("renren-fast文档")
// .termsOfServiceUrl("https://www.renren.io")
// .version("3.0.0")
// .build();
// }
//
// private List<ApiKey> security() {
// return newArrayList(
// new ApiKey("token", "token", "header")
// );
// }*/
//
//}
\ No newline at end of file
package io.office.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* TODO
*
* @author wudi
* @version 1.0
* @date 2021/3/24 9:44
*/
//@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
}
......@@ -11,9 +11,11 @@ package io.office.modules.app.config;
import io.office.modules.app.interceptor.AuthorizationInterceptor;
import io.office.modules.app.resolver.LoginUserHandlerMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
......@@ -25,6 +27,11 @@ import java.util.List;
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Value("${request.upload.file.prefix}")
private String requestUploadFilePrefix;
@Value("${file.save.path}")
private String fileSavePath;
@Autowired
private AuthorizationInterceptor authorizationInterceptor;
@Autowired
......@@ -39,4 +46,15 @@ public class WebMvcConfig implements WebMvcConfigurer {
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(loginUserHandlerMethodArgumentResolver);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//注意前面要加file,不然是访问不了的
registry.addResourceHandler("/" + requestUploadFilePrefix + "/**").addResourceLocations("file" +
":" + fileSavePath);
}
}
\ No newline at end of file
package io.office.modules.manage.controller;
import io.office.common.utils.R;
import io.office.modules.manage.utils.UploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
*
* 
 @description:
*
* @author wudi
* @date 22:03 2021/10/12
*/
@RestController
@RequestMapping(value = "/file")
@Slf4j
public class FileController {
@Autowired
private UploadUtils uploadUtils;
@PostMapping("/uploadFile")
public R uploadFile(@RequestParam("file") MultipartFile file) {
return R.ok().put("data",uploadUtils.fileUpload(file));
}
}
package io.office.modules.manage.service.impl;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
......@@ -16,14 +17,20 @@ import io.office.modules.manage.service.NewsColumnService;
@Service("newsColumnService")
public class NewsColumnServiceImpl extends ServiceImpl<NewsColumnDao, NewsColumnEntity> implements NewsColumnService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<NewsColumnEntity> page = this.page(
new Query<NewsColumnEntity>().getPage(params),
new QueryWrapper<NewsColumnEntity>()
);
return new PageUtils(page);
}
}
\ No newline at end of file
package io.office.modules.manage.utils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.office.common.enumpack.ErrorCodeEnum;
import io.office.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* TODO
*
* @author wudi
* @version 1.0
* @date 2021/3/19 18:50
*/
@Slf4j
@Component
public class UploadUtils {
@Value("${file.save.path}")
private String fileSavePath;
/* @Value("${file.save.type}")
private List fileSaveTypeList;*/
@Value("${file.request.prefix}")
private String fileRequestPrefix;
/**
* @param file 上传的文件
* @return
* @描述:上传文件到临时目录
*/
public String fileUpload(MultipartFile file) {
if (file == null) {
throw new RRException(ErrorCodeEnum.FILE_IS_NULL);
}
String filename = "";
//String originalFilename = file.getOriginalFilename();
filename = file.getOriginalFilename();
File fileDir = new File(fileSavePath + DateUtils.formatDateToString(new Date(), DateUtils.FORMAT4) + "/");
if (!fileDir.exists()) {
fileDir.mkdirs();
}
String dateDirPath = DateUtils.formatDateToString(new Date(), DateUtils.FORMAT4);
String returnFilename = fileRequestPrefix + dateDirPath +"/" + filename;
filename = fileSavePath + dateDirPath +"/" + filename;
File dest = new File(filename);
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
return returnFilename;
}
/**
* 根据全路径名获取businessId
* "http://10.0.20.46:6051/emgcysys/uploadFile/2021-03-23/1374288822837604352.JAVA核心面试知识整理.pdf",
*/
// public static void main(String[] args) {
// System.out.println(getBusinessIdByFullPath("http://10.0.20.46:6051/emgcysys/uploadFile/2021-03-23" +
// "/1374288822837604352.JAVA核心面试知识整理.pdf"));
// }
}
......@@ -48,3 +48,14 @@ spring:
server:
port: 9091
# password: 123456
#自定义文件上传路径
request:
upload:
file:
prefix: uploadFileResource
file:
save:
path: F:/
request:
prefix: http://localhost:9091/office/uploadFileResource/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment