Skip to content

Commit b29e410

Browse files
committedJul 11, 2019
【修改】增加文件上传接口
1 parent 01e5787 commit b29e410

File tree

6 files changed

+319
-170
lines changed

6 files changed

+319
-170
lines changed
 

‎pom.xml

+176-170
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,194 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>org.springframework.boot</groupId>
7-
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.1.6.RELEASE</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10-
</parent>
11-
<groupId>com.shellming</groupId>
12-
<artifactId>ele-service</artifactId>
13-
<version>0.0.1-SNAPSHOT</version>
14-
<name>ele-service</name>
15-
<description>Demo project for Spring Boot</description>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.6.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.shellming</groupId>
12+
<artifactId>ele-service</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>ele-service</name>
15+
<description>Demo project for Spring Boot</description>
1616

17-
<properties>
18-
<java.version>1.8</java.version>
19-
</properties>
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
2020

21-
<dependencies>
22-
<dependency>
23-
<groupId>org.springframework.boot</groupId>
24-
<artifactId>spring-boot-configuration-processor</artifactId>
25-
<optional>true</optional>
26-
</dependency>
27-
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
28-
<dependency>
29-
<groupId>mysql</groupId>
30-
<artifactId>mysql-connector-java</artifactId>
31-
<version>5.1.47</version>
32-
</dependency>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-configuration-processor</artifactId>
25+
<optional>true</optional>
26+
</dependency>
27+
<!-- 文件上传-->
28+
<dependency>
29+
<groupId>commons-fileupload</groupId>
30+
<artifactId>commons-fileupload</artifactId>
31+
<version>1.3.3</version>
32+
</dependency>
33+
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
34+
<dependency>
35+
<groupId>mysql</groupId>
36+
<artifactId>mysql-connector-java</artifactId>
37+
<version>5.1.47</version>
38+
</dependency>
3339

34-
<!--mybatis.generator-->
35-
<dependency>
36-
<groupId>org.mybatis.generator</groupId>
37-
<artifactId>mybatis-generator-core</artifactId>
38-
<version>1.3.5</version>
39-
</dependency>
40+
<!--mybatis.generator-->
41+
<dependency>
42+
<groupId>org.mybatis.generator</groupId>
43+
<artifactId>mybatis-generator-core</artifactId>
44+
<version>1.3.5</version>
45+
</dependency>
4046

41-
<!-- swagger -->
42-
<dependency>
43-
<groupId>io.springfox</groupId>
44-
<artifactId>springfox-swagger2</artifactId>
45-
<version>2.5.0</version>
46-
</dependency>
47-
<!-- swagger-ui -->
48-
<dependency>
49-
<groupId>io.springfox</groupId>
50-
<artifactId>springfox-swagger-ui</artifactId>
51-
<version>2.5.0</version>
52-
</dependency>
47+
<!-- swagger -->
48+
<dependency>
49+
<groupId>io.springfox</groupId>
50+
<artifactId>springfox-swagger2</artifactId>
51+
<version>2.5.0</version>
52+
</dependency>
53+
<!-- swagger-ui -->
54+
<dependency>
55+
<groupId>io.springfox</groupId>
56+
<artifactId>springfox-swagger-ui</artifactId>
57+
<version>2.5.0</version>
58+
</dependency>
5359

54-
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
55-
<dependency>
56-
<groupId>org.apache.commons</groupId>
57-
<artifactId>commons-lang3</artifactId>
58-
<version>3.9</version>
59-
</dependency>
60+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
61+
<dependency>
62+
<groupId>org.apache.commons</groupId>
63+
<artifactId>commons-lang3</artifactId>
64+
<version>3.9</version>
65+
</dependency>
6066

61-
<!-- 分页 -->
67+
<!-- 分页 -->
6268
<dependency>
6369
<groupId>com.github.pagehelper</groupId>
6470
<artifactId>pagehelper</artifactId>
6571
<version>5.1.2</version>
6672
</dependency>
67-
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-autoconfigure -->
68-
<dependency>
69-
<groupId>com.github.pagehelper</groupId>
70-
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
71-
<version>1.2.3</version>
72-
</dependency>
73+
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-autoconfigure -->
74+
<dependency>
75+
<groupId>com.github.pagehelper</groupId>
76+
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
77+
<version>1.2.3</version>
78+
</dependency>
7379

74-
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
75-
<dependency>
76-
<groupId>com.github.pagehelper</groupId>
77-
<artifactId>pagehelper-spring-boot-starter</artifactId>
78-
<version>1.2.3</version>
79-
</dependency>
80-
<!--JWT-->
81-
<dependency>
82-
<groupId>io.jsonwebtoken</groupId>
83-
<artifactId>jjwt</artifactId>
84-
<version>0.9.0</version>
85-
</dependency>
86-
<!--Redis-->
87-
<dependency>
88-
<groupId>org.springframework.boot</groupId>
89-
<artifactId>spring-boot-starter-data-redis</artifactId>
90-
</dependency>
91-
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
92-
<dependency>
93-
<groupId>org.apache.commons</groupId>
94-
<artifactId>commons-pool2</artifactId>
95-
<version>2.6.2</version>
96-
</dependency>
97-
<!-- https://mvnrepository.com/artifact/commons-io/commons-io 文件工具类 -->
98-
<dependency>
99-
<groupId>commons-io</groupId>
100-
<artifactId>commons-io</artifactId>
101-
<version>2.6</version>
102-
</dependency>
103-
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
104-
<dependency>
105-
<groupId>net.sf.json-lib</groupId>
106-
<artifactId>json-lib</artifactId>
107-
<version>2.4</version>
108-
<classifier>jdk15</classifier>
109-
</dependency>
110-
<dependency>
111-
<groupId>com.alibaba</groupId>
112-
<artifactId>fastjson</artifactId>
113-
<version>1.2.49</version>
114-
</dependency>
115-
<dependency>
116-
<groupId>org.springframework.boot</groupId>
117-
<artifactId>spring-boot-starter-web</artifactId>
118-
</dependency>
119-
<dependency>
120-
<groupId>org.mybatis.spring.boot</groupId>
121-
<artifactId>mybatis-spring-boot-starter</artifactId>
122-
<version>2.0.1</version>
123-
</dependency>
80+
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
81+
<dependency>
82+
<groupId>com.github.pagehelper</groupId>
83+
<artifactId>pagehelper-spring-boot-starter</artifactId>
84+
<version>1.2.3</version>
85+
</dependency>
86+
<!--JWT-->
87+
<dependency>
88+
<groupId>io.jsonwebtoken</groupId>
89+
<artifactId>jjwt</artifactId>
90+
<version>0.9.0</version>
91+
</dependency>
92+
<!--Redis-->
93+
<dependency>
94+
<groupId>org.springframework.boot</groupId>
95+
<artifactId>spring-boot-starter-data-redis</artifactId>
96+
</dependency>
97+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
98+
<dependency>
99+
<groupId>org.apache.commons</groupId>
100+
<artifactId>commons-pool2</artifactId>
101+
<version>2.6.2</version>
102+
</dependency>
103+
<!-- https://mvnrepository.com/artifact/commons-io/commons-io 文件工具类 -->
104+
<dependency>
105+
<groupId>commons-io</groupId>
106+
<artifactId>commons-io</artifactId>
107+
<version>2.6</version>
108+
</dependency>
109+
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
110+
<dependency>
111+
<groupId>net.sf.json-lib</groupId>
112+
<artifactId>json-lib</artifactId>
113+
<version>2.4</version>
114+
<classifier>jdk15</classifier>
115+
</dependency>
116+
<dependency>
117+
<groupId>com.alibaba</groupId>
118+
<artifactId>fastjson</artifactId>
119+
<version>1.2.49</version>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.springframework.boot</groupId>
123+
<artifactId>spring-boot-starter-web</artifactId>
124+
</dependency>
125+
<dependency>
126+
<groupId>org.mybatis.spring.boot</groupId>
127+
<artifactId>mybatis-spring-boot-starter</artifactId>
128+
<version>2.0.1</version>
129+
</dependency>
124130

