根據一個oss的地址把圖片轉換成一個文件


//圖片轉換
public static File getFileByUrl(String fileUrl) {
    ByteArrayOutputStream outStream =new ByteArrayOutputStream();
    BufferedOutputStream stream =null;
    InputStream inputStream =null;
    File file =null;
    String suffix ="";
    try {
        suffix = fileUrl.split("\\.")[1];
        URL imageUrl =new URL(fileUrl);
        HttpURLConnection conn =(HttpURLConnection)imageUrl.openConnection();
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        inputStream = conn.getInputStream();
        byte[] buffer =new byte[1024];
        int len =0;
        while( (len=inputStream.read(buffer)) != -1 ){
            outStream.write(buffer, 0, len);
        }
        file = File.createTempFile("pattern", "." + suffix);
        log.info("臨時文件創建成功={}", file.getCanonicalPath());
        FileOutputStream fileOutputStream =new FileOutputStream(file);
        stream =new BufferedOutputStream(fileOutputStream);
        stream.write(outStream.toByteArray());
    } catch (Exception e) {
        log.error("創建人臉獲取服務器圖片異常", e);
    } finally {
        try {
            if (inputStream !=null) inputStream.close();
            if (stream !=null) stream.close();
            outStream.close();
        } catch (Exception e) {log.error("關閉流異常", e);}
    }
    return file;
}

 


免責聲明!

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



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