QUESTIONS? CALL: (+84) 24 666 026 12

HiveTech Solutions

HiveTech Solutions

HiveTech is a technology company focus on AI and big data technologies for educational purposes.

T (+84) 24 666 026 12
Email: contact@hivetech.vn

HiveTech JSC
No. 98 Hoang Quoc Viet Str, Cau Giay Dist, Ha Noi

Open in Google Maps
  • HOME
  • SERVICES
  • OUR MISSIONS & VALUES
  • PARTNERS
  • CONTACT
  • NEWS & EVENTS
  • RECRUITMENTS
  • Home
  • Uncategorized
  • File upload trong Spring
2023-05-31

File upload trong Spring

by Thao Nguyen / Wednesday, 30 December 2020 / Published in Uncategorized















Trong quá trình xây dựng một ứng dụng web nói chung. Việc lưu trữ, xử lý các tệp tin từ các request của người dùng là một tác vụ cơ bản cần phải có. Trong các ứng dụng Java web cũng không là ngoại lệ. Bài viết này mình xin giới thiệu cách xây dựng một ứng dụng Spring Web MCV có khả năng xử lý và lưu trữ các tệp tin được gửi lên thông qua giao thức HTTP.

Bài viết này sẽ tập trung vào cách Xư lý file được upload multilpart-form cùng một số phương pháp lưu trữ cơ bản.

Cấu hình

Xây dựng bằng Spring MVC

Spring cho phép ta xử lý file thông qua một đối tượng MultipartResolver mà Spring cung cấp, Và để nó có thể hoạt động, ta cần phải cấu hình chúng.

Đầu tiên chúng ta cần 1 dependency sau 

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

Tiếp sau đó ta cần định nghĩa CommonsMultipartResolver Bean vào trong file cấu hình Spring của chúng ta

Java class annotation config

@Configuration
@EnableWebMvc
@ComponentScan(“hivetech.sang.*”)
public class MutilpartFileConfig {
   
    @Bean(name = “multipartResolver”)
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100000);
        return multipartResolver;
    }
   
}

XML config

<beans xmlns=”https://www.springframework.org/schema/beans”
       xmlns:xsi=”https://www.w3.org/2001/XMLSchema-instance”
       xmlns:context=”https://www.springframework.org/schema/context”
       xsi:schemaLocation=”https://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       https://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd”>
 
    <context:component-scan base-package=”hivetech.sang.*”/>
    <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”>
        <property name=”maxUploadSize” value=”1000000″/>
    </bean>
</beans>

Đối tượng CommonsMultipartResolver có nhiều các property khác nhau như: uploadTempDir, defaultEncoding, maxUploadFilePerSize,… Tuỳ vào nhu cầu ta có thể thêm các trường này để config như mong muốn

Vậy là ta đã cấu hình xong. Bây giờ ứng dụng Spring WebMVC của chúng ta đã có thể hỗ trợ upload file như mong muốn.

Xây dựng bằng Spring boot

Với một ứng dụng Spring Boot. Mọi thứ ở trên đều được hỗ trợ. Thậm chí Spring Boot còn khiến nó trở nên dễ dàng hơn nữa khi mà ta không cần phải thực hiện bất cứ công đoạn cấu hình nào. Mọi thứ đã được Spring tự động thêm vào khi chúng ta thêm Web module của ứng dụng Spring boot

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

 Việc còn lại ta cần làm là tuỳ chỉnh các thông số tương tự như với cách mà Spring MVC làm. Tất cả sẽ được định nghĩa trong file application.properties

spring.servlet.multipart.max-file-size=128KBspring.servlet.multipart.max-request-size=128KB
 #We can also control whether file uploading is enabled, and the location for temp file upload:
spring.servlet.multipart.enabled=true
#define the upload location by variable so that we can use the temporary location for different operating systems.spring.servlet.multipart.location=${java.io.tmpdir}

Các cách lưu trữ file

Lưu trữ file có nhiều phương pháp khác nhau, Tuỳ từng trường hợp ta có thể lựa chọn các cách lưu trữ phù hợp, Các cách lưu trữ phổ biến nhất hiện nay ta có thể kể tới:

  • Lưu trữ trực tiếp vào bộ nhớ của server và lưu tên file vào cơ sở dữ liệu
  • Lưu trữ thẳng vào database dưới dạng binary 
  • Lưu trữ trên các nền tảng đám mây, các server lưu trữ chuyên biệt như GoogleDrive, MinIO, AWS S3,… thông qua các API được cung cấp

