layui上传图片、回显、预览

jsp页面:

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




上传图片demo









     
                                  
             

    
  

java后台:

 //图片上传控制器
     @RequestMapping(value = "/uploadFile" , method = RequestMethod.POST) 
     @ResponseBody
     public Map uploadPicture(@RequestParam("file")MultipartFile file,HttpServletRequest servletRequest) 
                     throws IOException { 
             
             Map res = new HashMap();
             
			 
             //上传文件路径 
             String path = servletRequest.getServletContext().getRealPath("/upload");
             System.out.println("文件名称"+file.getOriginalFilename()); 
             //上传文件名         
             String name = file.getOriginalFilename();//上传文件的真实名称
             
             String suffixName = name.substring(name.lastIndexOf("."));//获取后缀名
            // String hash = Integer.toHexString(new Random().nextInt());//自定义随机数(字母+数字)作为文件名
             String hash = UUID.randomUUID().toString().replaceAll("-","");
             String fileName = hash + suffixName;        
             File filepath = new File(path, fileName); 
             System.out.println("随机数文件名称"+filepath.getName()); 
             //判断路径是否存在,没有就创建一个 
             if (!filepath.getParentFile().exists()) { 
                 filepath.getParentFile().mkdirs(); 
                 } 
             //将上传文件保存到一个目标文档中 
             File tempFile = new File(path + File.separator + fileName);
             file.transferTo(tempFile);
             
            // resUrl.put("src", tempFile.getPath());
             res.put("code", "0");
             res.put("msg", "");
             res.put("data", tempFile.getName());
             return res;
         } 

 

你可能感兴趣的:(web前端,layui图片上传)