/***
* 新增文件
* @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服務器上面進行共享文件