Java URL Download downImg(String filePath, String imageUrl, String fileName)

Here you can find the source of downImg(String filePath, String imageUrl, String fileName)

Description

down Img

License

Apache License

Declaration

public static boolean downImg(String filePath, String imageUrl, String fileName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static boolean downImg(String filePath, String imageUrl, String fileName) {
        try {//from  w ww .  ja  v a2  s. c o m
            File files = new File(filePath);
            if (!files.exists()) {
                files.mkdirs();
            }
            URL url = new URL(imageUrl);
            URLConnection con = url.openConnection();
            InputStream is = con.getInputStream();
            if (!filePath.endsWith("/")) {
                filePath = filePath + "/";
            }
            File file = new File(filePath + fileName);
            FileOutputStream out = new FileOutputStream(file);
            int i = 0;
            while ((i = is.read()) != -1) {
                out.write(i);
            }
            is.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

Related

  1. download(final String sURL, final String sFilename)
  2. download(final URL url)
  3. download(String downloadUrlStr, String filename)
  4. download(String fileUrl, String savePath, String fileName)