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);
}