125-
<dependency>
126-
<groupId>org.springframework.boot</groupId>
127-
<artifactId>spring-boot-devtools</artifactId>
128-
<scope>runtime</scope>
129-
<optional>true</optional>
130-
</dependency>
131-
<dependency>
132-
<groupId>org.projectlombok</groupId>
133-
<artifactId>lombok</artifactId>
134-
<optional>true</optional>
135-
</dependency>
136-
<dependency>
137-
<groupId>org.springframework.boot</groupId>
138-
<artifactId>spring-boot-starter-test</artifactId>
139-
<scope>test</scope>
140-
</dependency>
141-
<dependency>
142-
<groupId>org.springframework.boot</groupId>
143-
<artifactId>spring-boot-starter-data-jpa</artifactId>
144-
</dependency>
145-
<dependency>
146-
<groupId>org.springframework.boot</groupId>
147-
<artifactId>spring-boot-starter-data-jpa</artifactId>
148-
</dependency>
149-
</dependencies>
131+
<dependency>
132+
<groupId>org.springframework.boot</groupId>
133+
<artifactId>spring-boot-devtools</artifactId>
134+
<scope>runtime</scope>
135+
<optional>true</optional>
136+
</dependency>
137+
<dependency>
138+
<groupId>org.projectlombok</groupId>
139+
<artifactId>lombok</artifactId>
140+
<optional>true</optional>
141+
</dependency>
142+
<dependency>
143+
<groupId>org.springframework.boot</groupId>
144+
<artifactId>spring-boot-starter-test</artifactId>
145+
<scope>test</scope>
146+
</dependency>
147+
<dependency>
148+
<groupId>org.springframework.boot</groupId>
149+
<artifactId>spring-boot-starter-data-jpa</artifactId>
150+
</dependency>
151+
<dependency>
152+
<groupId>org.springframework.boot</groupId>
153+
<artifactId>spring-boot-starter-data-jpa</artifactId>
154+
</dependency>
155+
</dependencies>
150156

151-
<build>
152-
<plugins>
153-
<plugin>
154-
<groupId>org.springframework.boot</groupId>
155-
<artifactId>spring-boot-maven-plugin</artifactId>
156-
</plugin>
157-
</plugins>
158-
<resources>
159-
<resource>
160-
<directory>src/main/java</directory>
161-
<includes>
162-
<include>**/*.yml</include>
163-
<include>**/*.properties</include>
164-
<include>**/*.xml</include>
165-
<include>**/*.json</include>
166-
</includes>
167-
<filtering>false</filtering>
168-
</resource>
169-
<resource>
170-
<directory>src/main/resources</directory>
171-
<includes>
172-
<include>**/*.yml</include>
173-
<include>**/*.properties</include>
174-
<include>**/*.json</include>
175-
<include>**/*.xml</include>
176-
</includes>
177-
<filtering>false</filtering>
178-
</resource>
179-
</resources>
180-
</build>
157+
<build>
158+
<plugins>
159+
<plugin>
160+
<groupId>org.springframework.boot</groupId>
161+
<artifactId>spring-boot-maven-plugin</artifactId>
162+
</plugin>
163+
</plugins>
164+
<resources>
165+
<resource>
166+
<directory>src/main/java</directory>
167+
<includes>
168+
<include>**/*.yml</include>
169+
<include>**/*.properties</include>
170+
<include>**/*.xml</include>
171+
<include>**/*.json</include>
172+
</includes>
173+
<filtering>false</filtering>
174+
</resource>
175+
<resource>
176+
<directory>src/main/resources</directory>
177+
<includes>
178+
<include>**/*.yml</include>
179+
<include>**/*.properties</include>
180+
<include>**/*.json</include>
181+
<include>**/*.xml</include>
182+
</includes>
183+
<filtering>false</filtering>
184+
</resource>
185+
</resources>
186+
</build>
181187

