diff --git a/SpringBoot-ShardingJdbc/README.MD b/SpringBoot-ShardingJdbc/README.MD index b9bae7659955ca5b6941c57db69585935308a4ba..ea44ede9b87918957034b3ef798697504b77533b 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 + diff --git a/Springboot-File-Upload-Download/README.MD b/Springboot-File-Upload-Download/README.MD new file mode 100644 index 0000000000000000000000000000000000000000..01c53b4885f0fa77c817455bf77e82933b62f77b --- /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 0000000000000000000000000000000000000000..ce27d05ff5e93b3e01ab5d84fc73771a24cc2489 --- /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 0000000000000000000000000000000000000000..14d31411978575163c0750c07a3b352fea636a94 --- /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 0000000000000000000000000000000000000000..4c1e4810711572707af7cb1d97231cf6f0c20ee5 --- /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 0000000000000000000000000000000000000000..3eaafe9e4249523949d9a22a3fd546651d64d26f --- /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 0000000000000000000000000000000000000000..3369d92f45961f541b62c211f04f1fdf0b0592fd --- /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 0000000000000000000000000000000000000000..02c53cedaeec96bafa83558f12d55dd65cf6e026 --- /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 0000000000000000000000000000000000000000..a5b914507fcd62e030c66ead0c7b0857ce159bf5 --- /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 0000000000000000000000000000000000000000..542d57895a45573d8f8755255fe8f3afe5c945f0 --- /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 0000000000000000000000000000000000000000..3fe76f75e63d35aff4211a516eae4e462f362e15 --- /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 0000000000000000000000000000000000000000..9f417e9ab69da5ab902376b0a0a263f909804203 --- /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 0000000000000000000000000000000000000000..fdcf9bdff76e927e516b42663bd52d79178a8731 --- /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 0000000000000000000000000000000000000000..67dc2ec73adec21d0945d6a3e25bf803a25eae60 --- /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 0000000000000000000000000000000000000000..0d995394912fce7deaf09796d78b9ceb20e82b92 --- /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 0000000000000000000000000000000000000000..0f627f4fe8c6afd9b29815855797f5fcf4fcfee2 --- /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 0000000000000000000000000000000000000000..9116352cb4901faad19e7e517436d53986d0c1f7 --- /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 0000000000000000000000000000000000000000..5418f386ca6066f45e890faad1d033aeabae4d00 --- /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);