-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bde87ff
commit 0605523
Showing
8 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,5 @@ out | |
.DS_Store | ||
dump.rdb | ||
*.log | ||
/bin/ | ||
/bin/ | ||
/singleFileUpload/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>springboot-learn-integration</artifactId> | ||
<groupId>com.futao</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>springboot-learn-file-upload</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
15 changes: 15 additions & 0 deletions
15
...le-upload/src/main/java/com/futao/springboot/learn/file/upload/FileUploadApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.futao.springboot.learn.file.upload; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author futao | ||
* Created on 2019-06-28. | ||
*/ | ||
@SpringBootApplication | ||
public class FileUploadApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(FileUploadApplication.class, args); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...file-upload/src/main/java/com/futao/springboot/learn/file/upload/config/WebMvcConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.futao.springboot.learn.file.upload.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
/** | ||
* @author futao | ||
* Created on 2019-06-28. | ||
*/ | ||
@Configuration | ||
public class WebMvcConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addResourceHandlers(ResourceHandlerRegistry registry) { | ||
// registry.addResourceHandler("/**") | ||
// .addResourceLocations("classpath:/static/") | ||
// .setCacheControl(CacheControl.noCache()); | ||
|
||
|
||
// registry.addResourceHandler("/singleFileUpload/**") | ||
// .addResourceLocations("classpath:/singleFileUpload/") | ||
// .setCacheControl(CacheControl.maxAge(10, TimeUnit.DAYS)); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...pload/src/main/java/com/futao/springboot/learn/file/upload/controller/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.futao.springboot.learn.file.upload.controller; | ||
|
||
import cn.hutool.core.util.RandomUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.util.DigestUtils; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* TODO 上传之后无法访问 | ||
* | ||
* @author futao | ||
* Created on 2019-06-28. | ||
*/ | ||
@Slf4j | ||
@RestController | ||
public class FileController { | ||
private SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); | ||
private static final Map<String, String> FILE_MD5_CACHE = new HashMap<>(); | ||
|
||
@GetMapping("/single") | ||
public ModelAndView singleFile() { | ||
ModelAndView modelAndView = new ModelAndView(); | ||
modelAndView.setViewName("/singleFileUpload.html"); | ||
return modelAndView; | ||
} | ||
|
||
@PostMapping("/single") | ||
public String singleFile(MultipartFile file, HttpServletRequest request) throws IOException { | ||
if (file == null) { | ||
return "请先选择文件a"; | ||
} | ||
byte[] bytes = file.getBytes(); | ||
String fileMd5 = DigestUtils.md5DigestAsHex(bytes); | ||
String s1 = FILE_MD5_CACHE.get(fileMd5); | ||
if (StringUtils.isEmpty(s1)) { | ||
log.info("md5:[{}]", fileMd5); | ||
String dateString = this.format.format(new Date()); | ||
File folder = new File(request.getSession().getServletContext().getRealPath("singleFileUpload")); | ||
log.info("generate folder:[{}]", folder); | ||
folder.mkdir(); | ||
String filename = file.getOriginalFilename(); | ||
String randomString = RandomUtil.randomString(16); | ||
assert filename != null; | ||
String newFileName = randomString + filename.substring(filename.lastIndexOf(".")); | ||
try { | ||
file.transferTo(new File(folder, newFileName)); | ||
} catch (IOException e) { | ||
log.error("上传文件失败:", e); | ||
return "上传文件失败"; | ||
} | ||
FILE_MD5_CACHE.put(fileMd5, dateString + "/" + newFileName); | ||
return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/singleFileUpload/" + dateString + "/" + newFileName; | ||
} else { | ||
log.info("已存在,直接返回"); | ||
return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/singleFileUpload/" + s1; | ||
} | ||
} | ||
|
||
@PostMapping("/multiFileUpload") | ||
public void multiFileUpload(MultipartFile[] files) { | ||
for (MultipartFile file : files) { | ||
//upload() | ||
} | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
springboot-learn-file-upload/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
server: | ||
port: 8060 | ||
spring: | ||
application: | ||
name: file-upload | ||
servlet: | ||
multipart: | ||
enabled: true # 开启文件上传 | ||
max-file-size: 10GB | ||
resolve-lazily: true | ||
max-request-size: 100GB | ||
|
||
|
||
#singleFileUploadPath: /Users/futao/src/backend/fun/springboot-learn-integration/springboot-learn-file-upload/src/main/resources/singleFileUpload |
13 changes: 13 additions & 0 deletions
13
springboot-learn-file-upload/src/main/resources/static/singleFileUpload.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Title</title> | ||
</head> | ||
<body> | ||
<form action="/single" method="post" enctype="multipart/form-data"> | ||
<input type="file" name="file" value="choose file to upload,please"/> | ||
<input type="submit" value="提交"/> | ||
</form> | ||
</body> | ||
</html> |