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

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

Description

down Picture

License

Apache License

Declaration

public static Map<String, Object> downPicture(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.HttpURLConnection;
import java.net.URL;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static Map<String, Object> downPicture(String filePath, String imageUrl, String fileName) {
        Map<String, Object> map = new HashMap<String, Object>();
        Integer size = null;//from  w  w w .j  a  v  a  2  s.com
        String mime = null;
        try {
            File files = new File(filePath);
            if (!files.exists()) {
                files.mkdirs();
            }
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream is = connection.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);
            }
            size = connection.getContentLength();
            mime = connection.getContentType();
            is.close();
        } catch (Exception e) {

        }
        map.put("size", size);
        map.put("mime", mime);
        return map;
    }
}

Related

  1. downloadUrlToFile(URL url, File result)
  2. downloadWebpage(String url)
  3. downloadZip(URL file, File dump)
  4. downlod(String url, File dest)
  5. downNetImg(String filePath, String remotePath, String htmlUrl, String fileName)
  6. fetchUrl(String _url, String charset)
  7. fetchURL(String url)
  8. fetchURL(String url_string)
  9. fetchURL(String urlStr, List errors, Map requestProperties)