├── README.md ├── src ├── test │ └── java │ │ └── com │ │ └── javatechie │ │ └── StorageServiceApplicationTests.java └── main │ ├── java │ └── com │ │ └── javatechie │ │ ├── respository │ │ ├── StorageRepository.java │ │ └── FileDataRepository.java │ │ ├── entity │ │ ├── FileData.java │ │ └── ImageData.java │ │ ├── util │ │ └── ImageUtils.java │ │ ├── StorageServiceApplication.java │ │ └── service │ │ └── StorageService.java │ └── resources │ └── application.properties └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | # file-storage -------------------------------------------------------------------------------- /src/test/java/com/javatechie/StorageServiceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.javatechie; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class StorageServiceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/respository/StorageRepository.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.respository; 2 | 3 | import com.javatechie.entity.ImageData; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.Optional; 7 | 8 | public interface StorageRepository extends JpaRepository { 9 | 10 | 11 | Optional findByName(String fileName); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url = jdbc:mysql://localhost:3306/javatechie 3 | spring.datasource.username = root 4 | spring.datasource.password = Password 5 | spring.jpa.show-sql = true 6 | spring.jpa.hibernate.ddl-auto = update 7 | spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 8 | server.port=9191 9 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/respository/FileDataRepository.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.respository; 2 | 3 | import com.javatechie.entity.FileData; 4 | import com.javatechie.entity.ImageData; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | import java.util.Optional; 8 | 9 | public interface FileDataRepository extends JpaRepository { 10 | Optional findByName(String fileName); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/entity/FileData.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import javax.persistence.*; 9 | @Entity 10 | @Table(name = "FILE_DATA") 11 | @Data 12 | @AllArgsConstructor 13 | @NoArgsConstructor 14 | @Builder 15 | public class FileData { 16 | 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | 21 | private String name; 22 | private String type; 23 | private String filePath; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/entity/ImageData.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import javax.persistence.*; 9 | 10 | @Entity 11 | @Table(name = "ImageData") 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | @Builder 16 | public class ImageData { 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | 21 | private String name; 22 | private String type; 23 | @Lob 24 | @Column(name = "imagedata",length = 1000) 25 | private byte[] imageData; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/util/ImageUtils.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.util; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.util.zip.Deflater; 5 | import java.util.zip.Inflater; 6 | 7 | public class ImageUtils { 8 | 9 | 10 | public static byte[] compressImage(byte[] data) { 11 | Deflater deflater = new Deflater(); 12 | deflater.setLevel(Deflater.BEST_COMPRESSION); 13 | deflater.setInput(data); 14 | deflater.finish(); 15 | 16 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); 17 | byte[] tmp = new byte[4*1024]; 18 | while (!deflater.finished()) { 19 | int size = deflater.deflate(tmp); 20 | outputStream.write(tmp, 0, size); 21 | } 22 | try { 23 | outputStream.close(); 24 | } catch (Exception ignored) { 25 | } 26 | return outputStream.toByteArray(); 27 | } 28 | 29 | 30 | 31 | public static byte[] decompressImage(byte[] data) { 32 | Inflater inflater = new Inflater(); 33 | inflater.setInput(data); 34 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); 35 | byte[] tmp = new byte[4*1024]; 36 | try { 37 | while (!inflater.finished()) { 38 | int count = inflater.inflate(tmp); 39 | outputStream.write(tmp, 0, count); 40 | } 41 | outputStream.close(); 42 | } catch (Exception ignored) { 43 | } 44 | return outputStream.toByteArray(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.7.3 9 | 10 | 11 | com.javatechie 12 | storage-service 13 | 0.0.1-SNAPSHOT 14 | storage-service 15 | Demo project for Spring Boot 16 | 17 | 1.8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-data-jpa 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | mysql 31 | mysql-connector-java 32 | runtime 33 | 34 | 35 | org.projectlombok 36 | lombok 37 | true 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-maven-plugin 51 | 52 | 53 | 54 | org.projectlombok 55 | lombok 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/StorageServiceApplication.java: -------------------------------------------------------------------------------- 1 | package com.javatechie; 2 | 3 | import com.javatechie.service.StorageService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.*; 11 | import org.springframework.web.multipart.MultipartFile; 12 | 13 | import java.io.IOException; 14 | 15 | @SpringBootApplication 16 | @RestController 17 | @RequestMapping("/image") 18 | public class StorageServiceApplication { 19 | 20 | @Autowired 21 | private StorageService service; 22 | 23 | @PostMapping 24 | public ResponseEntity uploadImage(@RequestParam("image")MultipartFile file) throws IOException { 25 | String uploadImage = service.uploadImage(file); 26 | return ResponseEntity.status(HttpStatus.OK) 27 | .body(uploadImage); 28 | } 29 | 30 | @GetMapping("/{fileName}") 31 | public ResponseEntity downloadImage(@PathVariable String fileName){ 32 | byte[] imageData=service.downloadImage(fileName); 33 | return ResponseEntity.status(HttpStatus.OK) 34 | .contentType(MediaType.valueOf("image/png")) 35 | .body(imageData); 36 | 37 | } 38 | 39 | 40 | @PostMapping("/fileSystem") 41 | public ResponseEntity uploadImageToFIleSystem(@RequestParam("image")MultipartFile file) throws IOException { 42 | String uploadImage = service.uploadImageToFileSystem(file); 43 | return ResponseEntity.status(HttpStatus.OK) 44 | .body(uploadImage); 45 | } 46 | 47 | @GetMapping("/fileSystem/{fileName}") 48 | public ResponseEntity downloadImageFromFileSystem(@PathVariable String fileName) throws IOException { 49 | byte[] imageData=service.downloadImageFromFileSystem(fileName); 50 | return ResponseEntity.status(HttpStatus.OK) 51 | .contentType(MediaType.valueOf("image/png")) 52 | .body(imageData); 53 | 54 | } 55 | 56 | public static void main(String[] args) { 57 | SpringApplication.run(StorageServiceApplication.class, args); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/service/StorageService.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.service; 2 | 3 | import com.javatechie.entity.FileData; 4 | import com.javatechie.entity.ImageData; 5 | import com.javatechie.respository.FileDataRepository; 6 | import com.javatechie.respository.StorageRepository; 7 | import com.javatechie.util.ImageUtils; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | import org.springframework.web.multipart.MultipartFile; 11 | 12 | import java.io.File; 13 | import java.io.IOException; 14 | import java.nio.file.Files; 15 | import java.util.Optional; 16 | 17 | @Service 18 | public class StorageService { 19 | 20 | @Autowired 21 | private StorageRepository repository; 22 | 23 | @Autowired 24 | private FileDataRepository fileDataRepository; 25 | 26 | private final String FOLDER_PATH="/Users/javatechie/Desktop/MyFIles/"; 27 | 28 | public String uploadImage(MultipartFile file) throws IOException { 29 | ImageData imageData = repository.save(ImageData.builder() 30 | .name(file.getOriginalFilename()) 31 | .type(file.getContentType()) 32 | .imageData(ImageUtils.compressImage(file.getBytes())).build()); 33 | if (imageData != null) { 34 | return "file uploaded successfully : " + file.getOriginalFilename(); 35 | } 36 | return null; 37 | } 38 | 39 | 40 | 41 | public byte[] downloadImage(String fileName) { 42 | Optional dbImageData = repository.findByName(fileName); 43 | byte[] images = ImageUtils.decompressImage(dbImageData.get().getImageData()); 44 | return images; 45 | } 46 | 47 | 48 | public String uploadImageToFileSystem(MultipartFile file) throws IOException { 49 | String filePath=FOLDER_PATH+file.getOriginalFilename(); 50 | 51 | FileData fileData=fileDataRepository.save(FileData.builder() 52 | .name(file.getOriginalFilename()) 53 | .type(file.getContentType()) 54 | .filePath(filePath).build()); 55 | 56 | file.transferTo(new File(filePath)); 57 | 58 | if (fileData != null) { 59 | return "file uploaded successfully : " + filePath; 60 | } 61 | return null; 62 | } 63 | 64 | public byte[] downloadImageFromFileSystem(String fileName) throws IOException { 65 | Optional fileData = fileDataRepository.findByName(fileName); 66 | String filePath=fileData.get().getFilePath(); 67 | byte[] images = Files.readAllBytes(new File(filePath).toPath()); 68 | return images; 69 | } 70 | 71 | 72 | 73 | } 74 | --------------------------------------------------------------------------------