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