Lưu trữ trực tiếp

Đây là phương pháp thường thấy trong các ứng dụng nhỏ, dung lượng các file không lớn do sự đơn giản của nó. Tuy nhiên phương pháp này có nhiều nhược điểm khi mà dữ liệu được lưu trực tiếp sẽ gây tốn cho tài nguyên máy chủ, cùng với đó sẽ khó nâng cấp được khi hệ thống  mở rộng

Ta tạo một đối tượng để có thể lấy ra thông tin về các custom setting được chúng ta tự định nghĩa trong file application.properties

Spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
storage.location=D:/storage
mimes.type=image/jpg,image/png

Tạo một object để đọc config ở trên

@ConfigurationProperties(prefix = “storage”)
public class FileStoreConfig {
    /**
     * Folder location for storing files
     */
    private String location;
 
    public String getLocation() {
        return location;
    }
 
    public void setLocation(String location) {
        this.location = location;
    }
}

Tiếp đó ta sẽ tạo một đối tượng Service gồm các function cơ bản như lưu file, hiển thị

@Configuration
@EnableWebMvc
@ComponentScan("hivetech.sang.*")
public class MutilpartFileConfig {
   
    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100000);
        return multipartResolver;
    }
   
}

Tiếp đó ta sẽ tạo một Controller để xử lý request

@RestController
public class FileManagerLocal {
 
    @Autowired
    private FileManagerLocalService fileManagerLocalService;
 
    @GetMapping(“/hello”)
    public String hello() {
        return “hello”;
    }
 
    @GetMapping(“/list-file”)
    public List<Object> listUploadedFiles() throws IOException {
        return Arrays.asList(fileManagerLocalService.loadAll().toArray());
    }
 
    @GetMapping(“/files/{filename:.+}”)
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
 
        Resource file = fileManagerLocalService.loadAsResource(filename);
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
                “attachment; filename=\”” + file.getFilename() + “\””).body(file);
    }
 
    @PostMapping(“/add-file”)
    public ResponseEntity handleFileUpload(@RequestParam(“file”) MultipartFile[] file, @RequestParam(“description”) String desc) {
        return ResponseEntity.ok().body(fileManagerLocalService.store(file));
    }
 
    @ExceptionHandler(FileStoreException.class)
    public ResponseEntity<?> handleStorageFileNotFound(FileStoreException exc) {
        return new ResponseEntity(exc.getMessage(), HttpStatus.I_AM_A_TEAPOT);
    }
}

Ta sẽ test ứng dụng bằng Postman như sau 

Lưu trữ trực tiếp vào database dưới dạng binary

Đây là phương pháp ít được sử dụng hơn do có nhiều nhược điểm như giới hạn về dung lượng, gây khó khăn trong truy xuất dữ liệu,… Tuy nhiên nếu như các tệp tin nhỏ có thể sử dụng phương pháp này nhưng không khuyến khích

Đầu tiên ta tạo một Entity với một trường annotation Lob với kiểu dữ liệu byte[]

@Table(name = "file_entity")public class FileEntity {
@Id
@GeneratedValue
@Column(name = "id")private Long id; 
private String name; 
private String type;
@Lob
private byte[] content;
}

Tiếp đó ta tạo một Repository để thao tác với CSDL

@Repository
public interface FileDBRepo extends JpaRepository<FileEntity, Long> {
    FileEntity findByNameEquals(String filename);
    void deleteByNameEquals(String fileName);
}

Rồi sau đó ta cũng tạo một đối tượng Service cùng một số function cơ bản

@Service
public class FileManagerDBServiceIlpm implements FileManagerDBService {
    @Autowired
    FileDBRepo fileDBRepo;
 
    @Override
    public FileEntity store(MultipartFile file) {
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
        try {
            FileEntity FileDB = FileEntity.builder()
                    .name(fileName)
                    .type(file.getContentType())
                    .content(file.getBytes())
                    .build();
            return fileDBRepo.save(FileDB);
        } catch (IOException e) {
            throw new FileStoreException(“Cant save file with cause “+e.getMessage(), e);
        }
    }
 
