发布网友 发布时间:2022-04-22 00:48
共1个回答
热心网友 时间:2023-07-05 04:20
//保存图片
private void saveImg(HttpServletRequest request,FormFile imgFile,FileForm fileForm){
if (imgFile != null && imgFile.getFileSize() > 0) {
String fileName = imgFile.getFileName();
String sqlPath = "img/" + fileName;
//图片所在路径
String savePath = request.getSession().getServletContext().getRealPath("/")+ "img\\" + fileName;
System.out.println(fileName);
System.out.println(sqlPath);
System.out.println(savePath);
HttpSession session=request.getSession();
session.setAttribute("savePath", savePath);
session.setMaxInactiveInterval(60*60);
//String savePath1=(String)session.getAttribute("savePath");
// 数据库
fileForm.getFile().setFileEmpPhoto(sqlPath);
// 文件
try {
InputStream input = imgFile.getInputStream();
FileOutputStream output = new FileOutputStream(savePath);
byte[] b = new byte[1024];
while (input.read(b) != -1) {
output.write(b);
b = new byte[1024];
}
output.close();
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}