java springboot -- MultipartFile -圖片上傳到遠程服務器上


/***
* 新增文件
* @param file
* @return
*/
private boolean saveFile(MultipartFile file) {

try {
// 文件保存路徑
String filePath = "A://merchant//"; //映射的地址
//String filePath = request.getSession().getServletContext().getRealPath("upload/");本地項目路徑

String filename = file.getOriginalFilename();//獲取file圖片名稱

uploadFile(file.getBytes(), filePath, filename);


PictureRes picRes = new PictureRes(); //數據庫中只存圖片表
picRes.setIsType(isType);//圖片類型
picRes.setPicPath("http://192.168.1.....:8080/upload/merchant/"+filename);//遠程tomcat下的路徑+圖片名;保存在數據庫中,
picDao.save(picRes);
return true;

} catch (Exception e) {
e.printStackTrace();
}
return false;
}

public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file);
out.flush();
out.close();
}

以下圖片是本地進行映射網絡;在此之前需要在tomcat服務器上面進行共享

 tomcat服務器上面進行共享文件

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM