java

์Šคํ”„๋ง - ์œ ํšจ์„ฑ๊ฒ€์‚ฌ, ํŒŒ์ผ์—…๋กœ๋“œ

dev_summer 2021. 2. 27. 14:05

 

 

 

๋‚˜๋Š” ์•„๋ฌด ์ƒ๊ฐ์ด ์—†๋‹ค

๊ทธ๋ƒฅ ์•„๋ฌด ์ƒ๊ฐ ํ•˜๊ธฐ ์‹ซ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค

 

 

 

1. ์œ ํšจ์„ฑ๊ฒ€์‚ฌ

@Valid

๊ฒŸ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์„๋•Œ ๊ฐ’์ด ๋„์ด๊ฑฐ๋‚˜ ๊ณต๋ฐฑ์ด๊ฑฐ๋‚˜ ๋“ฑ๋“ฑ์„ ๊ฒ€์‚ฌํ•˜์—ฌ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ์ง€๋งŒ ์Šคํ”„๋ง์—์„œ๋Š” ๋ญ๊ฐ€ ์•ˆ๋“ค์–ด์™”๋Š”์ง€ ์ž๋™์œผ๋กœ ์•Œ๋ ค์ฃผ๊ธฐ๋„ ํ•œ๋‹ค.

 

 

 

* ์•ˆ๋“œ๋กœ์ด๋“œ๊ฐœ๋ฐœ์€ http๋ฅผ ๋ฆฌํ„ด ๋ฐ›์„ ํ•„์š”๊ฐ€ ์—†๊ณ  ๋ฐ์ดํ„ฐ๋งŒ ๋ฆฌํ„ด ๋ฐ›์œผ๋ฉด ๋œ๋‹ค.

 

* ์ •๋ฆฌ ์ฐธ ์ž˜ํ•ด๋†“์•˜๋„ค ์ฐธ๊ณ ํ•ด์•ผ๊ฒ ๋‹ค

https://victorydntmd.tistory.com/179

* ์•ˆ๋“œ๋กœ์ด๋“œ๊ฐœ๋ฐœ์€ http๋ฅผ ๋ฆฌํ„ด ๋ฐ›์„ ํ•„์š”๊ฐ€ ์—†๊ณ  ๋ฐ์ดํ„ฐ๋งŒ ๋ฆฌํ„ด ๋ฐ›์œผ๋ฉด ๋œ๋‹ค.

 

https://www.baeldung.com/

 

Java, Spring and Web Development tutorials

Java, Spring and Web Development tutorials

www.baeldung.com

* ์˜ค๋ฅ˜๋ฉ”์‹œ์ง€ ์ฐธ๊ณ  ์‚ฌ์ดํŠธ

www.baeldung.com  

 

Java, Spring and Web Development tutorials

Java, Spring and Web Development tutorials

www.baeldung.com

 

 

 

 

 

 

	@PostMapping("/mem/api/joinproc")
	public @ResponseBody ResponseEntity<String> memApiJoin(@Valid @RequestBody RequestMemJoinDto requestMemJoinDto) {
		// memํ˜•ํƒœ๋กœ ๋ฐ›์„์ˆ˜๋„์žˆ๊ฒ ์ง€๋งŒ, DTO๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ›๋Š”๋‹ค. mem์€ ๋””๋น„์šฉ
		// ๋ฆฌํ€˜์ŠคํŠธ๋ฐ”๋””,๋ฒ„ํผ๋กœ ๋ฐ›๋Š”๋‹ค. ์›๋ž˜ ์Šคํ”„๋ง์€ ๋ชป๋ฐ›์ง€๋งŒ ์žญ์Šจ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ›์•„์ค€๋‹ค.

//		int result = memRepository.save(requestMemJoinDto);
//		
//		
//		if (result == 1) {
//			return new ResponseEntity<String>("ok", HttpStatus.CREATED); // ์›๋ž˜๋Š” ๋ทฐ๋ฆฌ์กธ๋ธŒ๊ฐ€ ๊ด€์—ฌํ•˜๊ธฐ๋•Œ๋ฌธ์— @ResponseBody ๋กœ ๋ฐ์ดํ„ฐ๋กœ
//																			// ์ธ์‹ํ•˜๊ฒŒ ํ•ด์•ผํ•จ.
//		} else {
//			return new ResponseEntity<String>("fail", HttpStatus.BAD_REQUEST);
//		}

		return new ResponseEntity<String>("ok", HttpStatus.CREATED);	
	}

 

 

- RequestMemJoinDto.java

package com.cos.springboot.dto;

import javax.validation.constraints.NotBlank;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder

