使用JAVA代码实现文件上传到阿里云

这篇博客提供了一个简单的JAVA代码示例,演示如何将文件上传到阿里云存储服务。用户需要替换endpoint、accessKeyId、accessKeySecret和bucket name等个性化配置信息。
摘要由CSDN通过智能技术生成

一个简单的demo,上传的时候根据你的阿里云更换相应的endpoint,accessKeyId,accessKeySecret和bucket name

package com;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;

import java.io.*;

/**
 * Created by Administrator on 2016/10/19 0019.
 */
public class Test02 {
    public static void main(String[] args) throws IOException {
        // 华东2的endpoint
        String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
        // accessKey请登录https://ak-console.aliyun.com/#/查看
        String accessKeyId = "your accessKeyId";
        String accessKeySecret = "your accessKeySecret";
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传文件流
        // InputStream inputStream = new FileInputStream("C:\\Aliyun.png");
//		    System.out.println(ossClient.putObject("dajia-test-1", "1.txt", createSamp
上传文件到阿里云OSS可以通过以下步骤实现: 1. 引入aliyun-java-sdk-oss依赖 ``` <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-java-sdk-oss</artifactId> <version>3.10.0</version> </dependency> ``` 2. 创建OSSClient对象 ``` String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; String accessKeyId = "yourAccessKeyId"; String accessKeySecret = "yourAccessKeySecret"; String bucketName = "yourBucketName"; OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); ``` 3. 上传文件 ``` String objectName = "yourObjectName"; // 上传到OSS后的文件名,可以包含路径 File file = new File("yourLocalFile"); // 要上传的文件路径 ossClient.putObject(bucketName, objectName, file); ``` 4. 关闭OSSClient对象 ``` ossClient.shutdown(); ``` 完整的Java代码示例: ``` import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import java.io.File; public class OSSUploader { public static void main(String[] args) { String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; String accessKeyId = "yourAccessKeyId"; String accessKeySecret = "yourAccessKeySecret"; String bucketName = "yourBucketName"; String objectName = "yourObjectName"; // 上传到OSS后的文件名,可以包含路径 File file = new File("yourLocalFile"); // 要上传的文件路径 // 创建OSSClient对象 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上传文件 ossClient.putObject(bucketName, objectName, file); // 关闭OSSClient对象 ossClient.shutdown(); } } ``` 需要注意的是,上传文件时可以指定上传后的文件名,也可以使用原文件名。同时,上传文件的大小不能超过OSS的限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值