182-
<repositories>
183-
<repository>
184-
<id>aliyunmaven</id>
185-
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
186-
</repository>
187-
</repositories>
188+
<repositories>
189+
<repository>
190+
<id>aliyunmaven</id>
191+
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
192+
</repository>
193+
</repositories>
188194
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.shellming.eleservice.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
7+
8+
/**
9+
* * 设置虚拟路径,访问绝对路径下资源
10+
*/
11+
@Configuration
12+
public class UploadFilePathConfig implements WebMvcConfigurer {
13+
14+
@Value("${file.staticAccessPath}")
15+
private String staticAccessPath;
16+
17+
@Value("${file.uploadFolder}")
18+
private String uploadFolder;
19+
20+
@Override
21+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
22+
registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder);
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.shellming.eleservice.controller;
2+
3+
4+
import com.alibaba.fastjson.JSON;
5+
import com.shellming.eleservice.common.ResultBean;
6+
import com.shellming.eleservice.service.impl.FileUploadServiceImpl;
7+
import io.swagger.annotations.Api;
8+
import io.swagger.annotations.ApiOperation;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.apache.commons.io.FileUtils;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.RestController;
15+
import org.springframework.web.multipart.MultipartFile;
16+
17+
import javax.servlet.http.HttpServletRequest;
18+
import javax.servlet.http.HttpServletResponse;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.util.*;
22+
23+
@Api(value = "文件上传", description = "文件上传")
24+
@RequestMapping("/upload")
25+
@RestController
26+
@Slf4j
27+
public class FileUploadController {
28+
29+
@Autowired
30+
private FileUploadServiceImpl fileUploadService;
31+
32+
@ApiOperation(value = "上传")
33+
@RequestMapping(value = "", method = RequestMethod.POST)
34+
public ResultBean uploadApk(MultipartFile[] files, HttpServletRequest request, HttpServletResponse response) throws Exception{
35+
log.info("上传{}", files.length);
36+
List result = new ArrayList();
37+
String path = request.getContextPath();
38+
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
39+
40+
for (MultipartFile file : files) {
41+
ResultBean ret = fileUploadService.upload(file, path, basePath);
42+
if (ret.isSuccess()) {
43+
Object data = ret.getData();
44+
result.add(data);
45+
} else {
46+
log.info(ret.getMsg());
47+
}
48+
}
49+
return ResultBean.success(result);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.shellming.eleservice.service;
2+
3+
import com.shellming.eleservice.common.ResultBean;
4+
import org.springframework.web.multipart.MultipartFile;
5+
6+
public interface IFileUploadService {
7+
ResultBean upload(MultipartFile myfiles, String path, String basePath) throws Exception;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.shellming.eleservice.service.impl;
2+
3+
import com.shellming.eleservice.common.ResultBean;
4+
import com.shellming.eleservice.service.IFileUploadService;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.apache.commons.io.FileUtils;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.web.multipart.MultipartFile;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.util.Date;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
16+
@Service
17+
@Slf4j
18+
public class FileUploadServiceImpl implements IFileUploadService {
19+
20+
@Override
21+
public ResultBean upload(MultipartFile myfiles, String path, String basePath) throws Exception {
22+
if (myfiles.getSize() > 1024 * 1024 * 5) {
23+
return ResultBean.fail("文件过大,请上传5M以内的图片");
24+
}
25+
Date dt = new Date();
26+
Long time = dt.getTime();
27+
28+
if (myfiles == null) {
29+
return ResultBean.fail("未选择文件");
30+
}
31+
32+
String realPath = "d://uploadFiles/"; // 获取保存的路径,本地磁盘中的一个文件夹
33+
if (myfiles.isEmpty()) {
34+
// 未选择文件
35+
return ResultBean.fail("未选择文件");
36+
} else {
37+
// 文件原名称
38+
String originFileName = "";
39+
// 上传文件重命名
40+
String originalFilename = time.toString().substring(time.toString().length() - 8,
41+
time.toString().length());
42+
originalFilename = originalFilename.concat(".");
43+
originalFilename = originalFilename.concat(myfiles.getOriginalFilename().toString()
44+
.substring(myfiles.getOriginalFilename().toString().indexOf(".") + 1));
45+
// 这里使用Apache的FileUtils方法来进行保存
46+
FileUtils.copyInputStreamToFile(myfiles.getInputStream(), new File(realPath, originalFilename));
47+
Map resMap = new HashMap();
48+
resMap.put("filename", originalFilename);
49+
resMap.put("path", basePath + "/static/image/" + originalFilename);
50+
resMap.put("relativePath", "/static/image/" + originalFilename);
51+
return ResultBean.success(resMap);
52+
}
53+
}
54+
}

‎src/main/resources/application.yml

+5
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@ redis:
6262
host: 127.0.0.1
6363
port: 6379
6464
password: ""
65+
66+
#file
67+
file:
68+
staticAccessPath: /static/image/**
69+
uploadFolder: d://uploadFiles/

0 commit comments

Comments
 (0)