๋๋ ์๋ฌด ์๊ฐ์ด ์๋ค
๊ทธ๋ฅ ์๋ฌด ์๊ฐ ํ๊ธฐ ์ซ๊ธฐ ๋๋ฌธ์ด๋ค
1. ์ ํจ์ฑ๊ฒ์ฌ
@Valid
๊ฒ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์๋ ๊ฐ์ด ๋์ด๊ฑฐ๋ ๊ณต๋ฐฑ์ด๊ฑฐ๋ ๋ฑ๋ฑ์ ๊ฒ์ฌํ์ฌ ์์ธ์ฒ๋ฆฌํด์ฃผ์์ง๋ง ์คํ๋ง์์๋ ๋ญ๊ฐ ์๋ค์ด์๋์ง ์๋์ผ๋ก ์๋ ค์ฃผ๊ธฐ๋ ํ๋ค.
* ์๋๋ก์ด๋๊ฐ๋ฐ์ http๋ฅผ ๋ฆฌํด ๋ฐ์ ํ์๊ฐ ์๊ณ ๋ฐ์ดํฐ๋ง ๋ฆฌํด ๋ฐ์ผ๋ฉด ๋๋ค.
* ์ ๋ฆฌ ์ฐธ ์ํด๋์๋ค ์ฐธ๊ณ ํด์ผ๊ฒ ๋ค
https://victorydntmd.tistory.com/179
* ์๋๋ก์ด๋๊ฐ๋ฐ์ http๋ฅผ ๋ฆฌํด ๋ฐ์ ํ์๊ฐ ์๊ณ ๋ฐ์ดํฐ๋ง ๋ฆฌํด ๋ฐ์ผ๋ฉด ๋๋ค.
* ์ค๋ฅ๋ฉ์์ง ์ฐธ๊ณ ์ฌ์ดํธ
@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
'java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ๋ง - ์ํ ์กฐํ ๊ฒ์ํ ๋ง๋ค๊ธฐ(2) (0) | 2021.02.27 |
---|---|
์คํ๋ง - ์ํ ์กฐํ ๊ฒ์ํ ๋ง๋ค๊ธฐ(1) (0) | 2021.02.27 |
์คํ๋ง mysql ์ฐ๊ฒฐํ๊ธฐ (0) | 2021.02.27 |
JSP๋ฅผ ์ด์ฉํ ์ ํ ์๊ฐ ํํ์ด์ง ๋ง๋ค๊ธฐ (0) | 2021.02.27 |
์คํ๋ง ์์ํ๊ธฐ -2 (0) | 2021.02.27 |