【Java】UploadUtil

package com.copote.common.utils;

import com.copote.common.constant.FileConst;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
* 文件上传工具类
*/
public class UploadUtil {

/**
 * 将MultipartFile转化为file并保存到服务器上的某地
 */
public static String saveFileFromInputStream(MultipartFile file,String fileName) throws IOException {
	OutputStream os = null;
	InputStream inputStream;
	String url =null;
	inputStream = file.getInputStream();
	String path = FileConst.UPLOAD_DISK + FileConst.UPLOAD_PATH;
	try {
		byte[] bs = new byte[1024*1024];// 1K的数据缓冲
		// 读取到的数据长度
		int len;
		// 输出的文件流保存到本地文件
		File tempFile = new File(path);
		if (!tempFile.exists()) {tempFile.mkdirs();}
		url = tempFile.getPath() + File.separator + fileName;
		os = new FileOutputStream(url);
		// 开始读取
		while ((len = inputStream.read(bs)) != -1) {
			os.write(bs, 0, 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用jeecg-boot框架将图片上传到华为云OBS的步骤: 1.在华为云OBS中创建一个存储桶,并获取Access Key ID和Secret Access Key。 2.在jeecg-boot项目中添加OBS SDK依赖,可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.obs</groupId> <artifactId>obs-sdk-java</artifactId> <version>3.20.3</version> </dependency> ``` 3.在jeecg-boot项目中添加OBS配置信息,可以在application.yml文件中添加以下配置: ```yaml obs: endpoint: https://your-endpoint.obs.cn-north-1.myhuaweicloud.com access-key: your-access-key-id secret-key: your-secret-access-key bucket-name: your-bucket-name ``` 4.修改upload方法,添加上传到OBS的逻辑: ```java public static String upload(MultipartFile file, String bizPath, String uploadType) { String url = ""; try { if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) { url = MinioUtil.upload(file, bizPath); } else if (CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)) { url = OssBootUtil.upload(file, bizPath); } else if (CommonConstant.UPLOAD_TYPE_OBS.equals(uploadType)) { url = ObsBootUtil.upload(file, bizPath); } } catch (Exception exception) { exception.printStackTrace(); } return url; } public static String upload(MultipartFile file, String bizPath) throws IOException { String fileName = file.getOriginalFilename(); String suffix = fileName.substring(fileName.lastIndexOf(".")); String key = bizPath + "/" + UUID.randomUUID().toString().replaceAll("-", "") + suffix; ObsClient obsClient = new ObsClient(accessKey, secretKey, endpoint); try { obsClient.putObject(bucketName, key, file.getInputStream()); } finally { obsClient.close(); } return "https://" + bucketName + "." + endpoint + "/" + key; } ``` 5.调用upload方法上传图片到华为云OBS: ```java MultipartFile file = ...; // 获取上传的文件 String bizPath = "images"; // 业务路径 String uploadType = CommonConstant.UPLOAD_TYPE_OBS; // 上传类型为OBS String url = UploadUtil.upload(file, bizPath, uploadType); // 调用upload方法上传文件 System.out.println(url); // 输出上传后的文件URL ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值