java 獲取網絡圖片


String photoUrl = data.getString("attachment");
String filename = photoUrl.substring(photoUrl.lastIndexOf("/"));

//生成圖片鏈接的url類
URL urlImg = new URL(photoUrl);
HttpURLConnection httpcon =(HttpURLConnection)urlImg.openConnection(); 
httpcon.addRequestProperty("User-Agent","Mozilla / 4.76"); 
InputStream is = httpcon.getInputStream();
//定義字節數組大小
byte[] buffer = new byte[1024];
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
int rc = 0;  
while ((rc = is.read(buffer, 0, 100)) > 0) {  
    swapStream.write(buffer, 0, rc);  
}  
buffer = swapStream.toByteArray();
String unit_id=getCommont().getUnit_id();
String destFilePath=Constant.ANNEXPATH+File.separator+getCommon().getOrgan_code()+File.separator+unit_id+File.separator+"JX";
File file=new File(destFilePath);

/**
 *文件路徑是否存在 不存在則重新創建     配置路徑/區划代碼/單位ID/業務類型
 */
if(!file.exists()){
    file.mkdirs();
}
file=null;
/**
 * 文件命名規則:上傳時間_附件設置ID
 */
String attach_name=DateTime.toLocaleFileString()+"_"+annexSet.getAnnexSetId()+"."+FileUtil.getExtension(filename);
File fj=new File(destFilePath,attach_name);
if (!fj.exists()){
    fj.createNewFile();
}
try (OutputStream outputStream = new FileOutputStream(fj);) {
    outputStream.write(buffer);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM