Skip to content

Commit

Permalink
스프링과 파일 업로드
Browse files Browse the repository at this point in the history
  • Loading branch information
kiteB committed Oct 6, 2021
1 parent d4fc496 commit ecf16e1
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package hello.upload.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Slf4j
@Controller
@RequestMapping("/spring")
public class SpringUploadController {

@Value("${file.dir}")
private String fileDir;

@GetMapping("/upload")
public String newFile() {
return "upload-form";
}

@PostMapping("/upload")
public String saveFile(@RequestParam String itemName,
@RequestParam MultipartFile file, HttpServletRequest request) throws IOException {

log.info("request={}", request);
log.info("itemName={}", itemName);
log.info("multipartFile={}", file);

if (!file.isEmpty()) {
String fullPath = fileDir + file.getOriginalFilename();
log.info("파일 저장 fullPath={}", fullPath);
file.transferTo(new File(fullPath));
}

return "upload-form";
}
}

0 comments on commit ecf16e1

Please sign in to comment.