Skip to content

Commit

Permalink
add module fileUpload
Browse files Browse the repository at this point in the history
  • Loading branch information
FutaoSmile committed Jun 28, 2019
1 parent bde87ff commit 0605523
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ out
.DS_Store
dump.rdb
*.log
/bin/
/bin/
/singleFileUpload/
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>springboot-learn-freemarker</module>
<module>springboot-learn-api</module>
<module>springboot-learn-static-resources</module>
<module>springboot-learn-file-upload</module>
</modules>

<!--引入SpringBootParent的第二种方式-->
Expand Down Expand Up @@ -92,6 +93,10 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>


Expand Down
20 changes: 20 additions & 0 deletions springboot-learn-file-upload/pom.xml
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>
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);
}
}
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));
}
}
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 springboot-learn-file-upload/src/main/resources/application.yml
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
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>

0 comments on commit 0605523

Please sign in to comment.