Java URL Download downloadHttp(String downloadURL, String basePath, String commonPathURL)

Here you can find the source of downloadHttp(String downloadURL, String basePath, String commonPathURL)

Description

download Http

License

Apache License

Declaration

public static File downloadHttp(String downloadURL, String basePath, String commonPathURL) throws Exception 

Method Source Code

//package com.java2s;
/**//from  ww w . j av a 2s  . c om
 *  Copyright 2009, 2011 Welocalize, Inc. 
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  
 *  You may obtain a copy of the License at 
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;

public class Main {
    public static File downloadHttp(String downloadURL, String basePath, String commonPathURL) throws Exception {
        String urlDecode = URLDecoder.decode(downloadURL, "UTF-8").replaceAll("\\\\", "/");
        urlDecode = urlDecode.replace(" ", "%20");

        URL url = new URL(urlDecode);
        HttpURLConnection hurl = (HttpURLConnection) url.openConnection();
        hurl.connect();
        InputStream is = hurl.getInputStream();

        String realPath = URLDecoder.decode(commonPathURL, "UTF-8").replaceAll("\\\\", "/").replace(" ", "%20");
        realPath = realPath.replace("%20", " ");

        String fullPath = getFileName(realPath, basePath);
        File file = new File(fullPath);
        saveFile(is, file);

        return file;
    }

    private static String getFileName(String url, String basePath) throws Exception {
        String[] url1 = url.split("cxedocs");
        String after = url1[1];
        String before = basePath;
        if (before == null) {
            before = new File(".").getAbsolutePath();
        }
        String fileName = before + after;
        return fileName;
    }

    private static void saveFile(InputStream is, File file) throws IOException, FileNotFoundException {
        file.getParentFile().mkdirs();
        file.createNewFile();
        FileOutputStream outstream = new FileOutputStream(file);
        int c;
        while ((c = is.read()) != -1) {
            outstream.write(c);
        }
        outstream.close();
        is.close();
        if (file.length() == 0) {
            file.delete();
        }
    }
}

Related

  1. downloadFileToGivenNameElseExtension( URLConnection urlConnection, String fileName)
  2. downloadFromURL(String url)
  3. downloadFromUrl(String urlString, PrintStream logger)
  4. downloadFromUrl(URL url, String localFilename)
  5. downloadGzipCompressedFile(URL url, File destination)
  6. downloadImg(String urlPath, File file)
  7. downloadMobiFromEpubUrl(String href)
  8. downloadPageSource(String stringURL)
  9. downloadSource(PrintStream out, PrintStream err, String srcURL, String dirStr, boolean extract)