import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @Controller @RequestMapping("/contract") public class ContractController { @GetMapping("input") public String contract() { return "contract/input"; } @RequestMapping(value="upload", method=RequestMethod.POST) public void handle( HttpServletResponse response, @RequestParam MultipartFile file ) { if(file.isEmpty()) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } try { BufferedInputStream in = new BufferedInputStream(file.getInputStream()); BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(file.getOriginalFilename())); } catch (IOException e) { throw new RuntimeException("Error uploading file.", e); } } }
html
<form action="/contract/upload" method="post" enctype="multipart/form-data"> <div class="form-group"> <div class="custom-file"> <input type="file" name="file" class="custom-file-input" id="inputFile"> </div> </div> <br> <button type="submit" class="btn btn-primary" id="upload" value="upload">登録</button> </form>
BufferedInputStreamとBufferedOutputStreamの使い方がイマイチよくわからんな。。