1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 文件上传和OSS上传至阿里云

文件上传和OSS上传至阿里云

时间:2022-10-28 05:39:22

相关推荐

文件上传和OSS上传至阿里云

上传文件至本地 上传至阿里云在下面

简单实现上传文件(注意事项 ------ 只能post请求,不是上传至阿里云)

//Controller@PostMapping("/upload/file")@ResponseBodypublic String upload(@RequestParam("file") MultipartFile multipartFile,HttpServletRequest request){if (multipartFile.isEmpty()){return "文件为空";}String dir = request.getParameter("dir");return uploadService.uploadFile(multipartFile,dir);}/*** MultipartFile 是SpringMVC提供的文件上传接受类* 它的底层会自动的 和HttpServletRequest request中的request.getInputStream()融合* 从而达到文件上传的效果* 从而得知文件上传的底层基于request.getInputStream()* @param multipartFile* @param dir* @return*/public String uploadFile(MultipartFile multipartFile,String dir){File targetFile = new File("E://谷歌/IOFiles/" + dir);try {if (!targetFile.exists())targetFile.mkdirs();File filename = new File(targetFile,"aaa.png");multipartFile.transferTo(filename);return "上传成功";} catch (IOException e) {e.printStackTrace();return "file";}}

application.yml配置

servlet:multipart:enabled: true#单个文件大小max-request-size: 10MB#临时文件存储路径location: F://data/temp----------#文件达到多少时存入磁盘file-size-threshold: 20MB

前端

//html<form action="/upload/file" enctype="multipart/form-data" method="post"><input type="hidden" name="dir" value="images"><input type="file" name="file"><input type="submit" value="submit"></form>

!!!!!!!!!!!!!!!上传文件至阿里云!!!!!!!!!!!!!!!!!

首先引入SDK依赖

<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.0</version></dependency>

JDK9之后的还需引入下面两个(JDK9)

<dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.1</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency><!-- no more than 2.3.3--><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>2.3.3</version></dependency>

具体代码可参考阿里云的 API文档

下面是我的参数 改为自己的参数就ok

//例子public String uploadOss(MultipartFile multipartFile){// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "oss-cn-";// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。String accessKeyId = "LTAI5t922WrhhUZePmVupF3B";String accessKeySecret = "LRn312316rjdhdn5BPNejsQT";// 填写Bucket名称,例如examplebucket。String bucketName = "1bucket";// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {//2.获取文件上传流InputStream inputStream = multipartFile.getInputStream();// InputStream inputStream = new FileInputStream(multipartFile);//3.构建日期目录String datePath = new SimpleDateFormat("yyyy/MM/dd").format(new Date());//4.获取文件名称String orginName = multipartFile.getOriginalFilename();// String orginName = multipartFile.getName();String filename = UUID.randomUUID().toString();String suffix = orginName.substring(orginName.lastIndexOf("."));String newName = filename + suffix;String fileUrl = datePath+"/"+newName;//文件上传到阿里云ossClient.putObject(bucketName, fileUrl,inputStream);return "https://"+bucketName+"."+endpoint+"/"+fileUrl;} 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 (Exception 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());return "fail";} finally {if (ossClient != null) {ossClient.shutdown();}}return "success";}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。