图片上传

简介: 图片上传

第一种方法

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;

}





相关文章
|
小程序
UniApp上传图片
小程序大家应该都知道,通过上传组件得到的都是本地的一个临时路径,这个路径是不能被外网访问的,所以我们就需要将拿到的临时路径转成Base64上传到后台服务器。或者说是另外一个办法,就是通过组件直接上传文件,这个看需求设计吧。
282 0
|
8月前
|
小程序 JavaScript 数据库
微信小程序系列——上传下载图片以及图片的展示
微信小程序系列——上传下载图片以及图片的展示
|
9月前
uniapp上传图片
uniapp上传图片
96 0
|
8月前
uiapp 上传图片
uiapp 上传图片
80 0
|
8月前
|
API
uniapp图片上传
uniapp图片上传
381 0
|
存储 NoSQL 前端开发
一文搞定图片选择及图片上传
本篇介绍了在 Flutter 中如何选择图片文件,图片选择组件的封装和如何将图片上传到后台。通过本篇,可以了解Flutter 构建应用时的图片上传过程。
795 0
|
BI 数据安全/隐私保护
图片地址是BASE64的图片上传(头像上传)
1 package controller; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.
1109 0
图片上传预览
     最近做需求时遇到的,上传的时候预览一下,一开始并没有想着用插件什么的,太复杂,只是个预览效果,不如自己写省事。前前后后也就几十行代码(包含头尾HTML、注释、输出调试),反正是比引用插件少多了,自己写也是个锻炼。
788 0

热门文章

最新文章