Springboot加layui

eclipse

layui

方式/步骤

  1. 1

    首先html引入layui模板,js及css,如图

    9213b07eca806538c88d07d299dda144ac348288.jpg
  2. 2

    搭建Springboot+thymeleaf情况,成立文件夹static/upload,作为文件上传后的路径

    5bafa40f4bfbfbede4e895be76f0f736aec31fb9.jpg
  3. 3

    参考如图所示html,class引入layui-upload

    代码:

    *图片

    选择文件

    377adab44aed2e73e0b124ed8901a18b86d6fa93.jpg
  4. 4

    单文件书写js代码(主动上传auto:true,不主动上传auto:false):

    //选完文件后主动上传

      upload.render({

    elem: '#showimage'

    ,url: '/a_product/upload'

    ,auto: true

    ,field:"file1"

      });

    bba1cd11728b47109ba55f0dcdcec3fdfd03236c.jpg
  5. 5

    单文件后端java代码:

    @ResponseBody

    @RequestMapping("upload")

    public Map upload(MultipartFile file1) throws FileNotFoundException {

    UploadUtil.singFile(file1,GetTimestamp.getRandomTime(),"static/upload/");

    Map map=new HashMap<>();

    map.put("code", 0);

    return map;

    }

    503d269759ee3d6ddb112a0a4d166d224e4ade18.jpg
  6. 6

    UploadUtil文件上传东西类代码:

    public class UploadUtil {

    public static String singFile(MultipartFile file,String fileName,String folder) throws FileNotFoundException {

    //利用ResourceUtils  来获取真实路径  确保摆设时不会犯错

    File path = new File(ResourceUtils.getURL("classpath:").getPath());

    System.out.println(path.getAbsolutePath());

    //若是上传的文件为/static/upload/  如下

    File upload = new File(path.getAbsolutePath(), folder);

    if (!upload.exists())

    upload.mkdirs();

    String uploadPath = upload + "\\";

    System.out.println("图片上传后的路径:"+uploadPath);

    try {

    File fileUpload=new File(uploadPath + fileName + ".jpg");

    file.transferTo(fileUpload);

    return "/"+folder+fileName+".jpg";

    } catch (IllegalStateException | IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    return "error";

    }

    }

    63d9f2d3572c11df287f13e26d2762d0f603c241.jpg
  7. 7

    具体操作演示界面如下:

    8cb1cb134954092324f5ccbf9c58d109b2de4957.jpg
  8. 8

    节制台找到文件上传后的路径

    43a7d933c895d143ef234ae27df082025baf079d.jpg
  9. 9

    按照上传之后的目次查看,上传之后结果图如下

    2e2eb9389b504fc209f71ec4ebdde71191ef6d43.jpg
  • 发表于 2019-08-22 17:00
  • 阅读 ( 300 )
  • 分类:其他类型

相关问题

0 条评论

请先 登录 后评论