提交 355b0fe5 authored 作者: 宋文杰's avatar 宋文杰

feat(emergency plan): add the restful api for emergency plan file upload

上级 88e76940
...@@ -6,9 +6,15 @@ import com.elephant.framework.galaxy.aroad.response.R; ...@@ -6,9 +6,15 @@ import com.elephant.framework.galaxy.aroad.response.R;
import com.elephant.framework.galaxy.aroad.module.common.annotation.SysLog; import com.elephant.framework.galaxy.aroad.module.common.annotation.SysLog;
import com.elephant.framework.galaxy.aroad.module.emergencyplan.entity.EmergencyPlan; import com.elephant.framework.galaxy.aroad.module.emergencyplan.entity.EmergencyPlan;
import com.elephant.framework.galaxy.aroad.module.emergencyplan.service.EmergencyPlanService; import com.elephant.framework.galaxy.aroad.module.emergencyplan.service.EmergencyPlanService;
import com.elephant.framework.galaxy.aroad.response.UpFileResponse;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController; import com.elephant.framework.galaxy.aroad.module.common.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/** /**
...@@ -79,4 +85,42 @@ public class EmergencyPlanController extends BaseController { ...@@ -79,4 +85,42 @@ public class EmergencyPlanController extends BaseController {
return new R<>(emergencyPlanService.removeById(planId)); return new R<>(emergencyPlanService.removeById(planId));
} }
@PostMapping("/upload")
@ResponseBody
public UpFileResponse upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return null;
}
// 获取文件名
String fileName = file.getOriginalFilename();
// 获取文件的后缀名
// String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 文件上传路径
String filePath = "./src/main/resources/images/";
// 解决中文问题,liunx 下中文路径,图片显示问题
//fileName = UUID.randomUUID() + suffixName;
File dest = new File(filePath + fileName);
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
// file.transferTo(dest);
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file.getBytes());
out.flush();
out.close();
return UpFileResponse.builder()
.name(fileName)
.url("/images/"+fileName)
.build();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null; }
} }
package com.elephant.framework.galaxy.aroad.response;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
/**
* @program:
* @description:
* @author: songwenjie
* @create: 2019-04-25 10:34
**/
@Data
@Builder
public class UpFileResponse implements Serializable {
/**
* 文件名称
*/
private String name;
/**
* 文件路径
*/
private String url;
}
...@@ -3,6 +3,7 @@ package com.elephant.framework.galaxy.aroad.security.config; ...@@ -3,6 +3,7 @@ package com.elephant.framework.galaxy.aroad.security.config;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
...@@ -28,4 +29,6 @@ public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter ...@@ -28,4 +29,6 @@ public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter
.disable(); .disable();
} }
} }
...@@ -30,7 +30,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ...@@ -30,7 +30,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
.loginProcessingUrl("/token/form") .loginProcessingUrl("/token/form")
.and() .and()
.authorizeRequests() .authorizeRequests()
.antMatchers("/token/**").permitAll() .antMatchers(new String[]{"/token/**","/images/**"}).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and().csrf().disable(); .and().csrf().disable();
} }
...@@ -43,6 +43,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ...@@ -43,6 +43,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
*/ */
@Override @Override
public void configure(WebSecurity web) throws Exception { public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/images/**");
} }
@Bean @Bean
......
server: server:
port: 9999 port: 8989
spring: spring:
mvc:
static-path-pattern: /images/*
resources:
static-locations: classpath:images/
servlet:
multipart:
enabled: true
max-file-size: 512000 # 最大支持文件大小
max-request-size: 512000 # 最大支持请求大小
datasource: datasource:
driver-class-name: com.mysql.jdbc.Driver driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/aroad?useUnicode=true&characterEncoding=utf-8&useSSL=false url: jdbc:mysql://127.0.0.1:3306/aroad?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root username: root
password: root password: 123456
cache: cache:
type: redis type: redis
redis: redis:
...@@ -26,6 +35,7 @@ spring: ...@@ -26,6 +35,7 @@ spring:
max-wait: -1ms max-wait: -1ms
max-active: 8 max-active: 8
shutdown-timeout: 100ms shutdown-timeout: 100ms
mybatis-plus: mybatis-plus:
mapper-locations: classpath:/mapper/*/*.xml mapper-locations: classpath:/mapper/*/*.xml
configuration: configuration:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论