直播app源代码,SpringBoot 上传图片接口

直播app源代码,SpringBoot 上传图片接口实现的相关代码

yml里参数
menu:
  img:
    zipPath: /usr/local/img/menu/
    zipUrl: http://39.104.123.82:12604/img/menu/
    uploadPath: D:/usr/local/img/upload/
    uploadUrl: http://39.104.123.82:12604/img/upload/   
 
	@Value("${menu.img.uploadPath}")
	private String uploadPath;
	
	@Value("${menu.img.uploadUrl}")
	private String uploadUrl;
	
	@Value("${menu.img.zipPath}")
	private String zipPath;
	
	@Value("${menu.img.zipUrl}")
	private String zipUrl;
 
 
 
	/**
	 * 上传图片
	 * @param IdForm
	 * @return RestResult<Object>
	 */
	@ControllerLog
	@ResponseBody
	@RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
	public RestResult<Object> uploadImg(@RequestParam("file") MultipartFile file,String id) {
		
		log.info("Controller层,method[uploadImg]调用开始!");
		RestResult<Object> info = menuLayoutService.uploadImg(file,id); 
		log.info("Controller层,method[uploadImg]调用结束!");
		return info;
	}
 
 
	/**
	 * 上传图片
	 * @Description:
	 * @param: MultipartRequest file, HttpServletRequest request,  IdForm form
	 * @return:RestResult<Object>
	 * @author: zhanghd
	 * @date: 20181221 下午2:39:24
	 */
	@Override
	public RestResult<Object> uploadImg(@RequestParam("file") MultipartFile  file,String id) {
		String oldName = file.getOriginalFilename();
		String imgType = oldName.substring(oldName.lastIndexOf("."), oldName.length());
		String name = UUID.randomUUID().toString()+imgType; // 图片名
		String realpath = uploadPath + "/" + id;
		String fileName = writeUploadFile(file, realpath, name);
		String url = uploadUrl + fileName;
		return RestResult.genOkResult(url);
	}
 
	/**
	 * 文件上传处理
	 * @Description: 上传图片
	 * @param:MultipartFile file, String realpath, String fileName
	 * @return:String fileName
	 * @author: zhanghd
	 * @date: 20181221 下午5:00:56
	 */
	public static String writeUploadFile(MultipartFile file, String realpath, String fileName) {
		File fileDir = new File(realpath);
		if (!fileDir.exists())
			fileDir.mkdirs();
 
		InputStream input = null;
		FileOutputStream fos = null;
		try {
			input = file.getInputStream();
			fos = new FileOutputStream(realpath + "/" + fileName);
			IOUtils.copy(input, fos);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			IOUtils.closeQuietly(input);
			IOUtils.closeQuietly(fos);
		}
		return fileName;
	}

以上就是 直播app源代码,SpringBoot 上传图片接口实现的相关代码,更多内容欢迎关注之后的文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值