public class RequestMemJoinDto {
	@NotBlank(message ="์œ ์ €๋„ค์ด๋ฏธ ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
	private String username;
	@NotBlank(message ="ํŒจ์Šค์›Œ๋“œ๊ฐ€ ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
	private String password;
	@NotBlank(message ="์ด๋ฉ”์ผ์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค")
	private String email;
}

 

 

 

 

 

 

 

 

 

 

- >๋ณด๊ธฐ ์ข‹๊ฒŒ ๊ฐ€๊ณตํ•˜๊ธฐ

 

@Valid, BindingResult bindResult๋Š” ์…‹ํŠธ์ด๋‹ค.

Valid๊ฐ€ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋‚š์•„์ฑ„์„œ BindingResult๊ฐ์ฒด์— ์™ ๋“ค์–ด๊ฐ„๋‹ค.

 

 

 

 

 

 

2. ์ด๋ฏธ์ง€ ํŒŒ์ผ์—…๋กœ๋“œ

p487

ํŒŒ์ผ์„ ๋ณด๋‚ผ๋•Œ์—๋Š” PUT์œผ๋กœ ๋ณด๋‚ด๊ธฐ๋กœ ์•ฝ์†ํ–ˆ๋‹ค.

 

1) form๋ฐฉ์‹์˜ ํŒŒ์ผ ์—…๋กœ๋“œ <multipart/form ์‚ฌ์šฉ>

- imagesController.java ๋งŒ๋“ค๊ธฐ

@Controller
public class imagesController {
	@PutMapping("/image/upload")
	public @ResponseBody String imageUpload(@RequestParam("imgFile") MultipartFile imgFile) {
		// 1. imgfile ์ถœ๋ ฅ
		Path filePath=Paths.get("C:/src/springbootWork/springboot-test/img/"+imgFile.getOriginalFilename());
		try {
			Files.write(filePath, imgFile.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "ok";
	}
}

์™ธ๋ถ€์— ์ €์žฅํด๋”๋ฅผ ์žก์•„์•ผ ๋น ๋ฅด๋‹ค.

static์— ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์œผ๋ฉด target์œผ๋กœ ๋„˜๊ธธ๋•Œ์˜ ์‹œ๊ฐ„์ฐจ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœํ™˜๊ฒฝ์—์„œ ์—‘๋ฐ•์ด ๋œจ๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.(๋™์ ์œผ๋กœ๋“ค์–ด์˜ค๋Š” ํŒŒ์ผ์— ํ•œํ•จ) ๋ฐฐํฌํ•˜๊ณ ๋‚˜์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ, ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ์œ„ํ•ด static๊ณผ target์ด ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์˜ฌ๋•Œ ์•„์˜ˆ ์™ธ๋ถ€๋ฅผ ํ•จ๊ป˜ ๋ฐ”๋ผ๋ณด๋ฉด ๋œ๋‹ค.

 

 

 

ํฌ์ŠคํŠธ๋งจ์œผ๋กœ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ธฐ!

 

 

 

*์™ธ๋ถ€ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

ํƒ€์ผ“์ด ๋ชป๋Œ์–ด์˜ค๋ฉด ์™ธ๋ถ€ํŒŒ์ผ

- yml ์ˆ˜์ •

file:
  path: C:/src/springbootWork/springboot-test/media

 

 

 

- imagesController

@Controller
public class imagesController {
	
	@Value("${file.path}") // ์ฃผ์ž…
	private String fileRealPath;
	
	@PutMapping("/image/upload")
	public @ResponseBody String imageUpload(@RequestParam("imgFile") MultipartFile imgFile) {
		// 1. imgfile ์ถœ๋ ฅ
		
		System.out.println(imgFile.getOriginalFilename());
		System.out.println(imgFile.getContentType());
		System.out.println(imgFile.getName());
		System.out.println(imgFile.getSize());
		System.out.println();
		
		Path filePath=Paths.get(fileRealPath+"/"+imgFile.getOriginalFilename());
		
		try {
			Files.write(filePath, imgFile.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "ok";
	}
}

 

-WebConfig

package com.cos.springboot.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

@Configuration
public class WebConfig implements WebMvcConfigurer {

	@Value("${file.path}")
	private String fileRealPath;

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		WebMvcConfigurer.super.addResourceHandlers(registry);
		// ํŒŒ์ผ ๊ฒฝ๋กœ ์ธ์‹ํ•˜๊ฒŒ ํ•˜๊ธฐ
		registry.addResourceHandler("/media/**").addResourceLocations("file:///" + fileRealPath)
				.setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver());

	}
}

 

 

 

์ค‘๋ณต๊ฐ’์„ ์œ„ํ•ด uuid๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ ์ผํ•œ ๊ฐ’์„ ๋งŒ๋“ฌ

package com.cos.springboot.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class imagesController {
	
	@Value("${file.path}") // ์ฃผ์ž…
	private String fileRealPath;
	
	@PutMapping("/image/upload")
	public @ResponseBody String imageUpload(@RequestParam("imgFile") MultipartFile imgFile) {
		// 1. imgfile ์ถœ๋ ฅ
		UUID uuid = UUID.randomUUID();
		String uuidFilename = uuid+"_"+imgFile.getOriginalFilename();
		Path filePath=Paths.get(fileRealPath+uuidFilename);
		
		try {
			Files.write(filePath, imgFile.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "ok";
	}
}

 

* ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์ฝ”๋“œ

@PostMapping("/test/image/uploadProc")
	public String testImageUploadProc(@AuthenticationPrincipal MyUserDetail userDetail,
			@RequestParam("file") MultipartFile file, @RequestParam("caption") String caption,
			@RequestParam("location") String location, @RequestParam("tags") String tags) throws IOException{
	
		// ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์ˆ˜ํ–‰
		UUID uuid = UUID.randomUUID();
		String uuidFilename = uuid + "_" + file.getOriginalFilename();
	
		Path filePath = Paths.get(fileRealPath + uuidFilename);
		if (!Files.exists(filePath)) {
			Files.createFile(filePath);
		}
	
		AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(filePath, StandardOpenOption.WRITE);
	
		ByteBuffer buffer = ByteBuffer.allocate((int) file.getSize());
	
		buffer.put(file.getBytes());
		buffer.flip();
	
		Future<Integer> operation = fileChannel.write(buffer, 0);
		buffer.clear();
	
		while (!operation.isDone());
	
		System.out.println("Write done");
	
		return "redirect:/";
	}

 

[์ถœ์ฒ˜] ์Šคํ”„๋ง ํŒŒ์ผ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ|์ž‘์„ฑ์ž getinthere

 

 

 

-yml ์„ค์ •ํŒŒ์ผ

์• ์ดˆ์— 1mb max size๋ฅผ ๊ฑธ์–ด ๋†“๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Œ.

  servlet:
    multipart:
      enabled: true
      max-file-size: 1MB