上传文件(图片,视频,音频)(File文件流 和 在线地址)upload File

上传文件(图片,视频,音频)(File文件流 和 在线地址)
/**
	 * 上传文件并保存至附件表
	 *
	 * @param file 文件
	 * @return ObjectStat
	 */
	@SneakyThrows
	@PostMapping("/put-file-attach")
	public R<BladeFile> putFileAttach(@RequestParam MultipartFile file) {
		String fileName = file.getOriginalFilename();
		BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
		Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
		bladeFile.setAttachId(attachId);
//		if (Xtool.isNotNull(bladeFile.getLink())){
//			bladeFile.setLink(bladeFile.getLink().replaceAll(url,"").trim());
//		}
		//这里 的 bladeFile 对象里的 domain 属性 读取的是 yml 里面的 oss.endpoint 的 值
		//还有一个属性是 name , 就是 不带 ip和端口的上传路径
		// 还有一个属性是 link = domain + name
		//所以如果要将 link里面的前缀去掉 可以 可以直接 set link (get name)
		bladeFile.setLink(bladeFile.getName());
		//或者更简单的方法 直接修改 ossBuilder.template().putFile() 方法
		return R.data(bladeFile);
	}

	/**
	 * 上传文件(链接地址)
	 * @param url
	 * @return
	 */
	@SneakyThrows
	@PostMapping("/put-file-attach-url")
	public R<BladeFile> putFileAttachUrl(@RequestParam String url) {
		File imageFile = getFile(url);
		MultipartFile file = getMultipartFile(imageFile);
		String fileName = file.getOriginalFilename();
		BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
		Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
		bladeFile.setAttachId(attachId);
//		if (Xtool.isNotNull(bladeFile.getLink())){
//			bladeFile.setLink(bladeFile.getLink().replaceAll(url,"").trim());
//		}
		//这里 的 bladeFile 对象里的 domain 属性 读取的是 yml 里面的 oss.endpoint 的 值
		//还有一个属性是 name , 就是 不带 ip和端口的上传路径
		// 还有一个属性是 link = domain + name
		//所以如果要将 link里面的前缀去掉 可以 可以直接 set link (get name)
		bladeFile.setLink(bladeFile.getName());
		//或者更简单的方法 直接修改 ossBuilder.template().putFile() 方法
		return R.data(bladeFile);
	}

	/**
	 * 将图片转为file
	 *
	 * @param url 图片url
	 * @return File
	 */
	private static File getFile(String url) throws Exception {
		//对本地文件命名
		String fileName = url.substring(url.lastIndexOf("."), url.length());
		File file = null;

		URL urlfile;
		InputStream inStream = null;
		OutputStream os = null;
		try {
			file = File.createTempFile("net_url", fileName);
			//下载
			urlfile = new URL(url);
			inStream = urlfile.openStream();
			os = new FileOutputStream(file);

			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != os) {
					os.close();
				}
				if (null != inStream) {
					inStream.close();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return file;
	}

	/**
	 * File转换为MultipartFile
	 *
	 * @param file 图片file
	 * @return MultipartFile
	 */
	public static MultipartFile getMultipartFile(File file) {
		DiskFileItem item = new DiskFileItem("file"
			, MediaType.MULTIPART_FORM_DATA_VALUE
			, true
			, file.getName()
			, (int) file.length()
			, file.getParentFile());
		try {
			OutputStream os = item.getOutputStream();
			os.write(FileUtils.readFileToByteArray(file));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new CommonsMultipartFile(item);
	}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值