자바설정파일!

→ 자바 설정 가능하게 열기(그냥 이게 편하고, 추세니까!)

MyConfig.java

package com.jeungsu.test.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class MyConfig implements WebMvcConfigurer{

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		System.out.println("요기가 실행되었는지 check?");
		registry.addResourceHandler("/upload/**")             // 웹 접근 경로 
		        .addResourceLocations("file:///d:/upload/");  // 서버내 실제 경로
	}

}

→ 테스트

d:/upload 에 파일1개 넣어 놓고

브라우져에서 http://서버명:포트/웹컨텍스트/upload/파일명으로 테스트해본다!

설정 파일을 부트가 읽었다는것을 확인!

Untitled

Untitled

설정이 실제로 잘 이루어진 것을 확인할 수 있다.

Untitled

Controller

package com.jeungsu.test.contoller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jeungsu.test.vo.TestVO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/")
public class JeongsuController {
	
	@GetMapping("/") // 여기에 @RequestMapping 쓰지 않아요!
	public String home() {
		return "home";
	}
	
@PostMapping(value="/mfile", produces = "application/json;charset=utf-8")
	@ResponseBody
	// formData를 받을때는 @RequestBody를 사용하지 않음
	public String restFile(MultipartFile myFile) throws Exception{
		log.info(myFile.getOriginalFilename());		
		log.info(""+myFile.getSize());		
		
		String destPath = "d:/upload/" + myFile.getOriginalFilename();
		
		myFile.transferTo(new File(destPath));
		return "/upload/" + myFile.getOriginalFilename();
	}
	
	
}