第一种方法
public int insertimage(MultipartFile file,String name, String position,String introduction,String navigationOne,HttpServletRequest request) throws IOException {
String path1 = request.getSession().getServletContext().getRealPath("/");//获取服务器在本机中的绝对路径
String path3 = path1.substring(0, path1.toString().indexOf("target"));//截取target文件夹之前
String filename = file.getOriginalFilename(); //获取上传文件名
String filename1= filename.substring(filename.lastIndexOf("."));//判断格式
filename= UUIDUtil.getOneUUID();//给图片重新命名
String path2 = path3 + "src/main/webapp/imgages/"; //创建imgs文件夹
String path = path2 + filename+filename1;
//第一次如果没有保存图片的文件夹创建文件夹
File dest = new File(path);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
file.transferTo(dest);
Date date = new Date();
//添加到图片表
Picture picture=new Picture();
picture.setType3(name);
picture.setPicUrl(filename+filename1);
picture.setState("1");
picture.setCreateTime(date);
int code=pictureMapper.insertSelective(picture);
//添加到人员表
Person person = new Person();
person.setName(name);
person.setCreateTime(date);
person.setState("1");
person.setNavigationOne(navigationOne);
person.setIntroduction(introduction);
person.setPosition(position);
int result = personMapper.insertSelective(person);
if (code == 1 && result == 1){
return 1;
}
else {
return 0;
}
第二种方法
public HttpResponseEntity insertDoctor(HashMap<String,Object> hashMap) throws Exception{
MultipartFile file = (MultipartFile) hashMap.get("file");
String name = hashMap.get("name").toString();
String position = hashMap.get("position").toString();
String introduction = hashMap.get("introduction").toString();
String navigationOne = hashMap.get("navigationOne").toString();
HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
if (file.isEmpty()){
httpResponseEntity.setCode(Constans.FILE_NULL_ERROR_CODE);
httpResponseEntity.setMessage(Constans.FILE_NULL_ERROR_MESSAGE);
}
//获取文件名
String fileName = file.getOriginalFilename();
//获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
fileName = UUIDUtil.getOneUUID();
//文件上传后的路径
String filePath = "britainBeautySalon/images/";
String dbPath = filePath+fileName+suffixName;
//
String baseURL = "E:/meirongyuan/apache-tomcat-7.0.79/webapps/";
String path = baseURL+filePath+fileName+suffixName;
Date date = new Date();
Picture picture = new Picture();
picture.setType3(name);
picture.setState("1");
picture.setCreateTime(date);
picture.setPicUrl(dbPath);
Person person = new Person();
person.setName(name);
person.setCreateTime(date);
person.setState("1");
person.setIntroduction(introduction);
person.setPosition(position);
person.setNavigationOne(navigationOne);
int number = pictureMapper.insertSelective(picture);
int num = personMapper.insertSelective(person);
if (number == 1 && num == 1){
httpResponseEntity.setCode(Constans.SUCCESS_CODE);
}
File dest = new File(path);
if (!dest.getParentFile().exists()){
dest.getParentFile().mkdirs();
}
file.transferTo(dest);
return httpResponseEntity;
}