gistfile1.txt //图片保存 和弹框框架配合 $('#imgSave').on('click', function () { $('.update-store-form .upload-msg').html(""); if ($('#upload-file').val()) { layer.open({ content: '您确认保存吗?', btn: ['确认', '取消']
//图片保存 和弹框框架配合 $('#imgSave').on('click', function () { $('.update-store-form .upload-msg').html(""); if ($('#upload-file').val()) { layer.open({ content: '您确认保存吗?', btn: ['确认', '取消'], shadeClose: true, yes: function () { layer.open({ content: '确认', time: 1, success: function (layero, index) { comitUpdateShop(); //调用函数头像上传 tag = "你修改了头像" $('#imgSave').removeAttr("disabled"); } }); }, no: function () { layer.open({content: '您选择了取消', time: 1}); } }); } else { showTip("请选择图片"); } }); //函数图片上传 var comitUpdateShop = function () { var target = $('.update-store-form .upload-msg'); if ($('#upload-file').val()) { var formData = new FormData($("#fileForm")[0], $("#shopId").val()); $("#imgSave").attr("disabled", true);//防表单提交 $.ajax({ url: 'shop/shopSrc', type: "post", data: formData, processData: false, contentType: false, success: function (data, status) { if (data == 0) { target.html('*' + data.desc); } else { target.html('*' + data.desc); } }, error: function () { target.html('* 保存失败,请重新上传!'); } }); } else { target.html('* 保存失败,请重新上传!'); } $('#upload-file').val(''); target.addClass('show'); } ////////////////////////////////html代码////////////////////////////////////////////////////////////////java后台代码/////////////////////////////////////// @PostMapping(value = "shop/shopSrc") public ResponseMsg fildUpload(@RequestParam(value = "fileName", required = false) MultipartFile file, @RequestParam(value = "shopid", required = false) String shopid, HttpServletRequest request) throws IllegalStateException, IOException { if (file != null) {// 判断上传的文件是否为空 String path = null;// 文件路径 String type = null;// 文件类型 String fileName = file.getOriginalFilename();// 文件原名称 if (file.getSize() >= 1024000) return new ResponseMsg(-1, null, "图片超过1M"); System.out.println("上传的文件原名称:" + fileName); type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null; if (type != null) {// 判断文件类型是否为空 if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) { // 项目在容器中实际发布运行的根路径 // String realPath = request.getClass().getResource("/").getFile().toString(); String root = ""; if (System.getProperty("os.name").toLowerCase().startsWith("win")) { root = "d:"; } String realPath = root + Constance.UPLOAD_PATH + "img" + System.getProperty("file.separator");//request.getClass().getResource("/").getFile().toString(); File tmp = new File(realPath); if (!tmp.exists()) { tmp.mkdirs(); } // 自定义的文件名称 // String trueFileName = String.valueOf(System.currentTimeMillis())+"." + type; String trueFileName = UUID.randomUUID().toString()+"." + type; // 设置存放图片文件的路径 //path=realPath+System.getProperty("file.separator")+trueFileName; // path="/static/img/"+trueFileName; // path = System.getProperty("file.separator") + "static" + System.getProperty("file.separator") + "img" + System.getProperty("file.separator") + trueFileName; System.out.println("存放图片文件的路径:" + realPath + trueFileName); // 转存文件到指定的路径 file.transferTo(new File(realPath + trueFileName)); // String savePath = System.getProperty("file.separator") + "img" + System.getProperty("file.separator") + trueFileName; // String savePath = "/upload/" + "img" + System.getProperty("file.separator") + trueFileName; String savePath = "/upload/" + "img" + "/" + trueFileName; shopService.updateShopImgSrc(shopid, savePath); System.out.println("文件成功上传到指定目录下" + savePath); return new ResponseMsg(0, "图片保存成功"); } else { return new ResponseMsg(-1, "图片后辍名错误"); } } } else { return new ResponseMsg(-1, "图片不存在"); } return null; } ///////////////////////代码用到的类和变量 ///////////////////////////// public final class Constance { public static final String UPLOAD_PATH = "/home/cma/custWifi/upload/"; }