    @Override
    public List<FileEntity> store(MultipartFile[] files) {
        return Arrays.asList(files).stream().map(this::store).collect(Collectors.toList());
    }
 
    @Override
    public void delete(String filename) {
        fileDBRepo.deleteByNameEquals(filename);
    }
 
    @Override
    public Resource loadAsResource(String filename) {
        return new ByteArrayResource(fileDBRepo.findByNameEquals(filename).getContent());
    }
}

Tạo Controller để xử lý Request 

@RestController()
@RequestMapping(path = “/db-manager”)
public class FileManagerDB {
    @Autowired
    FileManagerDBService fileManagerDBService;
 
    @GetMapping(“/hello”)
    public String hello() {
        return “hello”;
    }
 
    @GetMapping(“/files/{filename:.+}”)
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
 
        Resource file = fileManagerDBService.loadAsResource(filename);
        return ResponseEntity.ok().body(file);
    }
 
    @PostMapping(“/add-file”)
    public ResponseEntity handleFileUpload(@RequestParam(“file”) MultipartFile[] file, @RequestParam(“description”) String desc) {
        return ResponseEntity.ok().body(fileManagerDBService.store(file));
    }
}
 

Ta tiến hành upload một ảnh 

Dữ liệu đã được lưu trong database dưới dạng nhị phân

Kiểu dữ liệu Blob trong mySQL có dung lượng lưu trữ lên tới 4GB (LONGBLOB)

Kết luận

Từ các ví dụ trên ta có thể thấy các cách lưu trữ khác nhau và tự rút ra được ưu nhược điểm. Cùng với đó là cách khởi tạo mà triển khai một ứng dụng Spring web hỗ trợ thao tác và xử lý file.

Ngoài ra chủ đề về upload và lưu trữ file trên các nền tảng đám mây sẽ được bổ sung trong 1 chủ đề khác.

Phần code chi tiết có thể xem tại đây Link

Phạm Đức Sang

  • Tweet
Tagged under: #Spring_MVC

About Thao Nguyen

What you can read next

Nhân viên Kế toán tổng hợp
HIVETECH-ER WAY OF LIVE
AI CŨNG CÓ THỂ TRỞ THÀNH CODER VỚI NHỮNG WEBSITE HỌC LẬP TRÌNH MIỄN PHÍ

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Thực Tập Sinh Nhân Sự Tổng Hợp

    1. Mô Tả Công Việc Hỗ trợ các công việc hành ch...
  • DevOps Engineer (AWS/Azure/GCP)

    1. Mô Tả Công Việc Nghiên cứu, xây dựng và triể...
  • Tester/Quality Assurance Engineer

    1. Mô Tả Công Việc Tham gia các dự án có domain...
  • Giảng Viên Tiếng Anh nội bộ

    1. Mô Tả Công Việc Trực tiếp tham gia giảng dạy...
  • CHỦ TỊCH HIVETECH VIỆT NAM NGÔ THÀNH LÊ: “HÃY TRỞ THÀNH MỘT NHÂN VIÊN GIỎI TRƯỚC KHI KHỞI NGHIỆP”

    Từ bỏ vị trí công việc ổn định tại doanh nghiệp...

Recent Comments

  • hivequeen on Intern/Fresher ReactJS/VueJS Developer
  • Khuất Anh Tuấn on Intern/Fresher ReactJS/VueJS Developer

Archives

  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • November 2019
  • September 2019
  • July 2019
  • June 2019

Categories

  • Mobile
  • Networking
  • Posts
  • Recruitment
  • Sale
  • Technology
  • Uncategorized

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Featured Posts

  • Thực Tập Sinh Nhân Sự Tổng Hợp

    0 comments
  • DevOps Engineer (AWS/Azure/GCP)

    0 comments
  • Tester/Quality Assurance Engineer

    0 comments
  • Giảng Viên Tiếng Anh nội bộ

    0 comments
  • CHỦ TỊCH HIVETECH VIỆT NAM NGÔ THÀNH LÊ: “HÃY TRỞ THÀNH MỘT NHÂN VIÊN GIỎI TRƯỚC KHI KHỞI NGHIỆP”

    0 comments
  • DISCLAIMER
  • SUPPORT POLICY
  • LEGAL
HiveTech Solutions

© HiveTech 2019 All rights reserved

TOP