Skip to content

Commit 36d8fee

Browse files
author
liming
committed
first commit
0 parents  commit 36d8fee

File tree

17 files changed

+992
-0
lines changed

17 files changed

+992
-0
lines changed

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/
34+
/uploads

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## 輕量級文件管理系統(开箱即用)
2+
3+
### 使用场景:
4+
5+
对于需要充当临时网络文件服务器。省去了其余不需要的功能。仅保留基础功能!
6+
7+
### 依赖说明:
8+
9+
| | 源码版本 | jar包版本 |
10+
| ----- | -------- | --------- |
11+
| JDK | 1.8以上 | 1.8以上 |
12+
| maven | 需要 | 不需要 |
13+
14+
###
15+
16+
### 效果图:
17+
18+
![image-20211117133653253](./image-20211117133653253.png)
19+
20+
###
21+
22+
### 功能说明:
23+
24+
| 上传 ||
25+
| :------: | :--: |
26+
| 下载 ||
27+
| 删除 ||
28+
| 查看 ||
29+
| 进度查看 ||
30+
31+
32+
33+
### 使用說明:
34+
35+
36+
#### jar包版本
37+
38+
```java
39+
1.下载对应的`jar`包
40+
41+
2.java -jar file_system-0.0.1-SNAPSHOT.jar
42+
43+
3.浏览器访问 http://127.0.0.1:8081即可进行操作
44+
```
45+
46+
47+
48+
49+
50+
#### 源代码版本
51+
52+
```
53+
1. git clone https://github.com/q920447939/file_system
54+
2. mvn clean package
55+
3. java -jar file_system-0.0.1-SNAPSHOT.jar
56+
4. 浏览器访问 http://127.0.0.1:8081即可进行操作
57+
```
58+

image-20211117133653253.png

21.3 KB
Loading

pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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 https://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.5.6</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>cn.withmes</groupId>
12+
<artifactId>file_system</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>file_system</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
<!-- thymeleaf -->
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.projectlombok</groupId>
31+
<artifactId>lombok</artifactId>
32+
<optional>true</optional>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-devtools</artifactId>
42+
<optional>true</optional>
43+
<scope>true</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
<configuration>
53+
<excludes>
54+
<exclude>
55+
<groupId>org.projectlombok</groupId>
56+
<artifactId>lombok</artifactId>
57+
</exclude>
58+
</excludes>
59+
</configuration>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
64+
</project>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* @Project:
3+
* @Author: leegoo
4+
* @Date: 2021年11月16日
5+
*/
6+
package cn.withmes.file.system;
7+
8+
import lombok.Builder;
9+
import lombok.Getter;
10+
import lombok.Setter;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.core.io.Resource;
15+
import org.springframework.http.HttpHeaders;
16+
import org.springframework.http.MediaType;
17+
import org.springframework.http.ResponseEntity;
18+
import org.springframework.stereotype.Controller;
19+
import org.springframework.web.bind.annotation.CrossOrigin;
20+
import org.springframework.web.bind.annotation.GetMapping;
21+
import org.springframework.web.bind.annotation.PathVariable;
22+
import org.springframework.web.bind.annotation.PostMapping;
23+
import org.springframework.web.bind.annotation.RequestMapping;
24+
import org.springframework.web.bind.annotation.RequestMethod;
25+
import org.springframework.web.bind.annotation.RequestParam;
26+
import org.springframework.web.bind.annotation.ResponseBody;
27+
import org.springframework.web.multipart.MultipartFile;
28+
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
29+
30+
import javax.servlet.http.HttpServletRequest;
31+
import java.io.File;
32+
import java.io.IOException;
33+
import java.io.Serializable;
34+
import java.nio.file.Path;
35+
import java.nio.file.Paths;
36+
import java.text.SimpleDateFormat;
37+
import java.util.ArrayList;
38+
import java.util.Arrays;
39+
import java.util.Collections;
40+
import java.util.List;
41+
import java.util.stream.Collectors;
42+
43+
/**
44+
* ClassName: FileController
45+
*
46+
* @author leegoo
47+
* @Description:
48+
* @date 2021年11月16日
49+
*/
50+
@Controller
51+
@CrossOrigin(allowCredentials = "true", originPatterns = "*", maxAge = 3600)
52+
public class FileController {
53+
54+
private static final Logger logger = LoggerFactory.getLogger(FileController.class);
55+
56+
@Autowired
57+
private FileService fileService;
58+
59+
60+
@RequestMapping(value = "/", method = RequestMethod.GET)
61+
public String index() {
62+
return "template/index";
63+
}
64+
65+
66+
@PostMapping("/uploadFile")
67+
@ResponseBody
68+
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
69+
String fileName = fileService.storeFile(file);
70+
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
71+
.path("/downloadFile/")
72+
.path(fileName)
73+
.toUriString();
74+
return new UploadFileResponse(fileName, fileDownloadUri,
75+
file.getContentType(), file.getSize());
76+
}
77+
78+
79+
@PostMapping("/uploadMultipleFiles")
80+
public List<UploadFileResponse> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
81+
return Arrays.stream(files)
82+
.map(this::uploadFile)
83+
.collect(Collectors.toList());
84+
}
85+
86+
@GetMapping("/downloadFile/{fileName:.+}")
87+
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
88+
// Load file as Resource
89+
Resource resource = fileService.loadFileAsResource(fileName);
90+
91+
// Try to determine file's content type
92+
String contentType = null;
93+
try {
94+
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
95+
} catch (IOException ex) {
96+
logger.info("Could not determine file type.");
97+
}
98+
99+
// Fallback to the default content type if type could not be determined
100+
if (contentType == null) {
101+
contentType = "application/octet-stream";
102+
}
103+
104+
return ResponseEntity.ok()
105+
.contentType(MediaType.parseMediaType(contentType))
106+
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
107+
.body(resource);
108+
}
109+
110+
111+
@javax.annotation.Resource
112+
private FileProperties fileProperties; // 文件在本地存储的地址
113+
114+
@GetMapping("/listFileNames")
115+
@ResponseBody
116+
public List<FileItem> listFileNames() {
117+
Path path = Paths.get(fileProperties.getUploadDir()).toAbsolutePath().normalize();
118+
File file = path.toFile();
119+
File[] files = file.listFiles();
120+
if (null == files) {
121+
return Collections.emptyList();
122+
}
123+
List<File> fileList = Arrays.stream(files).sorted((a, b) -> (int) (b.lastModified() - a.lastModified())).collect(Collectors.toList());
124+
List<FileItem> result = new ArrayList<>(files.length);
125+
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置格式
126+
for (File itemFile : fileList) {
127+
long lastModified = itemFile.lastModified();
128+
double d = itemFile.length() / 1048576D;
129+
result.add(FileItem.builder().filename(itemFile.getName()).freeSpace(String.format("%.2f", d)).lastModifiedStr(format.format(lastModified)).build());
130+
}
131+
return result;
132+
}
133+
134+
135+
@GetMapping("/deleteFile/{fileName}")
136+
@ResponseBody
137+
public boolean deleteFile(@PathVariable("fileName") String fileName) {
138+
return fileService.deleteFile(fileName);
139+
}
140+
141+
@Setter
142+
@Getter
143+
@Builder
144+
static
145+
class FileItem implements Serializable {
146+
private String filename;
147+
private long lastModifiedL;
148+
private String lastModifiedStr;
149+
private String freeSpace;
150+
}
151+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @Project:
3+
* @Author: leegoo
4+
* @Date: 2021年11月16日
5+
*/
6+
package cn.withmes.file.system;
7+
8+
/**
9+
* ClassName: FileException
10+
*
11+
* @author leegoo
12+
* @Description:
13+
* @date 2021年11月16日
14+
*/
15+
public class FileException extends RuntimeException {
16+
public FileException(String message) {
17+
super(message);
18+
}
19+
20+
public FileException(String message, Throwable cause) {
21+
super(message, cause);
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @Project:
3+
* @Author: leegoo
4+
* @Date: 2021年11月16日
5+
*/
6+
package cn.withmes.file.system;
7+
8+
/**
9+
* ClassName: FileProperties
10+
*
11+
* @Description:
12+
* @author leegoo
13+
* @date 2021年11月16日
14+
*/
15+
16+
import org.springframework.boot.context.properties.ConfigurationProperties;
17+
18+
19+
@ConfigurationProperties(prefix = "file")
20+
public class FileProperties {
21+
private String uploadDir;
22+
23+
public String getUploadDir() {
24+
return uploadDir;
25+
}
26+
27+
public void setUploadDir(String uploadDir) {
28+
this.uploadDir = uploadDir;
29+
}
30+
}

0 commit comments

Comments
 (0)