spring.mvc.static-path-pattern=/images/**
spring.web.resources.static-locations=classpath:/css
@PostMapping("upload")
public String up(String nickname, MultipartFile photo, HttpServletRequest request) throws IOException {
System.out.println(nickname);
// 獲取圖片的原始名稱
System.out.println(photo.getOriginalFilename());
// 獲取文件類型
System.out.println(photo.getContentType());
System.out.println(System.getProperty("user.dir"));
// 獲取web服務器運行的目錄
String path = request.getServletContext().getRealPath("/upload/");
System.out.println(path);
saveFile(photo,path);
return "上傳成功!";
}
// 保存文件到web服務器
public void saveFile(MultipartFile photo,String path) throws IOException{
// 判斷存儲的目錄是否存在,不存在則創(chuàng)建
File dir = new File(path);
if(!dir.exists()){
// 創(chuàng)建目錄
dir.mkdir();
}
File file = new File(path+photo.getOriginalFilename());
photo.transferTo(file);
}
瀏覽器訪問剛上傳到web服務器的文件
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號