java實(shí)現(xiàn)上傳文件到oss(阿里云)功能示例
本文實(shí)例講述了java實(shí)現(xiàn)上傳文件到oss(阿里云)功能。分享給大家供大家參考,具體如下:
做這個(gè)功能之前我們需要導(dǎo)入阿里云OSS官方提供的工具包
aliyun-sdk-oss-2.5.0.jar 這個(gè)文件 如果有最新版的話就下載最新版
aliyun.upload.AliyunConfig
package aliyun.upload;
public final class AliyunConfig
{
//你的oss所在域,要加http:// 不明白可以對(duì)照你的文件引用地址
public static String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
//密匙keyId 可以在阿里云獲取到
public static String accessKeyId = "xxxxxxx";
//密匙keySecret 可以在阿里云獲取到
public static String accessKeySecret = "";
//你的bucketName 名稱(chēng) 即是你的OSS對(duì)象名稱(chēng) 不明白查oss開(kāi)發(fā)文檔專(zhuān)業(yè)術(shù)語(yǔ)
public static String bucketName = "";
}
aliyun.upload.IAliyunUpload
package aliyun.upload;
public interface IAliyunUpload
{
/**
* @param
* String filePathName 本地圖片路徑(D:/xxxx/xxxx....../xx/xx.jgp|xx.png|..)
* String savePathName 將要保存到OSS上的路徑地址
* */
public String uploadFile(String filePathName,String savePathName);
}
aliyun.upload.AliyunUploadVersion1
package aliyun.upload;
import java.io.File;
import java.io.IOException;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.oss.model.UploadFileResult;
import Log.Log;
/**
* 阿里云文件上傳 - 簡(jiǎn)易版
* */
public class AliyunUploadVersion1 implements IAliyunUpload
{
/**
* 斷點(diǎn)續(xù)傳
* */
@Override
public String uploadFile(String uploadFile,String savePathName)
{
OSSClient ossClient = new OSSClient(AliyunConfig.endpoint, AliyunConfig.accessKeyId, AliyunConfig.accessKeySecret);
try {
UploadFileRequest uploadFileRequest = new UploadFileRequest(AliyunConfig.bucketName,savePathName);
// 待上傳的本地文件
uploadFileRequest.setUploadFile(uploadFile);
// 設(shè)置并發(fā)下載數(shù),默認(rèn)1
uploadFileRequest.setTaskNum(5);
// 設(shè)置分片大小,默認(rèn)100KB
uploadFileRequest.setPartSize(1024 * 1024 * 1);
// 開(kāi)啟斷點(diǎn)續(xù)傳,默認(rèn)關(guān)閉
uploadFileRequest.setEnableCheckpoint(true);
UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
CompleteMultipartUploadResult multipartUploadResult =
uploadResult.getMultipartUploadResult();
return multipartUploadResult.getLocation();
} catch (OSSException oe) {
Log.e("*************************************************OSS upload file error create_date " + tool.Tool.getDate() + "*************************************");
Log.e("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
Log.e("Error Message: " + oe.getErrorCode());
Log.e("Error Code: " + oe.getErrorCode());
Log.e("Request ID: " + oe.getRequestId());
Log.e("Host ID: " + oe.getHostId());
Log.e("*************************************************OSS upload file error*************************************");
} catch (ClientException ce) {
Log.e("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.");
Log.e("Error Message: " + ce.getMessage());
} catch (Throwable e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
return null;
}
}
文件上傳容器
package aliyun.upload;
import org.springframework.stereotype.Repository;
@Repository("aliyun_upload")
public class AliyunUpload extends AliyunUploadVersion1
{
/**
* 上傳文件
* */
@Override
public String uploadFile(String filePath,String savePathName)
{
return super.uploadFile(filePath,savePathName);
}
}
spring.beans.service.UploadService 文件上傳服務(wù)
package spring.beans.service;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import aliyun.upload.AliyunUpload;
import tool.RandomNumStr;
import tool.Tool;
@Service(value="upload_service")
public class UploadService
{
//默認(rèn) 本地存儲(chǔ)路徑
private final String save_local_path = "D:/java_class/Company/WebRoot/";
//默認(rèn) 文件保存路徑
private String save_context_path = "Upload/images/";
public void setSavePath(String savePath)
{
this.save_context_path = savePath;
}
/**
* 阿里云文件上傳
* @param
* MultipartFile file 文件流
* @return
* String 文件引用路徑 如 String filePath = "http://aliyun.xxxx.xxxx/xxxx/xxxx/xxxx.jpg"
* */
public String aliyunUploadFile(MultipartFile file)
{
//獲取文件名稱(chēng)
String fileName = file.getOriginalFilename();
//生成存儲(chǔ)路徑
String save_handler_path = save_local_path + save_context_path;
//獲得文件后綴
String prefix=fileName.substring(fileName.lastIndexOf("."));
//存儲(chǔ)目錄
File parentDir = new File(save_handler_path);
//存儲(chǔ)目錄是否存在
if(!parentDir.exists())
{
parentDir.mkdirs();
}
//生成文件存儲(chǔ)名稱(chēng)
String fileSaveName = RandomNumStr.createRandomString(7) + String.valueOf(new Date().getTime()) + prefix;
try{
File saveFile = new File(save_handler_path,fileSaveName);
//移動(dòng)臨時(shí)文件
file.transferTo(saveFile);
//新增阿里云文件上傳
AliyunUpload aliyunUpload = new AliyunUpload();
String fileUrl = aliyunUpload.uploadFile(save_handler_path+fileSaveName,save_context_path+fileSaveName);
saveFile.delete();
return fileUrl;
}catch(IOException e)
{
return null;
}
}
/**
* 文件存儲(chǔ)
* @param
* MyltipartFile file 文件資源
* @return
* 文件文件存儲(chǔ)地址
* */
public String localUploadFile(MultipartFile file,HttpServletRequest request)
{
//獲取文件名稱(chēng)
String fileName = file.getOriginalFilename();
//生成存儲(chǔ)路徑
String save_handler_path = save_local_path + save_context_path;
//獲得文件后綴
String prefix=fileName.substring(fileName.lastIndexOf("."));
//存儲(chǔ)目錄
File parentDir = new File(save_handler_path);
//存儲(chǔ)目錄是否存在
if(!parentDir.exists())
{
parentDir.mkdirs();
}
//生成文件存儲(chǔ)名稱(chēng)
String fileSaveName = RandomNumStr.createRandomString(7) + String.valueOf(new Date().getTime()) + prefix;
try{
//移動(dòng)臨時(shí)文件
file.transferTo(new File(save_handler_path,fileSaveName));
//文件地址
return Tool.getDomain(request) + save_context_path + fileSaveName;
}catch(IOException e)
{
return null;
}
}
}
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Java實(shí)現(xiàn)整合文件上傳到FastDFS的方法詳細(xì)
- Java實(shí)現(xiàn)圖片上傳至FastDFS入門(mén)教程
- Java 客戶(hù)端操作 FastDFS 實(shí)現(xiàn)文件上傳下載替換刪除功能
- Java fastdfs客戶(hù)端實(shí)現(xiàn)上傳下載文件
- Java使用OSS實(shí)現(xiàn)上傳文件功能
- Java下載https文件并上傳阿里云oss服務(wù)器
- Java微信小程序oss圖片上傳的實(shí)現(xiàn)方法
- java獲取網(wǎng)絡(luò)圖片上傳到OSS的方法
- Java實(shí)現(xiàn)Fast DFS、服務(wù)器、OSS上傳功能
相關(guān)文章
mybatis通過(guò)if語(yǔ)句實(shí)現(xiàn)增刪改查操作
這篇文章主要介紹了mybatis通過(guò)if語(yǔ)句實(shí)現(xiàn)增刪改查操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Intellij IDEA遠(yuǎn)程debug教程實(shí)戰(zhàn)和要點(diǎn)總結(jié)(推薦)
這篇文章主要介紹了Intellij IDEA遠(yuǎn)程debug教程實(shí)戰(zhàn)和要點(diǎn)總結(jié)(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
mybatis generator只能生成insert和selectAll的操作
這篇文章主要介紹了mybatis generator只能生成insert和selectAll的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
java擴(kuò)展Hibernate注解支持java8新時(shí)間類(lèi)型
這篇文章主要介紹了java擴(kuò)展Hibernate注解支持java8新時(shí)間類(lèi)型,需要的朋友可以參考下2014-04-04
JAVA 并發(fā)容器的一些易出錯(cuò)點(diǎn)你知道嗎
今天給大家?guī)?lái)的文章是Java并發(fā)編程的相關(guān)知識(shí),文中對(duì)java同步容器與并發(fā)容器做了非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-09-09
Java實(shí)現(xiàn)整數(shù)的逆序輸出的三種方法
這篇文章主要介紹了Java實(shí)現(xiàn)整數(shù)的逆序輸出的三種方法,第一種是無(wú)限制整數(shù)的逆序輸出,第二種是非負(fù)整數(shù)的逆序輸出,第三種是非特殊情況的逆序輸出,每種方法給大家講解的非常詳細(xì)需要的朋友可以參考下2022-11-11
SpringBoot實(shí)現(xiàn)elasticsearch索引操作的代碼示例
這篇文章主要給大家介紹了SpringBoot如何實(shí)現(xiàn)elasticsearch 索引操作,文中有詳細(xì)的代碼示例,感興趣的同學(xué)可以參考閱讀下2023-07-07

