从url获取图片

从url获取图片

public static File getLogoFromWeb(String logourl, String uploadPath) {
        try {
            URL url = new URL(logourl);
            String fileType = logourl.substring(logourl.lastIndexOf("."), logourl.length());
            File outFile = new File(uploadPath + UUID.randomUUID().toString().replace("-", "") + fileType);
            OutputStream os = new FileOutputStream(outFile);
            InputStream is = url.openStream();
            byte[] buff = new byte[1024];
            while (true) {
                int readed = is.read(buff);
                if (readed == -1) {
                    break;
                }
                byte[] temp = new byte[readed];
                System.arraycopy(buff, 0, temp, 0, readed);
                os.write(temp);
            }
            is.close();
            os.close();
            return outFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

你可能感兴趣的:(从url获取图片)