Skip to content

Instantly share code, notes, and snippets.

@kyungjoongo
Created July 13, 2022 20:33
Show Gist options
  • Save kyungjoongo/eb006dfdbd95c88e449c87d0b3e1e8d8 to your computer and use it in GitHub Desktop.
Save kyungjoongo/eb006dfdbd95c88e449c87d0b3e1e8d8 to your computer and use it in GitHub Desktop.
upload_spring_boot
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 java.io.File;
import java.util.ArrayList;
import java.util.List;
@Controller
public class TestController {
@GetMapping("/index")
public String test(Model model) {
model.addAttribute("kyungjoon", "kyungjoon is genius");
return "index";
}
@PostMapping("/upload")
@ResponseStatus(HttpStatus.CREATED)
public Object upload(@RequestPart List<MultipartFile> reg_file, @RequestPart List<MultipartFile> ext_file, Model model) 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++;
}
index = 0;
for (MultipartFile file : ext_file) {
String originalfileName = file.getOriginalFilename();
//String[] splitedList = originalfileName.split("\\.");
String newFileName = "ext_file_" + index + "."+ originalfileName.split("\\.")[1];
File dest = new File("/Users/gilzako/java_pjt/uploaddemo/images/" + newFileName);
file.transferTo(dest);
index++;
// TODO
}
model.addAttribute("kyungjoon", "kyungjoon is genius");
return model;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment