package com.example.demo.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@RestController
public class FileController {
@PostMapping("/upload")
public String upload(String name, MultipartFile photo, HttpServletRequest request) throws IOException {
System.out.println(name);
System.out.println(photo.getName());
System.out.println(photo.getContentType());
System.out.println(photo.getOriginalFilename());
String servletPath = request.getServletPath();
System.out.println(servletPath);
String realPath = request.getServletContext().getRealPath("/upload");
saveFile(photo,realPath);
return "上传了"+name;
}
public void saveFile(MultipartFile photo,String path) throws IOException {
File dir = new File(path);
if (dir.exists()){
dir.mkdir();
}
File file = new File(path + photo.getOriginalFilename());
photo.transferTo(file);
}
}
标签:文件,SpringBoot,photo,System,String,File,import,上传,out From: https://www.cnblogs.com/niunai/p/18007484