OSS不支持通過(guò)一個(gè)網(wǎng)絡(luò)地址來(lái)上傳圖片,所以若想將網(wǎng)絡(luò)上的圖片上傳到OSS上需要走點(diǎn)彎路。

成都創(chuàng)新互聯(lián)是專業(yè)的上饒網(wǎng)站建設(shè)公司,上饒接單;提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行上饒網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
1、通過(guò)鏈接將圖片下載到本地的一個(gè)文件夾下面
2、用OSS上傳該文件夾下的文件
3、上傳完成后刪除本地的文件
具體代碼如下:
//獲取當(dāng)前項(xiàng)目的絕對(duì)路徑
public static String getTomcatPath(){
String nowpath;
String tempdir;
nowpath=System.getProperty("user.dir");
tempdir=nowpath.replace("bin", ""); //把bin 文件夾變到 webapps文件里面
return tempdir;
}
/**
* 將圖片下載下來(lái)后,上傳到OSS
* @param imgLink
* @param downloadPath
* @return
* @throws Exception
*/
private String downloadImagAndUploadToOss(String imgLink,String downloadPath) throws Exception{
List<String> urlList=new ArrayList<String>();
urlList.add(imgLink);
String imgName=DateUtil.formatDate(new Date(), "yyyyMMddhhmmss")+UuidUtil.createUUID()+".jpg";
downloadPicture(urlList,downloadPath,imgName);
String key="carAlbum/"+imgName;
String imgUrl=OSSObjectAPI.genOssPicUrl(OSSObjectAPI.XI_AN_BUCKET_NAME,OSSObjectAPI.XIAN_ACCESS_ID,OSSObjectAPI.XIAN_ACCESS_KEY,
"http://oss-cn-zhangjiakou.aliyuncs.com/",downloadPath+imgName,key);
FileUtil.delete(downloadPath+imgName);
return imgUrl;
}
/**
* 傳入要下載的圖片的url列表,將url所對(duì)應(yīng)的圖片下載到本地
* @param urlList
* @throws Exception
*/
private void downloadPicture(List<String> urlList,String path,String imgName) throws Exception {
if(urlList==null||urlList.size()==0){
return;
}
URL url = null;
FileOutputStream fileOutputStream =null;
InputStream inputStream =null;
for (String urlString : urlList) {
try {
url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0");
connection.setConnectTimeout(10 * 1000);
connection.setReadTimeout(15 * 1000);
inputStream = connection.getInputStream();
byte[] buffer = new byte[1024];
int length;
fileOutputStream= new FileOutputStream(path+ File.separator+ imgName);
while ((length = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
inputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)頁(yè)名稱:java獲取網(wǎng)絡(luò)圖片上傳到OSS的方法
本文網(wǎng)址:http://www.chinadenli.net/article14/peeide.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站維護(hù)、移動(dòng)網(wǎng)站建設(shè)、外貿(mào)建站、微信小程序、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)