springboot3+vue3(七)文件上传
本地存储案例
@RestController public class FileUploadController { @PostMapping("/upload") public Result<String> upload(MultipartFile file) throws IOException{ //把文件上传到本地磁盘上 String originalFilename = file.getOriginalFilename(); //保证文件的名字是唯一的,从而防止文件覆盖 这里使用UUID+文件后缀 String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf(".")); //上传文件至本地磁盘目录 file.transferTo(new File("D:\\MyDesk\\upimg\\"+filename)); return Result.success("URL:111"); } }
上传三方服务器思路:
1、注册帐号,充值,购买服务器OSS。如:阿里云、华为云、百度云服务器
2、参照官方SDK编写入门程序(包括辅助软件依赖jar包、代码示例等)
3、集成使用
阿里云案例
(1)打开https://www.aliyun.com/ ,申请阿里云账号并完成实名认证。
(2)充值 (可以不用做)
(3)开通OSS 登录阿里云官网。 点击右上角的控制台。
将鼠标移至产品,找到并单击对象存储OSS,打开OSS产品详情页面。在OSS产品详情 页中的单击立即开通。
开通服务后,在OSS产品详情页面单击管理控制台直接进入OSS管理控制台界面。您也 可以单击位于官网首页右上方菜单栏的控制台,进入阿里云管理控制台首页,然后单 击左侧的对象存储OSS菜单进入OSS管理控制台界面。
(4)创建存储空间 新建Bucket,命名 ,读写权限为 公共读
封装的工具类
public class AliOssUtil { //bucket的EndPoint private static final String ENDPOINT = "https://oss-cn-beijing.aliyuncs.com"; private static final String ACCESS_KEY_ID = "LTAI5tQ8e13igWZUMTjMEEQV"; private static final String SECRET_ACCESS_KEY = "MffMJoM24sc59SEBEJQDb0cfBVOAC9"; private static final String BUCKET_NAME = "big-event-gwd"; //上传文件,返回文件的公网访问地址 //objectName -- 文件名 //inputStream -- 上传的file文件 public static String uploadFile(String objectName, InputStream inputStream){ // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(ENDPOINT,ACCESS_KEY_ID,SECRET_ACCESS_KEY); //公文访问地址 String url = ""; try { // 创建存储空间。 ossClient.createBucket(BUCKET_NAME); ossClient.putObject(BUCKET_NAME, objectName, inputStream); url = "https://"+BUCKET_NAME+"."+ENDPOINT.substring(ENDPOINT.lastIndexOf("/")+1)+"/"+objectName; } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } return url; } }
完善后的upload接口
@RestController public class FileUploadController { @PostMapping("/upload") public Result<String> upload(MultipartFile file) throws IOException{ //把文件上传到本地磁盘上 String originalFilename = file.getOriginalFilename(); //保证文件的名字是唯一的,从而防止文件覆盖 这里使用UUID+文件后缀 String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf(".")); ////上传文件至本地磁盘目录 //file.transferTo(new File("D:\\MyDesk\\upimg\\"+filename)); //上传文件至阿里云 String url = AliOssUtil.uploadFile(filename, file.getInputStream()); return Result.success(url); } }