├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── javatechie
│ │ ├── StorageServiceApplication.java
│ │ ├── entity
│ │ └── ImageData.java
│ │ ├── respository
│ │ └── StorageRepository.java
│ │ ├── service
│ │ └── StorageService.java
│ │ └── util
│ │ └── ImageUtils.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── javatechie
└── StorageServiceApplicationTests.java
/README.md:
--------------------------------------------------------------------------------
1 | # file-storage-service
2 | Spring Boot File Upload and Download REST API |Store Images in Database
3 |
--------------------------------------------------------------------------------
/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 | public static void main(String[] args) {
40 | SpringApplication.run(StorageServiceApplication.class, args);
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/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/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/java/com/javatechie/service/StorageService.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.service;
2 |
3 | import com.javatechie.entity.ImageData;
4 | import com.javatechie.respository.StorageRepository;
5 | import com.javatechie.util.ImageUtils;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 | import org.springframework.web.multipart.MultipartFile;
9 |
10 | import java.io.IOException;
11 | import java.util.Optional;
12 |
13 | @Service
14 | public class StorageService {
15 |
16 | @Autowired
17 | private StorageRepository repository;
18 |
19 | public String uploadImage(MultipartFile file) throws IOException {
20 |
21 | ImageData imageData = repository.save(ImageData.builder()
22 | .name(file.getOriginalFilename())
23 | .type(file.getContentType())
24 | .imageData(ImageUtils.compressImage(file.getBytes())).build());
25 | if (imageData != null) {
26 | return "file uploaded successfully : " + file.getOriginalFilename();
27 | }
28 | return null;
29 | }
30 |
31 | public byte[] downloadImage(String fileName){
32 | Optional dbImageData = repository.findByName(fileName);
33 | byte[] images=ImageUtils.decompressImage(dbImageData.get().getImageData());
34 | return images;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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/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 |
--------------------------------------------------------------------------------