From cfb9b81c6acbd77660689e3753f3ee4ef91253f8 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Sun, 22 Sep 2019 13:43:30 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Springboot=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E4=B8=8E=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Springboot-File-Upload-Download/README.MD | 19 +++ Springboot-File-Upload-Download/pom.xml | 60 ++++++++ .../SpringBootFileApplication.java | 15 ++ .../application/conf/FileLoadProperties.java | 16 +++ .../controller/FileDownloadController.java | 46 ++++++ .../controller/FileUploadController.java | 40 ++++++ .../application/exception/FileException.java | 13 ++ .../service/FileDownloadService.java | 7 + .../service/FileUploadService.java | 7 + .../service/impl/FileDownloadServiceImpl.java | 51 +++++++ .../service/impl/FileUploadServiceImpl.java | 51 +++++++ .../application/utils/UploadFileResponse.java | 53 +++++++ .../src/main/resources/application.yml | 22 +++ .../src/main/resources/logback.xml | 36 +++++ .../src/main/resources/static/css/main.css | 131 ++++++++++++++++++ .../src/main/resources/static/index.html | 56 ++++++++ .../src/main/resources/static/js/main.js | 115 +++++++++++++++ 17 files changed, 738 insertions(+) create mode 100644 Springboot-File-Upload-Download/README.MD create mode 100644 Springboot-File-Upload-Download/pom.xml create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/SpringBootFileApplication.java create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/conf/FileLoadProperties.java create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/controller/FileDownloadController.java create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/controller/FileUploadController.java create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/exception/FileException.java create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/FileDownloadService.java create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/FileUploadService.java create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/impl/FileDownloadServiceImpl.java create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/impl/FileUploadServiceImpl.java create mode 100644 Springboot-File-Upload-Download/src/main/java/com/button/file/application/utils/UploadFileResponse.java create mode 100644 Springboot-File-Upload-Download/src/main/resources/application.yml create mode 100644 Springboot-File-Upload-Download/src/main/resources/logback.xml create mode 100644 Springboot-File-Upload-Download/src/main/resources/static/css/main.css create mode 100644 Springboot-File-Upload-Download/src/main/resources/static/index.html create mode 100644 Springboot-File-Upload-Download/src/main/resources/static/js/main.js diff --git a/Springboot-File-Upload-Download/README.MD b/Springboot-File-Upload-Download/README.MD new file mode 100644 index 0000000..9dbaeb9 --- /dev/null +++ b/Springboot-File-Upload-Download/README.MD @@ -0,0 +1,19 @@ +本案例主要实现SpringBoot文件上传与下载; + +页面访问地址:
+http://localhost:8080/file/index.html + +接口访问地址:
+单文件上传
+http://localhost:8080/file/uploadFile
+参数:file
+多文件上传
+http://localhost:8080/file/uploadMultipleFiles
+参数:files
+ +下载:
+http://localhost:8080/file/downloadFile/文件名
+例如:http://localhost:8080/file/downloadFile/dubbo.txt + +参考自:
+https://www.callicoder.com/spring-boot-file-upload-download-rest-api-example/
\ No newline at end of file diff --git a/Springboot-File-Upload-Download/pom.xml b/Springboot-File-Upload-Download/pom.xml new file mode 100644 index 0000000..ce27d05 --- /dev/null +++ b/Springboot-File-Upload-Download/pom.xml @@ -0,0 +1,60 @@ + + 4.0.0 + com.button + Springboot-File-Upload-Download + 0.0.1-SNAPSHOT + jar + + + org.springframework.boot + spring-boot-starter-parent + 2.0.1.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-devtools + true + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + org.springframework.boot + spring-boot-maven-plugin + + -Dfile.encoding=UTF-8 + true + + + + + \ No newline at end of file diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/SpringBootFileApplication.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/SpringBootFileApplication.java new file mode 100644 index 0000000..14d3141 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/SpringBootFileApplication.java @@ -0,0 +1,15 @@ +package com.button.file.application; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; + +import com.button.file.application.conf.FileLoadProperties; + +@SpringBootApplication +@EnableConfigurationProperties({FileLoadProperties.class}) +public class SpringBootFileApplication { + public static void main(String[] args) { + SpringApplication.run(SpringBootFileApplication.class, args); + } +} diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/conf/FileLoadProperties.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/conf/FileLoadProperties.java new file mode 100644 index 0000000..4c1e481 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/conf/FileLoadProperties.java @@ -0,0 +1,16 @@ +package com.button.file.application.conf; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "file") +public class FileLoadProperties { + private String uploadDir; + + public String getUploadDir() { + return uploadDir; + } + + public void setUploadDir(String uploadDir) { + this.uploadDir = uploadDir; + } +} diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/controller/FileDownloadController.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/controller/FileDownloadController.java new file mode 100644 index 0000000..3eaafe9 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/controller/FileDownloadController.java @@ -0,0 +1,46 @@ +package com.button.file.application.controller; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import com.button.file.application.service.FileDownloadService; + +@RestController +public class FileDownloadController { + private static final Logger logger = LoggerFactory.getLogger(FileDownloadController.class); + + @Autowired + private FileDownloadService fileDownloadService; + + @GetMapping("/downloadFile/{fileName:.+}") + public ResponseEntity downloadFile(@PathVariable String fileName, HttpServletRequest request) { + logger.info("接收到需要下载的文件. fileName={}", fileName); + Resource resource = fileDownloadService.loadFileAsResource(fileName); + + String contentType = null; + try { + contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath()); + } catch (IOException ex) { + logger.error("获取文件的MimeType时发生异常. ex={}", ex); + } + // Fallback to the default content type if type could not be determined + if (contentType == null) { + contentType = "application/octet-stream"; + } + return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType)) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") + .body(resource); + } +} diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/controller/FileUploadController.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/controller/FileUploadController.java new file mode 100644 index 0000000..3369d92 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/controller/FileUploadController.java @@ -0,0 +1,40 @@ +package com.button.file.application.controller; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import com.button.file.application.service.FileUploadService; +import com.button.file.application.utils.UploadFileResponse; + +@RestController +public class FileUploadController { + private static final Logger logger = LoggerFactory.getLogger(FileUploadController.class); + + @Autowired + private FileUploadService fileUploadService; + + @PostMapping("/uploadFile") + public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) { + String fileName = fileUploadService.storeFile(file); + logger.info("获取到上传的文件名: fileName={}", fileName); + String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath().path("/downloadFile/").path(fileName).toUriString(); + return new UploadFileResponse(fileName, fileDownloadUri, file.getContentType(), file.getSize()); + } + + @PostMapping("/uploadMultipleFiles") + public List uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) { + logger.info("多文件上传本次共接收到{}个文件.", files.length); + return Arrays.stream(files).map(this::uploadFile).collect(Collectors.toList()); + } + +} diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/exception/FileException.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/exception/FileException.java new file mode 100644 index 0000000..02c53ce --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/exception/FileException.java @@ -0,0 +1,13 @@ +package com.button.file.application.exception; + +public class FileException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public FileException(String message) { + super(message); + } + + public FileException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/FileDownloadService.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/FileDownloadService.java new file mode 100644 index 0000000..a5b9145 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/FileDownloadService.java @@ -0,0 +1,7 @@ +package com.button.file.application.service; + +import org.springframework.core.io.Resource; + +public interface FileDownloadService { + public Resource loadFileAsResource(String fileName); +} diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/FileUploadService.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/FileUploadService.java new file mode 100644 index 0000000..542d578 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/FileUploadService.java @@ -0,0 +1,7 @@ +package com.button.file.application.service; + +import org.springframework.web.multipart.MultipartFile; + +public interface FileUploadService { + public String storeFile(MultipartFile file); +} diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/impl/FileDownloadServiceImpl.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/impl/FileDownloadServiceImpl.java new file mode 100644 index 0000000..3fe76f7 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/impl/FileDownloadServiceImpl.java @@ -0,0 +1,51 @@ +package com.button.file.application.service.impl; + +import java.net.MalformedURLException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.core.io.UrlResource; +import org.springframework.stereotype.Service; + +import com.button.file.application.conf.FileLoadProperties; +import com.button.file.application.exception.FileException; +import com.button.file.application.service.FileDownloadService; + +@Service +public class FileDownloadServiceImpl implements FileDownloadService { + private final Path fileStorageLocation; // 文件在本地存储的地址 + + @Autowired + public FileDownloadServiceImpl(FileLoadProperties fileProperties) { + this.fileStorageLocation = Paths.get(fileProperties.getUploadDir()).toAbsolutePath().normalize(); + try { + Files.createDirectories(this.fileStorageLocation); + } catch (Exception ex) { + throw new FileException("保存文件创建目录时发生异常. .", ex); + } + } + + /** + * 加载文件 + * + * @param fileName 文件名 + * @return 文件 + */ + @Override + public Resource loadFileAsResource(String fileName) { + try { + Path filePath = this.fileStorageLocation.resolve(fileName).normalize(); + Resource resource = new UrlResource(filePath.toUri()); + if (resource.exists()) { + return resource; + } else { + throw new FileException("File not found " + fileName); + } + } catch (MalformedURLException ex) { + throw new FileException("File not found " + fileName, ex); + } + } +} diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/impl/FileUploadServiceImpl.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/impl/FileUploadServiceImpl.java new file mode 100644 index 0000000..9f417e9 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/service/impl/FileUploadServiceImpl.java @@ -0,0 +1,51 @@ +package com.button.file.application.service.impl; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import com.button.file.application.conf.FileLoadProperties; +import com.button.file.application.exception.FileException; +import com.button.file.application.service.FileUploadService; + +@Service +public class FileUploadServiceImpl implements FileUploadService { + private final Path fileStorageLocation; // 文件在本地存储的地址 + @Autowired + public FileUploadServiceImpl(FileLoadProperties fileProperties) { + this.fileStorageLocation = Paths.get(fileProperties.getUploadDir()).toAbsolutePath().normalize(); + try { + Files.createDirectories(this.fileStorageLocation); + } catch (Exception ex) { + throw new FileException("保存文件创建文件夹时出现异常.", ex); + } + } + + /** + * 存储文件到系统 + * + * @param file 文件 + * @return 文件名 + */ + @Override + public String storeFile(MultipartFile file) { + String fileName = StringUtils.cleanPath(file.getOriginalFilename()); + try { + if (fileName.contains("..")) { + throw new FileException("抱歉,文件名包含不合法的字符." + fileName); + } + Path targetLocation = this.fileStorageLocation.resolve(fileName); + Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING); + return fileName; + } catch (IOException ex) { + throw new FileException("保存文件:" + fileName + "时出现异常,请重试!!", ex); + } + } +} diff --git a/Springboot-File-Upload-Download/src/main/java/com/button/file/application/utils/UploadFileResponse.java b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/utils/UploadFileResponse.java new file mode 100644 index 0000000..fdcf9bd --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/java/com/button/file/application/utils/UploadFileResponse.java @@ -0,0 +1,53 @@ +package com.button.file.application.utils; + +public class UploadFileResponse { + private String fileName; + private String fileDownloadUri; + private String fileType; + private long size; + + public UploadFileResponse(String fileName, String fileDownloadUri, String fileType, long size) { + this.fileName = fileName; + this.fileDownloadUri = fileDownloadUri; + this.fileType = fileType; + this.size = size; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getFileDownloadUri() { + return fileDownloadUri; + } + + public void setFileDownloadUri(String fileDownloadUri) { + this.fileDownloadUri = fileDownloadUri; + } + + public String getFileType() { + return fileType; + } + + public void setFileType(String fileType) { + this.fileType = fileType; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + @Override + public String toString() { + return "UploadFileResponse [fileName=" + fileName + ", fileDownloadUri=" + fileDownloadUri + ", fileType=" + + fileType + ", size=" + size + "]"; + } +} diff --git a/Springboot-File-Upload-Download/src/main/resources/application.yml b/Springboot-File-Upload-Download/src/main/resources/application.yml new file mode 100644 index 0000000..67dc2ec --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/resources/application.yml @@ -0,0 +1,22 @@ +server: + servlet: + context-path: /file + port: 8080 + uri-encoding: utf-8 + +logging: + config: classpath:logback.xml + +## MULTIPART (MultipartProperties) +# 开启 multipart 上传功能 +spring: + servlet: + multipart: + enabled: true + file-size-threshold: 2KB # 文件写入磁盘的阈值 + max-file-size: 200MB # 最大文件大小 + max-request-size: 215MB # 最大请求大小 + +## 文件存储所需参数 +# 所有通过 REST APIs 上传的文件都将存储在此目录下 +file.upload-dir: ./uploads diff --git a/Springboot-File-Upload-Download/src/main/resources/logback.xml b/Springboot-File-Upload-Download/src/main/resources/logback.xml new file mode 100644 index 0000000..0d99539 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/resources/logback.xml @@ -0,0 +1,36 @@ + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + + ${LOG_HOME}/button_api.log + + + + ${LOG_HOME}/button_api.log.%d{yyyy-MM-dd}.%i.log + + + + 100MB + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n + + + + + + + + + \ No newline at end of file diff --git a/Springboot-File-Upload-Download/src/main/resources/static/css/main.css b/Springboot-File-Upload-Download/src/main/resources/static/css/main.css new file mode 100644 index 0000000..0f627f4 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/resources/static/css/main.css @@ -0,0 +1,131 @@ +* { + -webkit-box-sizing: border-box; + -moz-box-sizing: border-box; + box-sizing: border-box; +} + +body { + margin: 0; + padding: 0; + font-weight: 400; + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + font-size: 1rem; + line-height: 1.58; + color: #333; + background-color: #f4f4f4; +} + +body:before { + height: 50%; + width: 100%; + position: absolute; + top: 0; + left: 0; + background: #128ff2; + content: ""; + z-index: 0; +} + +.clearfix:after { + display: block; + content: ""; + clear: both; +} + + +h1, h2, h3, h4, h5, h6 { + margin-top: 20px; + margin-bottom: 20px; +} + +h1 { + font-size: 1.7em; +} + +a { + color: #128ff2; +} + +button { + box-shadow: none; + border: 1px solid transparent; + font-size: 14px; + outline: none; + line-height: 100%; + white-space: nowrap; + vertical-align: middle; + padding: 0.6rem 1rem; + border-radius: 2px; + transition: all 0.2s ease-in-out; + cursor: pointer; + min-height: 38px; +} + +button.primary { + background-color: #128ff2; + box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.12); + color: #fff; +} + +input { + font-size: 1rem; +} + +input[type="file"] { + border: 1px solid #128ff2; + padding: 6px; + max-width: 100%; +} + +.file-input { + width: 100%; +} + +.submit-btn { + display: block; + margin-top: 15px; + min-width: 100px; +} + +@media screen and (min-width: 500px) { + .file-input { + width: calc(100% - 115px); + } + + .submit-btn { + display: inline-block; + margin-top: 0; + margin-left: 10px; + } +} + +.upload-container { + max-width: 700px; + margin-left: auto; + margin-right: auto; + background-color: #fff; + box-shadow: 0 1px 11px rgba(0, 0, 0, 0.27); + margin-top: 60px; + min-height: 400px; + position: relative; + padding: 20px; +} + +.upload-header { + border-bottom: 1px solid #ececec; +} + +.upload-header h2 { + font-weight: 500; +} + +.single-upload { + padding-bottom: 20px; + margin-bottom: 20px; + border-bottom: 1px solid #e8e8e8; +} + +.upload-response { + overflow-x: hidden; + word-break: break-all; +} \ No newline at end of file diff --git a/Springboot-File-Upload-Download/src/main/resources/static/index.html b/Springboot-File-Upload-Download/src/main/resources/static/index.html new file mode 100644 index 0000000..9116352 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/resources/static/index.html @@ -0,0 +1,56 @@ + + + + + + Spring Boot File Upload / Download Rest API Example + + + + +
+
+

Spring Boot上传和下载文件

+
+
+
+

上传单文件

+
+ + +
+
+
+
+
+
+
+

上传多文件

+
+ + +
+
+
+
+
+
+
+
+

下载文件

+
+ + +
+
+
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/Springboot-File-Upload-Download/src/main/resources/static/js/main.js b/Springboot-File-Upload-Download/src/main/resources/static/js/main.js new file mode 100644 index 0000000..5418f38 --- /dev/null +++ b/Springboot-File-Upload-Download/src/main/resources/static/js/main.js @@ -0,0 +1,115 @@ +'use strict'; + +var singleUploadForm = document.querySelector('#singleUploadForm'); +var singleFileUploadInput = document.querySelector('#singleFileUploadInput'); +var singleFileUploadError = document.querySelector('#singleFileUploadError'); +var singleFileUploadSuccess = document.querySelector('#singleFileUploadSuccess'); + +var multipleUploadForm = document.querySelector('#multipleUploadForm'); +var multipleFileUploadInput = document.querySelector('#multipleFileUploadInput'); +var multipleFileUploadError = document.querySelector('#multipleFileUploadError'); +var multipleFileUploadSuccess = document.querySelector('#multipleFileUploadSuccess'); + +var fileDownloadForm = document.querySelector('#fileDownloadForm'); +var fileDownloadInput = document.querySelector('#fileDownloadInput'); +var downloadError = document.querySelector('#downloadError'); +var downloadSuccess = document.querySelector('#downloadSuccess'); + +function uploadSingleFile(file) { + var formData = new FormData(); + formData.append("file", file); + + var xhr = new XMLHttpRequest(); + xhr.open("POST", "/file/uploadFile"); + + xhr.onload = function() { + console.log(xhr.responseText); + var response = JSON.parse(xhr.responseText); + if(xhr.status == 200) { + singleFileUploadError.style.display = "none"; + singleFileUploadSuccess.innerHTML = "

文件上传成功.

DownloadUrl : " + response.fileDownloadUri + "

"; + singleFileUploadSuccess.style.display = "block"; + } else { + singleFileUploadSuccess.style.display = "none"; + singleFileUploadError.innerHTML = (response && response.message) || "Some Error Occurred"; + } + } + + xhr.send(formData); +} + +function uploadMultipleFiles(files) { + var formData = new FormData(); + for(var index = 0; index < files.length; index++) { + formData.append("files", files[index]); + } + + var xhr = new XMLHttpRequest(); + xhr.open("POST", "/file/uploadMultipleFiles"); + + xhr.onload = function() { + console.log(xhr.responseText); + var response = JSON.parse(xhr.responseText); + if(xhr.status == 200) { + multipleFileUploadError.style.display = "none"; + var content = "

所有文件均已上传成功.

"; + for(var i = 0; i < response.length; i++) { + content += "

DownloadUrl : " + response[i].fileDownloadUri + "

"; + } + multipleFileUploadSuccess.innerHTML = content; + multipleFileUploadSuccess.style.display = "block"; + } else { + multipleFileUploadSuccess.style.display = "none"; + multipleFileUploadError.innerHTML = (response && response.message) || "Some Error Occurred"; + } + } + + xhr.send(formData); +} + + +singleUploadForm.addEventListener('submit', function(event){ + var files = singleFileUploadInput.files; + if(files.length === 0) { + singleFileUploadError.innerHTML = "请选择一个文件!"; + singleFileUploadError.style.display = "block"; + } + uploadSingleFile(files[0]); + event.preventDefault(); +}, true); + + +multipleUploadForm.addEventListener('submit', function(event){ + var files = multipleFileUploadInput.files; + if(files.length === 0) { + multipleFileUploadError.innerHTML = "请至少选择一个文件!"; + multipleFileUploadError.style.display = "block"; + } + uploadMultipleFiles(files); + event.preventDefault(); +}, true); + +function downLoadFile(fileName) { + var xhr = new XMLHttpRequest(); + xhr.open("GET", "/file/downloadFile/" + fileName, true); + xhr.onload = function() { + console.log(xhr); + if(xhr.status == 200) { + window.open('/file/downloadFile/' + fileName); + downloadError.style.display = "none"; + downloadSuccess.innerHTML = "文件下载成功!"; + downloadSuccess.style.display = "block"; + } else { + downloadSuccess.style.display = "none"; + downloadError.innerHTML = "文件不存在或文件下载失败,请检查文件名!!"; + downloadError.style.display = "block"; + } + } + xhr.send(null); +} +fileDownloadForm.addEventListener('submit', function(event){ + var fileName = fileDownloadInput.value; + console.log(fileName); + downLoadFile(fileName); + event.preventDefault(); +}, true); -- Gitee From 2ae208ca873331ab83fa553f4dd7fef24934f8f1 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Sun, 22 Sep 2019 13:46:08 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Springboot-File-Upload-Download/README.MD | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Springboot-File-Upload-Download/README.MD b/Springboot-File-Upload-Download/README.MD index 9dbaeb9..3d51c18 100644 --- a/Springboot-File-Upload-Download/README.MD +++ b/Springboot-File-Upload-Download/README.MD @@ -1,13 +1,13 @@ -本案例主要实现SpringBoot文件上传与下载; - -页面访问地址:
+本案例主要实现SpringBoot文件上传与下载: +页面访问地址:
http://localhost:8080/file/index.html 接口访问地址:
单文件上传
http://localhost:8080/file/uploadFile
参数:file
-多文件上传
+ +多文件上传
http://localhost:8080/file/uploadMultipleFiles
参数:files
-- Gitee From 4c1a7bcc6a79a2a0b3f264fc7ae3ae4c47ee3106 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Sun, 22 Sep 2019 13:51:40 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Springboot-File-Upload-Download/README.MD | 26 +++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Springboot-File-Upload-Download/README.MD b/Springboot-File-Upload-Download/README.MD index 3d51c18..01c53b4 100644 --- a/Springboot-File-Upload-Download/README.MD +++ b/Springboot-File-Upload-Download/README.MD @@ -1,19 +1,19 @@ -本案例主要实现SpringBoot文件上传与下载: -页面访问地址:
+# 本案例主要实现SpringBoot文件上传与下载 +#### 页面访问地址: http://localhost:8080/file/index.html -接口访问地址:
-单文件上传
-http://localhost:8080/file/uploadFile
-参数:file
+#### 接口访问地址 +单文件上传: +http://localhost:8080/file/uploadFile +参数:file -多文件上传
-http://localhost:8080/file/uploadMultipleFiles
-参数:files
+多文件上传 +http://localhost:8080/file/uploadMultipleFiles +参数:files -下载:
-http://localhost:8080/file/downloadFile/文件名
+下载: +http://localhost:8080/file/downloadFile/文件名 例如:http://localhost:8080/file/downloadFile/dubbo.txt -参考自:
-https://www.callicoder.com/spring-boot-file-upload-download-rest-api-example/
\ No newline at end of file +#### 参考自 +https://www.callicoder.com/spring-boot-file-upload-download-rest-api-example/ \ No newline at end of file -- Gitee From 5682e545985dff786f59537af797c62292b9c200 Mon Sep 17 00:00:00 2001 From: "zhang.hang" <2740277548@qq.com> Date: Sun, 22 Sep 2019 13:53:15 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpringBoot-ShardingJdbc/README.MD | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/SpringBoot-ShardingJdbc/README.MD b/SpringBoot-ShardingJdbc/README.MD index b9bae76..ea44ede 100644 --- a/SpringBoot-ShardingJdbc/README.MD +++ b/SpringBoot-ShardingJdbc/README.MD @@ -1,3 +1,4 @@ -http://localhost:8080/sharding/user/saveUser
-http://localhost:8080/sharding/user/selectUser - +# 本案例主要实现SpringBoot使用Sharding-Jdbc实现分库分表 +http://localhost:8080/sharding/user/saveUser
+http://localhost:8080/sharding/user/selectUser + -- Gitee