import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
@Controller
public class FileUpdate {
@RequestMapping("updateFile")
public String updateFile(@RequestParam("file") MultipartFile[] files, HttpSession session) throws IOException {
System.out.println("开始文件上传");
//定义上传的位置
// 获取当前项目路径:
// session.getServletContext().getRealPath
String path = session.getServletContext().getRealPath("/uploads");
System.out.println(path);
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
for (MultipartFile multipartFile : files) {
String fileName = multipartFile.getOriginalFilename();
System.out.println("文件名称:" + fileName);
multipartFile.transferTo(new File(file, fileName));
}
return "home";
}
}