Angular 5 – Spring Boot 서버에서 MultipartFile 업로드/가져오기
Angular 5 – Spring Boot 서버에서 MultipartFile 업로드/받기
Servlet 컨테이너에 파일을 업로드할 때 애플리케이션은 MultipartConfigElement 클래스를 등록해야 합니다. 그러나 Spring Boot는 자동으로 구성하여 더 쉽게 만들 수 있습니다. 이 튜토리얼에서는 Spring Boot RestApi 서버에서 MultipartFile을 업로드/가져오기 위해 Angular 5 앱 클라이언트를 빌드하는 방법을 살펴보겠습니다.
관련 게시물:
I. 기술
Spring Tool Suite – 버전 3.8.1.RELEASE
II. 개요
1. 스프링 부트 서버
StorageService
초기화, 모든 파일 삭제, 파일 저장, 파일 로드에 도움이 됨UploadController
는 StorageService
를 사용하여 Rest API 제공: 파일 POST, 모든 파일 GETpom.xml의 Spring Boot Starter 웹 종속성
2. Angular 5 앱 클라이언트
upload-file.service
는 파일을 스토리지로 푸시하고 파일을 가져오는 방법을 제공합니다. list-upload.component
파일 목록을 가져와서 표시합니다. form-upload.component
가 파일 업로드를 도와줍니다. details-upload.component
는 파일 목록의 각 항목에 대한 세부 정보입니다. III. 관행
1. 스프링 부트 서버
1.1 스프링 부트 프로젝트 생성
종속성 있음:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1.2 파일 시스템용 스토리지 서비스 생성
4가지 기능으로 StorageService를 만듭니다.
package com.javasampleapproach.uploadfiles.storage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.multipart.MultipartFile;
@서비스
공개 클래스 StorageService {
Logger log = LoggerFactory.getLogger(this.getClass().getName());
private final Path rootLocation = Paths.get("upload-dir");
public void store(MultipartFile file) {
try {
Files.copy(file.getInputStream(), this.rootLocation.resolve(file.getOriginalFilename()));
} catch (Exception e) {
throw new RuntimeException("FAIL!");
}
}
public Resource loadFile(String filename) {
try {
Path file = rootLocation.resolve(filename);
Resource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
} else {
throw new RuntimeException("FAIL!");
}
} catch (MalformedURLException e) {
throw new RuntimeException("FAIL!");
}
}
public void deleteAll() {
FileSystemUtils.deleteRecursively(rootLocation.toFile());
}
public void init() {
try {
Files.createDirectory(rootLocation);
} catch (IOException e) {
throw new RuntimeException("Could not initialize storage!");
}
}
}
1.3 업로드 컨트롤러 만들기
더 보기:
https://grokonez.com/java-integration/angular-5-upload-get-multipartfile-to-from-spring-boot-server
Angular 5 – Spring Boot 서버에서 MultipartFile 업로드/받기
Reference
이 문제에 관하여(Angular 5 – Spring Boot 서버에서 MultipartFile 업로드/가져오기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/loizenai/angular-5-upload-get-multipartfile-to-from-spring-boot-server-57kk텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)