java中怎么將文件上傳到oss,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
“專業(yè)、務實、高效、創(chuàng)新、把客戶的事當成自己的事”是我們每一個人一直以來堅持追求的企業(yè)文化。 成都創(chuàng)新互聯(lián)公司是您可以信賴的網(wǎng)站建設服務商、專業(yè)的互聯(lián)網(wǎng)服務提供商! 專注于成都網(wǎng)站制作、網(wǎng)站建設、外貿(mào)網(wǎng)站建設、軟件開發(fā)、設計服務業(yè)務。我們始終堅持以客戶需求為導向,結(jié)合用戶體驗與視覺傳達,提供有針對性的項目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場,引領市場!
aliyun-sdk-oss-2.5.0.jar 這個文件 如果有最新版的話就下載最新版
aliyun.upload.AliyunConfigpackage aliyun.upload;public final class AliyunConfig { //你的oss所在域,要加http:// 不明白可以對照你的文件引用地址 public static String endpoint = "http://oss-cn-shenzhen.aliyuncs.com"; //密匙keyId 可以在阿里云獲取到 public static String accessKeyId = "xxxxxxx"; //密匙keySecret 可以在阿里云獲取到 public static String accessKeySecret = ""; //你的bucketName 名稱 即是你的OSS對象名稱 不明白查oss開發(fā)文檔專業(yè)術語 public static String bucketName = "";}
aliyun.upload.IAliyunUploadpackage 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.AliyunUploadVersion1package 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;/** * 阿里云文件上傳 - 簡易版 * */public class AliyunUploadVersion1 implements IAliyunUpload{ /** * 斷點續(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); // 設置并發(fā)下載數(shù),默認1 uploadFileRequest.setTaskNum(5); // 設置分片大小,默認100KB uploadFileRequest.setPartSize(1024 * 1024 * 1); // 開啟斷點續(xù)傳,默認關閉 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 文件上傳服務
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 { //默認 本地存儲路徑 private final String save_local_path = "D:/java_class/Company/WebRoot/"; //默認 文件保存路徑 private String save_context_path = "Upload/images/"; public void setSavePath(String savePath) { this.save_context_path = savePath; } /** * 阿里云文件上傳 * @param * MultipartFile file 文件流 * @return * String 文件引用路徑 如 String filePath = "/upload/otherpic71/47280.jpg" * */ public String aliyunUploadFile(MultipartFile file) { //獲取文件名稱 String fileName = file.getOriginalFilename(); //生成存儲路徑 String save_handler_path = save_local_path + save_context_path; //獲得文件后綴 String prefix=fileName.substring(fileName.lastIndexOf(".")); //存儲目錄 File parentDir = new File(save_handler_path); //存儲目錄是否存在 if(!parentDir.exists()) { parentDir.mkdirs(); } //生成文件存儲名稱 String fileSaveName = RandomNumStr.createRandomString(7) + String.valueOf(new Date().getTime()) + prefix; try{ File saveFile = new File(save_handler_path,fileSaveName); //移動臨時文件 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; } } /** * 文件存儲 * @param * MyltipartFile file 文件資源 * @return * 文件文件存儲地址 * */ public String localUploadFile(MultipartFile file,HttpServletRequest request) { //獲取文件名稱 String fileName = file.getOriginalFilename(); //生成存儲路徑 String save_handler_path = save_local_path + save_context_path; //獲得文件后綴 String prefix=fileName.substring(fileName.lastIndexOf(".")); //存儲目錄 File parentDir = new File(save_handler_path); //存儲目錄是否存在 if(!parentDir.exists()) { parentDir.mkdirs(); } //生成文件存儲名稱 String fileSaveName = RandomNumStr.createRandomString(7) + String.valueOf(new Date().getTime()) + prefix; try{ //移動臨時文件 file.transferTo(new File(save_handler_path,fileSaveName)); //文件地址 return Tool.getDomain(request) + save_context_path + fileSaveName; }catch(IOException e) { return null; } }}
看完上述內(nèi)容,你們掌握java中怎么將文件上傳到oss的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!