自己引入jar包实现的图片上传

Posted 根目录97

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自己引入jar包实现的图片上传相关的知识,希望对你有一定的参考价值。

这个图片上传主要实现用户实名认证,截取字符串有点繁琐,有任何建议,欢迎评论提出。

1.首先引入一个jar包

org.springframework.web.multipart;

2.开始上传图片,以文件流接收

package com.qianmo.qmyj.controller;

import com.qianmo.qmyj.bean.dto.UserInfo;
import com.qianmo.qmyj.bean.request.FindMsgRequest;
import com.qianmo.qmyj.bean.request.FindOneMessageRequest;
import com.qianmo.qmyj.bean.response.FindMsgResponse;
import com.qianmo.qmyj.bean.response.FindOneMessageResponse;
import com.qianmo.qmyj.common.Constants;
import com.qianmo.qmyj.framework.util.DataVerifyUtil;
import com.qianmo.qmyj.service.UserInfoExecuteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 图片上传
 * */
@Controller
@RequestMapping(value = "/UploadImageController")
public class UploadImageController extends BaseController {

    Logger logger = LoggerFactory.getLogger(UploadImageController.class);
//    @Resource
//    MsgService msgService;

    @Resource
    private UserInfoExecuteService userInfoExecuteService;

    /*
   *    上传图片;
   * */
    @ResponseBody
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Map upload(@RequestParam(value = "imgPath") MultipartFile multipartFile,String userId,String imgType, HttpServletRequest request){
        logger.info("——————进入上传图片接口");
        Map map=new HashMap();      //IdCard是项目中的文件目录,会自动存到本地项目里
        String upload=request.getRealPath("/IdCard/"+ multipartFile.getOriginalFilename());
        logger.info("——————进入上传图片接口,upload:"+upload);

        //截取图片名称前面的字符串
        String imgIdCardPath = upload.substring(upload.indexOf("D"),upload.lastIndexOf("m")-1);
        logger.info("——————截取图片名称前面的字符串,imgIdCardPath:"+imgIdCardPath);
        //截取图片名称后面的字符串
        String imgIdCardPath2 = upload.substring(upload.indexOf("C")+5,upload.length());
        logger.info("——————截取图片名称后面的字符串,imgIdCardPath2:"+imgIdCardPath2);
        Date d=new Date();
        long timeStr=d.getTime();
        if (imgType.equals("0")){
            upload = imgIdCardPath +"face"+ String.valueOf(timeStr)+imgIdCardPath2;
        }else if(imgType.equals("1")){
            upload = imgIdCardPath +"crtA"+ String.valueOf(timeStr)+imgIdCardPath2;
        }else if(imgType.equals("2")){
            upload = imgIdCardPath +"crtB"+ String.valueOf(timeStr)+imgIdCardPath2;
        }

        logger.info("——————upload,upload:"+upload);
        //截取图片存入数据库
        String imgPath=upload.replaceAll("\\\\","/");
        imgPath = imgPath+"0";
        imgPath = imgPath.substring(imgPath.indexOf("I"), imgPath.lastIndexOf("0"));
        logger.info("——————imgPath,imgPath:"+imgPath);

        //   System.out.println(str.substring(str.indexOf(">")+1, str.lastIndexOf("<"))); 截取指定两个字符之间的字符

        UserInfo cust=userInfoExecuteService.findOneUserStr(userId);
        logger.info("——————cust:"+cust);
        if (imgType.equals("0")){//0代表头像

            File folder = new File(imgIdCardPath);
            File[] files = folder.listFiles();//如果图片存在,则删除
            String str1=cust.getUserFaceImg().substring(cust.getUserFaceImg().indexOf("/")+ Constants.score.one, cust.getUserFaceImg().length());
            if (null !=files) {
                for (File file : files) {
                    if (file.getName().equals(str1)) {
                        file.delete();
                        break;
                    }
                }
            }
            /*if (!"".equals(cust.getUserFaceImg())||null!=cust.getUserFaceImg()){//如果图片存在,则删除
                logger.info("——————如果图片存在,则删除:删除!");
                clearFiles(upload);
            }*/
            //获取绝对相对路径
            //文件上传到指定路径;
            try {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(userId);
                userInfo.setUserFaceImg(imgPath);
                int count = userInfoExecuteService.updateUserImage(userInfo);//修改头像
                logger.info("——————修改头像,count:"+count);
                UserInfo customer=userInfoExecuteService.findOneUserStr(userId);//获取最新工人对象
                logger.info("——————获取最新工人对象,customer:"+customer);
                multipartFile.transferTo(new File(upload));
                logger.info("——————上传头像成功,multipartFile:"+upload);
                //使用给map添加数据并返回;
                map.put("code","0000");
                map.put("imagePath",imgPath);
                map.put("customer",customer);
                map.put("message","上传成功!");
                return map;
            }catch (Exception e){
                map.put("code","1111");
                map.put("message","头像");
                map.put("message","上传失败!");
                return map;
            }
        }else if (imgType.equals("1")){//1代表身份证正面照
            File folder = new File(imgIdCardPath);
            File[] files = folder.listFiles();//如果图片存在,则删除
            String str1=cust.getIdCardPositiveImg().substring(cust.getIdCardPositiveImg().indexOf("/")+ Constants.score.one, cust.getIdCardPositiveImg().length());
            if (null !=files) {
                for (File file : files) {
                    if (file.getName().equals(str1)) {
                        file.delete();
                        break;
                    }
                }
            }
            /*if (!"".equals(cust.getIdCardPositiveImg())||null!=cust.getIdCardPositiveImg()){//如果图片存在,则删除
                clearFiles(upload);
            }*/
            //获取绝对相对路径
            //文件上传到指定路径;
            try {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(userId);
                userInfo.setIdCardPositiveImg(imgPath);
                int count = userInfoExecuteService.updateUserImage(userInfo);//修改头像
                multipartFile.transferTo(new File(upload));
                //使用给map添加数据并返回;
                map.put("code","0000");
                map.put("imagePath",imgPath);
                map.put("message","上传成功!");
                return map;
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code","1111");
                map.put("CardPicture2","身份证正面");
                map.put("message","上传失败!");
                return map;
            }
        }else if (imgType.equals("2")){//2代表身份证反面照
            File folder = new File(imgIdCardPath);
            File[] files = folder.listFiles();//如果图片存在,则删除
            String str1=cust.getIdCardNegativeImg().substring(cust.getIdCardNegativeImg().indexOf("/")+ Constants.score.one, cust.getIdCardNegativeImg().length());
            if (null !=files) {
                for (File file : files) {
                    if (file.getName().equals(str1)) {
                        file.delete();
                        break;
                    }
                }
            }
            /*if (!"".equals(cust.getIdCardNegativeImg())||null!=cust.getIdCardNegativeImg()){//如果图片存在,则删除
                clearFiles(upload);
            }*/
            //获取绝对相对路径
            //文件上传到指定路径;
            try {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(userId);
                userInfo.setIdCardNegativeImg(imgPath);
                int count = userInfoExecuteService.updateUserImage(userInfo);//修改头像
                multipartFile.transferTo(new File(upload));
                //使用给map添加数据并返回;
                map.put("code","0000");
                map.put("imagePath",imgPath);
                map.put("message","上传成功!");
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code","1111");
                map.put("CardPicture2","身份证反面");
                map.put("message","上传失败!");
                return map;
            }
        }else{
            map.clear();
            map.put("code","1111");
            map.put("message","图片上传失败,类型请选择0(头像),1(身份证正面照),2(身份证反面照)");
            return map;
        }
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/findMsg",method = RequestMethod.POST)
    public FindMsgResponse findMsg(FindMsgRequest request){
        return userInfoExecuteService.findMsgList(request);
    }

    /***
     * 查询单个消息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findJupsh",method = RequestMethod.POST)
    public FindOneMessageResponse findOneMsg(FindOneMessageRequest request){
        return userInfoExecuteService.findOneMsg(request);
    }

    /***
     * 查询所有消息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findAllJupsh",method = RequestMethod.POST)
    public FindMsgResponse findOneMsg(FindMsgRequest request){
        return userInfoExecuteService.findMsgList(request);
    }
}

 

以上是关于自己引入jar包实现的图片上传的主要内容,如果未能解决你的问题,请参考以下文章

安卓项目如何优先使用自己引入的第三方jar包

在jmeter中引入自己的jar包

maven库中有jar包,但是引入不到,可以直接把包下载下来

FastDFS简单入门小demo

android中如何上传图片到FTP服务器

maven deploy 400失败,二次上传maven source.jar包