多商品提交JAVA端

上篇我们提到了多商品提交的小程序端,前端JS代码写完了现在我们来写后端代码.这里我的环境是JDK1.8+SpringBoot+Mybatis+MySql,那么就解决图片上传和删除的代码.因为不涉及到数据入库的操作.订单保存我相信大家都没兴趣看.

    @PostMapping("fileUpload")
	public JsonResult fileUpload(@RequestParam("file") MultipartFile file){
		try {
			String fileName = file.getOriginalFilename();
			String suffixName = fileName.substring(fileName.lastIndexOf("."));
			if (!(suffixName.contains("jpg") || suffixName.contains("png"))) {
				return JsonResult.successReuslt("不支持" + suffixName + "格式,请上传JPG或者PNG格式",null);
			}
			// 生成文件名称通用方法
			StringBuilder tempName = new StringBuilder();
			tempName.append(DateUtil.dateToString(new Date(), DateUtil.data_now_time_format))
					.append(UUID.randomUUID().toString().replace("-", "")).append(suffixName);
			String newFileName = tempName.toString();
			File fileDirectory = new File(WxConfig.FILE_PATH);
			// 创建文件
			File destFile = new File(WxConfig.FILE_PATH + newFileName);
			if (!fileDirectory.exists()) {
				if (!fileDirectory.mkdir()) {
					throw new IOException("文件夹创建失败,路径为:" + fileDirectory);
				}
			}
			file.transferTo(destFile);
			
			return JsonResult.successReuslt("成功",WxConfig.Http_Url + newFileName);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.successReuslt("上传失败",null);
		}
	    
	}
	
	@GetMapping("deleteImg")
	public JsonResult deleteImg (String imgPath) {
		int lastIndexOf = imgPath.lastIndexOf("/");
		String img_path = imgPath.substring(lastIndexOf + 1, imgPath.length());
		img_path = WxConfig.FILE_PATH + img_path;
		File file = new File(img_path);
		if (file.exists()) {//文件是否存在
			if (file.delete()) {//存在就删了,返回1
				return JsonResult.successReuslt("成功!", null);
			} else {
				return JsonResult.erroReuslt("失败");
			}
		} else {
			return JsonResult.erroReuslt("失败");
		}
		
	}
WxConfig.FILE_PATH :这是图片服务器路径(静态资源映射)
WxConfig.Http_Url :这是我的图片路径前缀,如果是本地就是localhost.如果是线上就是域名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值