Skip to content

Instantly share code, notes, and snippets.

@kyungjoongo
Created July 15, 2022 11:53
Show Gist options
  • Save kyungjoongo/a1cc96f1ef01a8c229a7dce9ed99f6e9 to your computer and use it in GitHub Desktop.
Save kyungjoongo/a1cc96f1ef01a8c229a7dce9ed99f6e9 to your computer and use it in GitHub Desktop.
package com.kyungjoon777.test.com;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@Controller
public class UploadController {
@GetMapping("/index")
public String test(Model model) {
model.addAttribute("kyungjoon", "kyungjoon is genius");
return "index";
}
@PostMapping("/upload")
@ResponseStatus(HttpStatus.CREATED)
public ModelAndView upload(
@RequestPart List<MultipartFile> reg_file,
@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "nickname", required = true) String nickname
)
throws Exception {
List<String> list = new ArrayList<>();
int index = 0;
for (MultipartFile file : reg_file) {
String originalfileName = file.getOriginalFilename();
String newFileName = "reg_file_" + index + "." + originalfileName.split("\\.")[1];
File dest = new File("/Users/gilzako/java_pjt/uploaddemo/images/" + newFileName);
file.transferTo(dest);
index++;
}
ModelAndView model = new ModelAndView();
model.setViewName("jsonView");
model.addObject("result", "upload complete");
return model;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment