├── clientservice ├── src │ ├── main │ │ ├── resources │ │ │ └── application.properties │ │ └── java │ │ │ └── com │ │ │ └── clientservice │ │ │ └── clientservice │ │ │ ├── ClientserviceApplication.java │ │ │ └── FileController.java │ └── test │ │ └── java │ │ └── com │ │ └── clientservice │ │ └── clientservice │ │ └── ClientserviceApplicationTests.java ├── settings.gradle ├── temp │ ├── slogo.png │ └── zuul.png ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── .gitignore ├── build.gradle ├── gradlew.bat └── gradlew ├── fileUploadDownload ├── settings.gradle ├── fileStorage │ ├── pcf.jpg │ ├── redis.png │ ├── slogo.png │ ├── zuul.png │ ├── susbcribe.png │ └── zuul%20netflix.png ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── dbScripts.sql ├── src │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── greenlearner │ │ │ └── fileUploadDownload │ │ │ ├── FileUploadDownloadApplicationTests.java │ │ │ └── controller │ │ │ └── UploadDownloadWithFileSystemControllerTest.java │ └── main │ │ ├── resources │ │ ├── templates │ │ │ └── index.html │ │ └── application.yaml │ │ └── java │ │ └── com │ │ └── greenlearner │ │ └── fileUploadDownload │ │ ├── FileUploadDownloadApplication.java │ │ ├── service │ │ ├── DocFileDao.java │ │ └── FileStorageService.java │ │ ├── controller │ │ ├── FileUploadWithFrontEndCode.java │ │ ├── UploadDownloadWithDatabaseController.java │ │ └── UploadDownloadWithFileSystemController.java │ │ └── dto │ │ ├── FileDocument.java │ │ └── FileUploadResponse.java ├── .gitignore ├── build.gradle ├── gradlew.bat └── gradlew ├── .gitattributes ├── File Operations With Spring Boot.pptx ├── .idea ├── vcs.xml ├── modules.xml ├── File-Upload-Download-With-SpringBoot.iml └── workspace.xml ├── .gitignore ├── README.md └── LICENSE /clientservice/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /clientservice/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'clientservice' 2 | -------------------------------------------------------------------------------- /fileUploadDownload/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'fileUploadDownload' 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /clientservice/temp/slogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/clientservice/temp/slogo.png -------------------------------------------------------------------------------- /clientservice/temp/zuul.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/clientservice/temp/zuul.png -------------------------------------------------------------------------------- /File Operations With Spring Boot.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/File Operations With Spring Boot.pptx -------------------------------------------------------------------------------- /fileUploadDownload/fileStorage/pcf.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/fileUploadDownload/fileStorage/pcf.jpg -------------------------------------------------------------------------------- /fileUploadDownload/fileStorage/redis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/fileUploadDownload/fileStorage/redis.png -------------------------------------------------------------------------------- /fileUploadDownload/fileStorage/slogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/fileUploadDownload/fileStorage/slogo.png -------------------------------------------------------------------------------- /fileUploadDownload/fileStorage/zuul.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/fileUploadDownload/fileStorage/zuul.png -------------------------------------------------------------------------------- /fileUploadDownload/fileStorage/susbcribe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/fileUploadDownload/fileStorage/susbcribe.png -------------------------------------------------------------------------------- /clientservice/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/clientservice/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /fileUploadDownload/fileStorage/zuul%20netflix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/fileUploadDownload/fileStorage/zuul%20netflix.png -------------------------------------------------------------------------------- /fileUploadDownload/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codefarm0/File-Upload-Download-With-SpringBoot/HEAD/fileUploadDownload/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /fileUploadDownload/dbScripts.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE green.file_document ( 2 | id long, 3 | fileName varchar(30), 4 | docFile longblob 5 | ); 6 | 7 | select * from green.file_document; -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /clientservice/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /fileUploadDownload/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /clientservice/src/test/java/com/clientservice/clientservice/ClientserviceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.clientservice.clientservice; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ClientserviceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | -------------------------------------------------------------------------------- /fileUploadDownload/src/test/java/com/greenlearner/fileUploadDownload/FileUploadDownloadApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class FileUploadDownloadApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /.idea/File-Upload-Download-With-SpringBoot.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 | 7 | 8 | 9 |
File to upload:
10 |
11 |
12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/java/com/greenlearner/fileUploadDownload/FileUploadDownloadApplication.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class FileUploadDownloadApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(FileUploadDownloadApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /clientservice/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/** 6 | !**/src/test/** 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | 17 | ### IntelliJ IDEA ### 18 | .idea 19 | *.iws 20 | *.iml 21 | *.ipr 22 | out/ 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | 31 | ### VS Code ### 32 | .vscode/ 33 | -------------------------------------------------------------------------------- /fileUploadDownload/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/** 6 | !**/src/test/** 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | 17 | ### IntelliJ IDEA ### 18 | .idea 19 | *.iws 20 | *.iml 21 | *.ipr 22 | out/ 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | 31 | ### VS Code ### 32 | .vscode/ 33 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 3 | 4 | file: 5 | storage: 6 | location: fileStorage 7 | 8 | spring: 9 | servlet: 10 | multipart: 11 | enabled: true 12 | location: temp123 13 | file-size-threshold: 5MB 14 | max-file-size: 1MB 15 | max-request-size: 20MB 16 | 17 | datasource: 18 | url: jdbc:mysql://localhost:3306/green?useSSL=false 19 | username: root 20 | password: tiger 21 | driver-class-name: com.mysql.cj.jdbc.Driver 22 | 23 | 24 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/java/com/greenlearner/fileUploadDownload/service/DocFileDao.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload.service; 2 | 3 | import com.greenlearner.fileUploadDownload.dto.FileDocument; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | /** 8 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) 9 | */ 10 | 11 | @Repository 12 | public interface DocFileDao extends CrudRepository { 13 | 14 | FileDocument findByFileName(String fileName); 15 | } 16 | -------------------------------------------------------------------------------- /clientservice/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.springframework.boot' version '2.3.1.RELEASE' 3 | id 'io.spring.dependency-management' version '1.0.9.RELEASE' 4 | id 'java' 5 | } 6 | 7 | group = 'com.clientservice' 8 | version = '0.0.1-SNAPSHOT' 9 | sourceCompatibility = '1.8' 10 | 11 | repositories { 12 | mavenCentral() 13 | } 14 | 15 | dependencies { 16 | implementation 'org.springframework.boot:spring-boot-starter-web' 17 | testImplementation('org.springframework.boot:spring-boot-starter-test') { 18 | exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' 19 | } 20 | } 21 | 22 | test { 23 | useJUnitPlatform() 24 | } 25 | -------------------------------------------------------------------------------- /clientservice/src/main/java/com/clientservice/clientservice/ClientserviceApplication.java: -------------------------------------------------------------------------------- 1 | package com.clientservice.clientservice; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.web.client.RestTemplateBuilder; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.web.client.RestTemplate; 8 | 9 | @SpringBootApplication 10 | public class ClientserviceApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(ClientserviceApplication.class, args); 14 | } 15 | 16 | @Bean 17 | RestTemplate restTemplate(RestTemplateBuilder builder){ 18 | return builder.build(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/java/com/greenlearner/fileUploadDownload/controller/FileUploadWithFrontEndCode.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.ExtendedModelMap; 5 | import org.springframework.ui.Model; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | /** 10 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) 11 | */ 12 | 13 | @Controller 14 | public class FileUploadWithFrontEndCode { 15 | 16 | @GetMapping("/files") 17 | ModelAndView fileUpload(){ 18 | Model m = new ExtendedModelMap(); 19 | return new ModelAndView("index.html"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /fileUploadDownload/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.springframework.boot' version '2.3.1.RELEASE' 3 | id 'io.spring.dependency-management' version '1.0.9.RELEASE' 4 | id 'java' 5 | } 6 | 7 | group = 'com.greenlearner' 8 | version = '0.0.1-SNAPSHOT' 9 | sourceCompatibility = '1.8' 10 | 11 | repositories { 12 | mavenCentral() 13 | } 14 | 15 | dependencies { 16 | implementation 'org.springframework.boot:spring-boot-starter-web' 17 | 18 | //spring data 19 | implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 20 | 21 | //my sql connector 22 | compile 'mysql:mysql-connector-java:8.0.17' 23 | 24 | implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 25 | 26 | testImplementation('org.springframework.boot:spring-boot-starter-test') { 27 | exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' 28 | } 29 | } 30 | 31 | test { 32 | useJUnitPlatform() 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # File-Upload-Download-With-SpringBoot (Single Microservice) 2 | File Upload | Download | Rendering to local file system as well as Database 3 | 4 | * [Part - 1 Single File upload download to filesystem](https://youtu.be/LUq4UtsGcyU) 5 | 6 | * [Part - 2 Multiple File upload download to filesystem](https://youtu.be/l0HsJJzCTj4) 7 | 8 | * [Part - 3 File upload download to database(mysql)](https://youtu.be/8YDeBkNidmg) 9 | 10 | * [Part - 4(Bonus) - Integration test](https://youtu.be/FzlfQErXLmo) 11 | 12 | 13 | ## Let's use the above microservice 14 | 15 | Below is the future plan that can be looked into. As soon as i compelte video, will update the link 16 | 17 | * [Part - 5 Java Client to call the file-upload service](https://youtu.be/t0Xy_Y9w6ik) 18 | 19 | * [Part - 6 Angular Application to call the file-upload service and upload/download file] - TODO 20 | 21 | * [Part - 7 React Application to call the file-upload service and upload/download file] - TODO 22 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/java/com/greenlearner/fileUploadDownload/dto/FileDocument.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload.dto; 2 | 3 | import javax.persistence.*; 4 | 5 | /** 6 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) 7 | */ 8 | @Entity 9 | public class FileDocument { 10 | 11 | @Id 12 | @GeneratedValue(strategy = GenerationType.AUTO) 13 | private long id; 14 | 15 | @Column(name = "filename") 16 | private String fileName; 17 | 18 | @Column(name = "docfile") 19 | @Lob 20 | private byte[] docFile; 21 | 22 | public long getId() { 23 | return id; 24 | } 25 | 26 | public void setId(long id) { 27 | this.id = id; 28 | } 29 | 30 | public String getFileName() { 31 | return fileName; 32 | } 33 | 34 | public void setFileName(String fileName) { 35 | this.fileName = fileName; 36 | } 37 | 38 | public byte[] getDocFile() { 39 | return docFile; 40 | } 41 | 42 | public void setDocFile(byte[] docFile) { 43 | this.docFile = docFile; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Green Learner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/java/com/greenlearner/fileUploadDownload/dto/FileUploadResponse.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload.dto; 2 | 3 | /** 4 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) 5 | */ 6 | public class FileUploadResponse { 7 | 8 | private String fileName; 9 | 10 | private String contentType; 11 | 12 | private String url; 13 | 14 | public FileUploadResponse(String fileName, String contentType, String url) { 15 | this.fileName = fileName; 16 | this.contentType = contentType; 17 | this.url = url; 18 | } 19 | 20 | public String getFileName() { 21 | return fileName; 22 | } 23 | 24 | public void setFileName(String fileName) { 25 | this.fileName = fileName; 26 | } 27 | 28 | public String getContentType() { 29 | return contentType; 30 | } 31 | 32 | public void setContentType(String contentType) { 33 | this.contentType = contentType; 34 | } 35 | 36 | public String getUrl() { 37 | return url; 38 | } 39 | 40 | public void setUrl(String url) { 41 | this.url = url; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 16 | 17 | 19 | 20 | 21 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 1593061441177 36 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/java/com/greenlearner/fileUploadDownload/service/FileStorageService.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload.service; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.core.io.Resource; 5 | import org.springframework.core.io.UrlResource; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.util.StringUtils; 8 | import org.springframework.web.multipart.MultipartFile; 9 | 10 | import java.io.IOException; 11 | import java.net.MalformedURLException; 12 | import java.nio.file.Files; 13 | import java.nio.file.Path; 14 | import java.nio.file.Paths; 15 | import java.nio.file.StandardCopyOption; 16 | 17 | /** 18 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) 19 | */ 20 | 21 | @Service 22 | public class FileStorageService { 23 | 24 | private Path fileStoragePath; 25 | private String fileStorageLocation; 26 | 27 | public FileStorageService(@Value("${file.storage.location:temp}") String fileStorageLocation) { 28 | 29 | this.fileStorageLocation = fileStorageLocation; 30 | fileStoragePath = Paths.get(fileStorageLocation).toAbsolutePath().normalize(); 31 | 32 | try { 33 | Files.createDirectories(fileStoragePath); 34 | } catch (IOException e) { 35 | throw new RuntimeException("Issue in creating file directory"); 36 | } 37 | } 38 | 39 | public String storeFile(MultipartFile file) { 40 | String fileName = StringUtils.cleanPath(file.getOriginalFilename()); 41 | 42 | Path filePath = Paths.get(fileStoragePath + "\\" + fileName); 43 | 44 | try { 45 | Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING); 46 | } catch (IOException e) { 47 | throw new RuntimeException("Issue in storing the file", e); 48 | } 49 | return fileName; 50 | } 51 | 52 | public Resource downloadFile(String fileName) { 53 | 54 | Path path = Paths.get(fileStorageLocation).toAbsolutePath().resolve(fileName); 55 | 56 | Resource resource; 57 | try { 58 | resource = new UrlResource(path.toUri()); 59 | 60 | } catch (MalformedURLException e) { 61 | throw new RuntimeException("Issue in reading the file", e); 62 | } 63 | 64 | if(resource.exists() && resource.isReadable()){ 65 | return resource; 66 | }else{ 67 | throw new RuntimeException("the file doesn't exist or not readable"); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /clientservice/src/main/java/com/clientservice/clientservice/FileController.java: -------------------------------------------------------------------------------- 1 | package com.clientservice.clientservice; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.core.io.Resource; 5 | import org.springframework.core.io.UrlResource; 6 | import org.springframework.util.LinkedMultiValueMap; 7 | import org.springframework.util.MultiValueMap; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.PostMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | import org.springframework.web.client.RestTemplate; 13 | 14 | import java.io.IOException; 15 | import java.net.MalformedURLException; 16 | import java.nio.file.Files; 17 | import java.nio.file.Path; 18 | import java.nio.file.Paths; 19 | import java.nio.file.StandardCopyOption; 20 | import java.util.stream.Stream; 21 | 22 | /** 23 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) 24 | */ 25 | 26 | @RestController 27 | public class FileController { 28 | 29 | @Autowired 30 | RestTemplate restTemplate; 31 | 32 | String baseUrl = "http://localhost:8081"; 33 | 34 | @GetMapping("getAndSave/{fileName}") 35 | String getAndSaveFile(@PathVariable String fileName) throws IOException { 36 | 37 | Resource resource = restTemplate.getForObject(baseUrl + "/download/" + fileName, Resource.class); 38 | 39 | Path path = Paths.get("temp").toAbsolutePath().normalize(); 40 | Files.createDirectories(Paths.get("temp")); 41 | 42 | Files.copy(resource.getInputStream(), Paths.get(path + "\\" + fileName), StandardCopyOption.REPLACE_EXISTING); 43 | 44 | return "data retrieved properly"; 45 | } 46 | 47 | @GetMapping("/executeTask") 48 | String executeTask() throws IOException { 49 | 50 | Stream list = Files.list(Paths.get("C:\\Users\\Arvind\\Pictures\\toupload")); 51 | 52 | //http://localhost:8081//multiple/upload?files="1.jpg"&files="2.jpg" 53 | MultiValueMap filesToUpload = new LinkedMultiValueMap<>(); 54 | list.forEach( 55 | file -> { 56 | try { 57 | filesToUpload.add("files", new UrlResource(file.toAbsolutePath().toUri())); 58 | } catch (MalformedURLException e) { 59 | e.printStackTrace(); 60 | } 61 | } 62 | ); 63 | 64 | String resp = restTemplate.postForObject(baseUrl+"/multiple/upload", filesToUpload, String.class); 65 | 66 | return resp; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/java/com/greenlearner/fileUploadDownload/controller/UploadDownloadWithDatabaseController.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload.controller; 2 | 3 | import com.greenlearner.fileUploadDownload.dto.FileDocument; 4 | import com.greenlearner.fileUploadDownload.dto.FileUploadResponse; 5 | import com.greenlearner.fileUploadDownload.service.DocFileDao; 6 | import org.springframework.core.io.Resource; 7 | import org.springframework.http.HttpHeaders; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.util.StringUtils; 11 | import org.springframework.web.bind.annotation.*; 12 | import org.springframework.web.multipart.MultipartFile; 13 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder; 14 | 15 | import javax.servlet.http.HttpServletRequest; 16 | import java.io.IOException; 17 | 18 | /** 19 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) 20 | */ 21 | 22 | @RestController 23 | public class UploadDownloadWithDatabaseController { 24 | 25 | private DocFileDao docFileDao; 26 | 27 | public UploadDownloadWithDatabaseController(DocFileDao docFileDao) { 28 | this.docFileDao = docFileDao; 29 | } 30 | 31 | @PostMapping("single/uploadDb") 32 | FileUploadResponse singleFileUplaod(@RequestParam("file") MultipartFile file) throws IOException { 33 | 34 | String name = StringUtils.cleanPath(file.getOriginalFilename()); 35 | FileDocument fileDocument = new FileDocument(); 36 | fileDocument.setFileName(name); 37 | fileDocument.setDocFile(file.getBytes()); 38 | 39 | docFileDao.save(fileDocument); 40 | 41 | ///http://localhost:8081/download/abc.jpg 42 | String url = ServletUriComponentsBuilder.fromCurrentContextPath() 43 | .path("/downloadFromDB/") 44 | .path(name) 45 | .toUriString(); 46 | 47 | String contentType = file.getContentType(); 48 | 49 | FileUploadResponse response = new FileUploadResponse(name, contentType, url); 50 | 51 | return response; 52 | 53 | } 54 | 55 | @GetMapping("/downloadFromDB/{fileName}") 56 | ResponseEntity downLoadSingleFile(@PathVariable String fileName, HttpServletRequest request) { 57 | 58 | FileDocument doc = docFileDao.findByFileName(fileName); 59 | 60 | String mimeType = request.getServletContext().getMimeType(doc.getFileName()); 61 | 62 | return ResponseEntity.ok() 63 | .contentType(MediaType.parseMediaType(mimeType)) 64 | // .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;fileName="+resource.getFilename()) 65 | .header(HttpHeaders.CONTENT_DISPOSITION, "inline;fileName=" + doc.getFileName()) 66 | .body(doc.getDocFile()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /clientservice/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /fileUploadDownload/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /fileUploadDownload/src/test/java/com/greenlearner/fileUploadDownload/controller/UploadDownloadWithFileSystemControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload.controller; 2 | 3 | import com.greenlearner.fileUploadDownload.service.FileStorageService; 4 | import org.assertj.core.internal.bytebuddy.matcher.ElementMatchers; 5 | import org.junit.jupiter.api.DisplayName; 6 | import org.junit.jupiter.api.Test; 7 | import org.mockito.ArgumentMatchers; 8 | import org.mockito.BDDMockito; 9 | import org.mockito.Mockito; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 12 | import org.springframework.boot.test.context.SpringBootTest; 13 | import org.springframework.boot.test.mock.mockito.MockBean; 14 | import org.springframework.core.io.Resource; 15 | import org.springframework.http.HttpHeaders; 16 | import org.springframework.mock.web.MockMultipartFile; 17 | import org.springframework.test.web.servlet.MockMvc; 18 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 19 | import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 20 | import org.springframework.web.multipart.MultipartFile; 21 | 22 | import java.io.ByteArrayInputStream; 23 | import java.io.File; 24 | import java.io.IOException; 25 | import java.io.InputStream; 26 | import java.net.URI; 27 | import java.net.URL; 28 | 29 | import static org.mockito.ArgumentMatchers.any; 30 | import static org.mockito.ArgumentMatchers.anyString; 31 | import static org.mockito.BDDMockito.then; 32 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; 33 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 34 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 35 | 36 | /** 37 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) 38 | */ 39 | 40 | @SpringBootTest 41 | @AutoConfigureMockMvc 42 | public class UploadDownloadWithFileSystemControllerTest { 43 | 44 | @Autowired 45 | private MockMvc mockMvc; 46 | 47 | @MockBean 48 | private FileStorageService fileStorageService; 49 | 50 | @Test 51 | @DisplayName("test to upload single file") 52 | void shouldUploadSingleFile() throws Exception { 53 | 54 | Mockito.when(fileStorageService.storeFile(any(MultipartFile.class))).thenReturn("test-file.txt"); 55 | 56 | MockMultipartFile mmf = new MockMultipartFile("file", "test-file.txt", 57 | "text/plain" , "Green Learner - Arvind".getBytes()); 58 | 59 | this.mockMvc.perform(multipart("/single/upload").file(mmf)) 60 | .andExpect(status().isOk()) 61 | .andExpect(content().json("{\"fileName\":test-file.txt,\"contentType\":\"text/plain\",\"url\":\"http://localhost/download/test-file.txt\"}")); 62 | 63 | then(this.fileStorageService).should().storeFile(mmf); 64 | 65 | } 66 | 67 | @Test 68 | @DisplayName("single file download test") 69 | void singleFileDownload() throws Exception{ 70 | Resource resource = getMockedResource(); 71 | Mockito.when(fileStorageService.downloadFile(anyString())).thenReturn(resource); 72 | 73 | mockMvc.perform(get("/download/abc.jpg")) 74 | .andExpect(status().isOk()) 75 | .andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION, "inline;fileName="+resource.getFilename())) 76 | .andExpect(content().bytes("Green Learner".getBytes())); 77 | 78 | BDDMockito.then(fileStorageService).should().downloadFile("abc.jpg"); 79 | } 80 | 81 | private Resource getMockedResource() { 82 | Resource resource = new Resource() { 83 | @Override 84 | public boolean exists() { 85 | return false; 86 | } 87 | 88 | @Override 89 | public URL getURL() throws IOException { 90 | return null; 91 | } 92 | 93 | @Override 94 | public URI getURI() throws IOException { 95 | return null; 96 | } 97 | 98 | @Override 99 | public File getFile() throws IOException { 100 | File file = new File("dummy.jpeg"); 101 | System.out.println(file.getAbsolutePath()); 102 | return file; 103 | } 104 | 105 | @Override 106 | public long contentLength() throws IOException { 107 | return 0; 108 | } 109 | 110 | @Override 111 | public long lastModified() throws IOException { 112 | return 0; 113 | } 114 | 115 | @Override 116 | public Resource createRelative(String relativePath) throws IOException { 117 | return null; 118 | } 119 | 120 | @Override 121 | public String getFilename() { 122 | return "dummy.jpeg"; 123 | } 124 | 125 | @Override 126 | public String getDescription() { 127 | return null; 128 | } 129 | 130 | @Override 131 | public InputStream getInputStream() throws IOException { 132 | return new ByteArrayInputStream("Green Learner".getBytes()); 133 | } 134 | }; 135 | return resource; 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /fileUploadDownload/src/main/java/com/greenlearner/fileUploadDownload/controller/UploadDownloadWithFileSystemController.java: -------------------------------------------------------------------------------- 1 | package com.greenlearner.fileUploadDownload.controller; 2 | 3 | import com.greenlearner.fileUploadDownload.dto.FileUploadResponse; 4 | import com.greenlearner.fileUploadDownload.service.FileStorageService; 5 | import org.springframework.core.io.Resource; 6 | import org.springframework.http.HttpHeaders; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.util.StreamUtils; 10 | import org.springframework.web.bind.annotation.*; 11 | import org.springframework.web.multipart.MultipartFile; 12 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder; 13 | 14 | import javax.servlet.http.HttpServletRequest; 15 | import javax.servlet.http.HttpServletResponse; 16 | import java.io.IOException; 17 | import java.util.ArrayList; 18 | import java.util.Arrays; 19 | import java.util.List; 20 | import java.util.zip.ZipEntry; 21 | import java.util.zip.ZipOutputStream; 22 | 23 | /** 24 | * @author - GreenLearner(https://www.youtube.com/c/greenlearner) 25 | */ 26 | 27 | @RestController 28 | public class UploadDownloadWithFileSystemController { 29 | 30 | private FileStorageService fileStorageService; 31 | 32 | public UploadDownloadWithFileSystemController(FileStorageService fileStorageService) { 33 | this.fileStorageService = fileStorageService; 34 | } 35 | 36 | @PostMapping("single/upload") 37 | FileUploadResponse singleFileUplaod(@RequestParam("file") MultipartFile file) { 38 | 39 | String fileName = fileStorageService.storeFile(file); 40 | 41 | ///http://localhost:8081/download/abc.jpg 42 | String url = ServletUriComponentsBuilder.fromCurrentContextPath() 43 | .path("/download/") 44 | .path(fileName) 45 | .toUriString(); 46 | 47 | String contentType = file.getContentType(); 48 | 49 | FileUploadResponse response = new FileUploadResponse(fileName, contentType, url); 50 | 51 | return response; 52 | 53 | } 54 | 55 | @GetMapping("/download/{fileName}") 56 | ResponseEntity downLoadSingleFile(@PathVariable String fileName, HttpServletRequest request) { 57 | 58 | Resource resource = fileStorageService.downloadFile(fileName); 59 | 60 | // MediaType contentType = MediaType.APPLICATION_PDF; 61 | 62 | String mimeType; 63 | 64 | try { 65 | mimeType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath()); 66 | } catch (IOException e) { 67 | mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE; 68 | } 69 | mimeType = mimeType == null ? MediaType.APPLICATION_OCTET_STREAM_VALUE : mimeType; 70 | 71 | return ResponseEntity.ok() 72 | .contentType(MediaType.parseMediaType(mimeType)) 73 | // .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;fileName="+resource.getFilename()) 74 | .header(HttpHeaders.CONTENT_DISPOSITION, "inline;fileName=" + resource.getFilename()) 75 | .body(resource); 76 | } 77 | 78 | @PostMapping("/multiple/upload") 79 | List multipleUpload(@RequestParam("files") MultipartFile[] files) { 80 | 81 | if (files.length > 7) { 82 | throw new RuntimeException("too many files"); 83 | } 84 | List uploadResponseList = new ArrayList<>(); 85 | Arrays.asList(files) 86 | .stream() 87 | .forEach(file -> { 88 | String fileName = fileStorageService.storeFile(file); 89 | 90 | ///http://localhost:8081/download/abc.jpg 91 | String url = ServletUriComponentsBuilder.fromCurrentContextPath() 92 | .path("/download/") 93 | .path(fileName) 94 | .toUriString(); 95 | 96 | String contentType = file.getContentType(); 97 | 98 | FileUploadResponse response = new FileUploadResponse(fileName, contentType, url); 99 | uploadResponseList.add(response); 100 | }); 101 | 102 | return uploadResponseList; 103 | } 104 | 105 | @GetMapping("zipDownload") 106 | void zipDownload(@RequestParam("fileName") String[] files, HttpServletResponse response) throws IOException { 107 | //zipoutstream - zipentry+zipentry 108 | 109 | try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { 110 | Arrays.asList(files) 111 | .stream() 112 | .forEach(file -> { 113 | Resource resource = fileStorageService.downloadFile(file); 114 | 115 | ZipEntry zipEntry = new ZipEntry(resource.getFilename()); 116 | 117 | try { 118 | zipEntry.setSize(resource.contentLength()); 119 | zos.putNextEntry(zipEntry); 120 | 121 | StreamUtils.copy(resource.getInputStream(), zos); 122 | 123 | zos.closeEntry(); 124 | } catch (IOException e) { 125 | System.out.println("some exception while ziping"); 126 | } 127 | }); 128 | zos.finish(); 129 | 130 | } 131 | 132 | response.setStatus(200); 133 | response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;fileName=zipfile"); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /clientservice/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /fileUploadDownload/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | --------------------------------------------------------------------------------