生活随笔
收集整理的這篇文章主要介紹了
Ucloud 文件上传
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關注 “弋凡”(YiFan)微信公眾號吧 記錄簡單筆記 做你的最愛
創建一個空間 創建一個令牌
查看官方文檔說明
https://github.com/ucloud/ufile-sdk-java
上傳文件
File file
= new File("your file path");try {PutObjectResultBean response
= UfileClient
.object(Constants
.OBJECT_AUTHORIZER
, config
).putObject(file
, "mimeType").nameAs("save as keyName").toBucket("upload to which bucket").setOnProgressListener(new OnProgressListener() {@Overridepublic void onProgress(long bytesWritten
, long contentLength
) {}}).execute();
} catch (UfileClientException e
) {e
.printStackTrace();
} catch (UfileServerException e
) {e
.printStackTrace();
}
案例
<dependency><groupId>cn.ucloud.ufile</groupId><artifactId>ufile-client-java</artifactId><version>2.4.4</version>
</dependency>
- application.properties 編寫
# ucloud 你創建的令牌 以及你的空間名
ucloud.ufile.publickey=TOKEN_xfd2a2-6fx8-4c6c-8af6-502x966x8xxxx
ucloud.ufile.privatekey=1e077ax2-a923-4a1x-bbea-9x2d8x32xxxx
ucloud.bucketName=zfan
import cn
.ucloud
.ufile
.UfileClient
;
import cn
.ucloud
.ufile
.api
.object
.ObjectConfig
;
import cn
.ucloud
.ufile
.auth
.ObjectAuthorization
;
import cn
.ucloud
.ufile
.auth
.UfileObjectLocalAuthorization
;
import cn
.ucloud
.ufile
.bean
.PutObjectResultBean
;
import cn
.ucloud
.ufile
.exception
.UfileClientException
;
import cn
.ucloud
.ufile
.exception
.UfileServerException
;
import org
.springframework
.beans
.factory
.annotation
.Value
;
import org
.springframework
.stereotype
.Service
;import java
.io
.File
;
import java
.io
.InputStream
;
import java
.util
.UUID
;@Service
public class UcloudProvider {@Value("${ucloud.ufile.publickey}")private String publicKey
;@Value("${ucloud.ufile.privatekey}")private String privateKey
;@Value("${ucloud.bucketName}")private String bucketName
;public String
upload(InputStream inputStream
, String mimeType
,String fileName
){File file
= new File("your file path");String newName
= "";String
[] split
= fileName
.split("\\.");if(split
.length
>1){newName
=UUID
.randomUUID()+"."+split
[split
.length
-1];}else {return null
;}try {ObjectAuthorization objectAuthorization
= new UfileObjectLocalAuthorization(publicKey
,privateKey
);ObjectConfig config
= new ObjectConfig("cn-bj", "ufileos.com");bucketName
= "zfan";PutObjectResultBean response
= UfileClient
.object(objectAuthorization
, config
).putObject(inputStream
, mimeType
).nameAs(newName
).toBucket(bucketName
).setOnProgressListener((bytesWritten
, contentLength
) -> { }).execute();if(response
!=null
&& response
.getRetCode() == 0 ){String url
= UfileClient
.object(objectAuthorization
, config
).getDownloadUrlFromPrivateBucket(newName
, bucketName
, 24 * 60 * 60).createUrl();return url
;}else {return "文件上傳失敗";}} catch (UfileClientException e
) {e
.printStackTrace();return null
;} catch (UfileServerException e
) {e
.printStackTrace();return null
;}}
}
@RestController
public class UploadController {@Autowiredprivate UcloudProvider ucloudProvider
;@RequestMapping("upload")public String
upload(HttpServletRequest request
) throws IOException
{MultipartHttpServletRequest multipartRequest
= (MultipartHttpServletRequest
)request
;MultipartFile file
= multipartRequest
.getFile("file");String upload
= ucloudProvider
.upload(file
.getInputStream(), file
.getContentType(), file
.getOriginalFilename());return upload
;}}
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title
</title>
</head>
<body><h4>首頁
</h4>
<form action="/upload" method="post" enctype="multipart/form-data"><p><input type="file" name="file"></p><p><input type="submit" value="提交"></p>
</form></body>
</html>
end -
快來關注“弋凡”微信公眾號吧
總結
以上是生活随笔為你收集整理的Ucloud 文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。