linux 上传图片路径,图片上传(和linux 兼容)

gistfile1.txt

//图片保存 和弹框框架配合

$('#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代码选择图片

仅支持JPG图片,且文件小于1M,尺寸

为200*200像素,请确认是否清晰。

p-dnjtecac-k.html

%7B%7Bshop.imgSrc%7D%7D

* 保存成功!

保存

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/";

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值