通過io流將字節流轉化成圖片並保存在本地路徑


//獲取圖片流
FileInputStream fileInputStream = new FileInputStream(new File("C:\\Users\\11841\\Desktop\\zz.jpg"));
int leng;
byte[] b = new byte[1024];
Date date = new Date();
String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
String url = "C:\\alarmImages\\"+time+"\\";
System.out.println(url);
File parent = new File(url);
if(!parent.exists()|| !parent.isDirectory()) {
boolean mkdirs = parent.mkdirs();
System.out.println(mkdirs);
}
String imgname = DateTime.now().toString("yyyyMMddhhmmss") + ".jpg";
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(url+imgname));//打開輸入流
while ((leng=fileInputStream.read(b))!=-1){
imageOutput.write(b, 0, leng);//將byte寫入硬盤
}
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + url);
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}


免責聲明!

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



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