ssm框架图片上传问题

jsp

<script>
    layui.use("upload",function(){
        var upload = layui.upload;
         var uploadInst = upload.render({
                elem: '#test1'
                ,url: '/jyals/upload/images'
                ,before: function(obj){
                  //预读本地文件示例,不支持ie8
                  obj.preview(function(index, file, result){
                    $('#demo').attr('src', result); //图片链接(base64)
                  });
                }
                ,done: function(res){
                  //上传成功
                if (res.code == 0) {
                    layer.msg("上传成功!");
                    $("#imageName").val(res.fileId);
                    $("#picId").val(res.fileName);
                    $("#picName").val("gktp");
                    $("#picAdress").val(res.filePath);
                    $("#picType").val(res.fileType);
                    console.log(res.filePath);
                }else{
                    layer.msg("上传失败!");
                }
                }
                ,error: function(){
                  //演示失败状态,并实现重传
                  var demoText = $('#demoText');
                  demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                  demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                  });
                }
              });
    });
    </script>

upload

@RequestMapping(value="/images",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject uploadImage(MultipartFile file,HttpServletRequest request,HttpSession session){
        /**
         * 仅服务器端保存文件,数据库端由表单上传具体处理保存
         */
        JSONObject json = new JSONObject();
        String orPath = request.getSession().getServletContext().getRealPath("/opt/tomcat/lbpic/");
        //对文件进行保存并且生成名称
        try{
            //成功
            json = baseServiceImpl.uploadlbFile(file, orPath,session,request);
            json.put("code", 0);            
        }catch (Exception e) {
            //失败            
            json.put("code", 1);
            e.printStackTrace();

        }   
        return json;
    }
layui
layedit.set({
          uploadImage: {
            url: '/jyals/upload/imgs' //接口url
            ,type: 'post' //默认post
            ,done : function(res, index, upload) {
                //上传完毕的回调函数
                if (res.code == 0) {
                    layer.msg("上传成功!");
                    $("#imageName").val(res.fileId);
                    console.log(res.filePath);
                } else {
                    layer.msg("上传失败!");
                    $('#tpyl').html("");
                }

            }
          }
        });
    //创建一个编辑器
    var editIndex = layedit.build('LAY_demo_editor');
@RequestMapping(value="/imgs",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject uploadImgs(MultipartFile file,HttpServletRequest request,HttpSession session){
    /**
     * 仅服务器端保存文件,数据库端由表单上传具体处理保存
     */
    JSONObject json = new JSONObject();
    JSONObject json1 = new JSONObject();
    String orPath = request.getSession().getServletContext().getRealPath("/opt/tomcat/pic/");
    //对文件进行保存并且生成名称
    try{
        //成功
        json.put("code", 0);    
        json1=baseServiceImpl.uploadFile(file, orPath,session,request);
        JSONObject data = new JSONObject();
        String filename = json1.getString("fileName");
        String name = filename.substring(0,16);
        String path = "http://47.97.201.2/jyals/uploadImages/";
        data.put("src",path+filename);
        json.put("data", data);

    }catch (Exception e) {
        //失败            
        json.put("code", 1);
        json.put("msg", "fail");
        e.printStackTrace();

    }   
    return json;
    }
    }

BaseServiceImpl

public JSONObject uploadlbFile(MultipartFile file, String path, HttpSession session, HttpServletRequest request)
            throws Exception {
        // 生成一个文件名16位数字+字母
        String fileRandomNameId = Integer.toHexString(new Random().nextInt());
        fileRandomNameId += Integer.toHexString(new Random().nextInt());
        String fileRandomName = Integer.toHexString(new Random().nextInt());
        fileRandomName += Integer.toHexString(new Random().nextInt());
        String realName = file.getOriginalFilename();
        String suffixName = realName.substring(realName.lastIndexOf("."));
        fileRandomName += suffixName;
        path = "/opt/tomcat/lbpic/";
        File tempFile = new java.io.File(path, fileRandomName);
        if (!tempFile.getParentFile().exists()) {
            tempFile.getParentFile().mkdirs();
        }
        if (tempFile.exists()) {
            tempFile.delete();
        }
        // tempFile.createNewFile();
        // file.transferTo(tempFile);
        session = request.getSession();
        session.setAttribute("picname", tempFile);
        session.setAttribute("multpart", file);
        session.setAttribute("path", path);
        session.setAttribute("fileName", fileRandomName);
        // 封装文件的信息
        JSONObject fileInfor = new JSONObject();
        fileInfor.put("fileId", fileRandomNameId);
        fileInfor.put("fileName", fileRandomName);
        fileInfor.put("fileType", suffixName);
        fileInfor.put("filePath", tempFile.getAbsolutePath());
        return fileInfor;
    }

猜你喜欢

转载自blog.csdn.net/qq_38087648/article/details/79976670