My footsteps
RestController / 10 본문
728x90
- 시스템물리경로...(?)
- 업로드 경로를 리얼패스 경로로 바꿔줘야 한다
- 디렉토리 알아서 만들어주는 애들 (마지막 경로에 해당되는 디렉토리(폴더)를 만들수 있다)
pathFile.mkdir();
pathFile.mkdirs();
- 요 코드를 넣어주면 파일이 실제로 저장된다
file.transferTo(pathFile);
- 윈도우즈는 경로를 "\\" 이렇게 표현한다
//존재하는데 디렉토리 만들어버리면 불필요하니까 조건처리
if(!pathFile.exists())
pathFile.mkdirs();
- value라는 어노테이션을 써서 파일을 읽어올수 있다
@Value("${upload.a}")
private String uploadPath;
- 이미지 url 경로 얻는 방법
return Paths.get(uploadPath,fileName).toString();
let imgUrl = await response.imgUrl();
- 파일 크기 리미트 거는 방법 (yml에함)
spring:
servlet:
multipart:
//각 파일의 max 사이즈
max-file-size: 100MB
//한번 요청에 보낼수 있는 크기
max-request-size: 500MB
//이름을 같게 하면 배열로 받는다
formData.append("file",file);
formData.append("file",file1);
formData.append("file",file2);
@PostMapping("{id}/image")
public List<String> add(@RequestParam("file") MultipartFile[] files, @PathVariable int id) throws IOException {
List<String> returnFiles = new ArrayList<>();
for(MultipartFile file : files)
{
String fileName = file.getOriginalFilename();
System.out.println(file.getOriginalFilename());
//String uploadPath = "/upload/menu/c/1/2023/";
Resource resource = resourceLoader.getResource(uploadPath);
// Unix에서는 파일과 폴더가 구분되어 있지 않다
File pathFile = resource.getFile();
//pathFile.mkdir();
//존재하는데 디렉토리 만들어버리면 불필요하니까 조건처리
if(!pathFile.exists())
pathFile.mkdirs();
//안좋은 방법
//String realPath = pathFile.getAbsolutePath()+"\\"+fileName;
String realPath = Paths.get(pathFile.getAbsolutePath(),fileName).toString();
System.out.println(realPath);
file.transferTo(new File(realPath));
returnFiles.add(Paths.get(uploadPath,fileName).toString());
}
return returnFiles;
}
- 파일 여러개 올리기
//imgFiles는 상단에 전역변수로둠
imgFiles = e.dataTransfer.files;
let formData = new FormData();
for (let file of imgFiles)
formData.append("file", file);
formData.append("file", file);
formData.append("file", file);
formData.append("file", file);
- 서버는 하고있는 역할에 대한 이름이고 제품에 대한 이름은 아니다
- 호스트컴퓨터 : 가상컴퓨터
- webapps가 home(홈디렉토리)이다
728x90
'국비수업 > 수업정리' 카테고리의 다른 글
스프링 예외처리 + 유닉스 (0) | 2023.06.30 |
---|---|
RestController / 11 (0) | 2023.06.29 |
RestController / 9 (0) | 2023.06.27 |
RestController / 8 (0) | 2023.06.26 |
RestController / 7 (0) | 2023.06.23 |