• SpringMVC之添加照片并修改照片名字


    @RequestMapping(value="/addIdcardsSubmit",method={RequestMethod.POST,RequestMethod.GET})
        public String addIdcardsSubmit(HttpServletRequest request,Idcard idcard,MultipartFile idcard_pic) throws IllegalStateException, IOException{
            String originalFileName = idcard_pic.getOriginalFilename();
            System.out.println("originalFileName"+originalFileName);
            String idcardCode = idcard.getIdcardcode();
            
            
            //改名字
            String pic_path = "F:\javaEEWorkspace\DemoJimSpringMVCMyBatis\src\main\webapp\pic\";//字符串需要两个斜杠
            
            String NewFileName = UUID.randomUUID()+originalFileName.substring(originalFileName.lastIndexOf("."));//修改照片名字
            
            //新的图片
            File newfile = new File(pic_path + NewFileName);
            
            //将内存中的数据写入磁盘
            idcard_pic.transferTo(newfile);
            
            idcard.setPic(NewFileName);
            System.out.println("idcardCode"+idcardCode);
            int num = idcardService.addIdCard(idcard);
            if(num == 1) return "/success";
            else return "/fail";
        }
    下面是前端代码:

      

    //index.jsp
    <%@ page language="java" contentType="text/html; charset=utf-8" isELIgnored="false" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> ${pageContext.request.contextPath} <a href="${pageContext.request.contextPath}/addIdcard.action">增加用户信息</a> <form action="${pageContext.request.contextPath}/findIdcardByIdcardcode.action" name="myform" method="post"> 查询条件: <table width="60%" border=1> <tr> <td width="50%"><input type="text" name="idcardcode" /></td> <td><input type="submit" value="查询"/></td> </tr> </table> </form> <form action="${pageContext.request.contextPath}/deleteIdcardAll.action" name="myform1" method="post"> 身份证信息列表: <table width="60%" border=1> <tr> <td colspan="3"><input type="submit" value="批量删除" /></td> </tr> <tr> <td>身份证</td> <td>身份证图片</td> <td>操作</td> </tr> <c:forEach items="${idcardsAll}" var="idcard" varStatus="status"><!-- 遍历idcardList集合,然后一个一个给idcard,idcard就 相当于是Idcard--> <tr height="100"> <td width="200"> <input type="checkbox" name="ids" value="${idcard.id }"/> <input type="hidden" name="idcardList[${status.index }].id" value="${idcard.id }"/> ${idcard.idcardcode} </td> <td width="200"> <c:if test="${idcard.pic !=null}"> <img src="./pic/${idcard.pic}" alt="" width="100" height="100"/> <br/> </c:if> </td> <td> <a href="${pageContext.request.contextPath }/findIdcardById.action?id=${idcard.id}">查询</a> <a href="${pageContext.request.contextPath }/editItems.action?id=${idcard.id}">修改</a> <a href="${pageContext.request.contextPath }/deleteIdcardById.action?id=${idcard.id}">删除</a> </td> </tr> </c:forEach> </table> </form> </body> </html>

      

    //findIdcard.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ${idcard.idcardcode} <form id="idcardForm" action="${pageContext.request.contextPath }/findIdcardsAll.action" method="post"> 身份证信息: <table width="100%" border=1> <tr> <td>身份证信息</td> <td><input type="text" name="idcardcode" disabled="disabled" value="${idcard.idcardcode}"/></td> </tr> <tr> <td>身份证图片</td> <td> <c:if test="${idcard.pic !=null}"> <img src="./pic/${idcard.pic}" width=100 height=100/> <br/> </c:if> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="返回"/> </td> </tr> </table> </form> </body> </html>

      

    //addIdcard.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form id="idcardForm" action="${pageContext.request.contextPath }/addIdcardsSubmit.action" method="post" enctype="multipart/form-data"> 身份证信息: <table width="100%" border=1> <tr> <td>身份证信息</td> <td><input type="text" name="idcardcode"/></td> </tr> <tr> <td>身份证图片</td> <td> <input type="file" name="idcard_pic" > <br/> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="添加"/> </td> </tr> </table> </form> </body> </html>

      

  • 相关阅读:
    windows 核心编程 第2章 U n i c o d e
    在一个类内不可以定义自身类的对象,为什么定义自身类的静态对象又是正确的
    将JPG图片绘制在对话框背景上:(这段代码绝对可以执行)
    小技巧给CEdit设置字体
    惠普 升级两年保修
    DoModal返回1,对话框不能显示,今天碰到项目在用unicode编码,和多字符编码时候出现的
    MFC 绘图
    CString类所有成员函数详解
    mysql 一些问题
    错误:不能实例化抽象类
  • 原文地址:https://www.cnblogs.com/luyuan-chen/p/11397808.html
Copyright © 2020-2023  润新知