美文网首页
springboot结合layUI实现文件上传

springboot结合layUI实现文件上传

作者: 轰轰_9ccd | 来源:发表于2020-11-13 08:56 被阅读0次

    package com.netvox.lora.controller;

    import org.springframework.core.io.Resource;

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.*;

    import org.springframework.web.multipart.MultipartFile;

    import javax.servlet.http.HttpServletRequest;

    import java.io.File;

    import java.io.IOException;

    import java.io.InputStream;

    import java.util.HashMap;

    import java.util.Map;

    /**

    * @Package: com.netvox.lora.controller

    * @ClassName: UploadController

    * @Author: ZhanWeiFeng

    * @Description: //TODO

    * @Date: 2020.11.12 9:23

    * @Version: 1.0

    */

    @Controller

    public class UploadController {

    @RequestMapping("upload")

    public StringuploadIndex() {

    return "upload";

        }

    @RequestMapping(value ="/uploadSource", method = RequestMethod.POST)

    @ResponseBody

        public MapuploadSource(@RequestParam("file") MultipartFile file, @RequestParam("password") String password) {

    Map result =new HashMap<>();

            if (!password.equals("netvox5717188")){

    result.put("code",-1);

                    result.put("msg","口令错误");

                return result;

            }

    System.out.println(file);

            String pathString =null;

            try {

    File directory =new File(Resource.class.getClassLoader().getResource("device.json").getPath());

                pathString = directory.getCanonicalPath();

                File files =new File(pathString);

                //打印查看上传路径

                System.out.println(pathString);

                if (files.exists()){

    files.delete();

                }

    file.transferTo(files);

                result.put("code", 200);

                result.put("msg", "success");

            }catch (Exception e) {

    result.put("code", -2);

                e.printStackTrace();

            }

    return result;

        }

    }

    前端代码


    <!DOCTYPE html>

    <html lang="en">

        <meta charset="utf-8">

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

        <title>文件上传

        <link rel="stylesheet" href="/layui/css/layui.css">

        <h1 align="center" style="font-size:30px">上传Lora设备描述文件

    <script src="/layui/layui.all_zh.js">

    <script src="/layui/jquery/jquery-3.3.1.min.js">

    <form class="layui-form" action="">

        <div class="layui-form-item">

            <div class="layui-col-md12" style="margin-left:600px">

                <label class="layui-form-label">  口令:

            <div class="layui-input-inline">

              <input type="text" name="password"  id="password"  lay-reqtext="口令:" placeholder="请输入"

                          autocomplete="off" class="layui-input-inline  lay-filter=password" >

            <div class="layui-col-md12" style="margin-left:800px">

                <button type="button" class="layui-btn" id="upload"><i class="layui-icon"></i>上传文件

            <div class="layui-col-md12" style="margin-left:800px;margin-top:20px">

                <button id="button" type="button"  class="layui-btn">提交

      var form =layui.form;

      var password= $("#password").val();

        // layui.use('upload', function () {

            var upload =layui.upload;

            //执行实例

            var uploadInst = upload.render({

    elem:'#upload' //绑定元素

                , url:'http://localhost:8080/uploadSource' //上传接口.

                ,method:'POST'

                ,accept:'file'

                ,bindAction:'#button'

                ,auto:false

                ,data: {

    password:$("#password").val()

    },

                before:function (obj) {

    this.data.password = $("#password").val();

                }

    , done:function (res) {

    //上传完毕回调

                    if (res.code==200){

    alert("上传成功")

    }else if(res.code==-1){

    alert("口令错误请重新输入")

    }else {

    alert("文件上传错误请重新上传")

    }

    console.log("done")

    }

    , error:function (res) {

    alert("上传失败")

    //请求异常回调

                    console.log("error")

    }

    });

        // });

    </html>

    相关文章

      网友评论

          本文标题:springboot结合layUI实现文件上传

          本文链接:https://www.haomeiwen.com/subject/ryfvbktx.html