Java URL Download downloadFile(final String url)

Here you can find the source of downloadFile(final String url)

Description

download File

License

Open Source License

Declaration

public static byte[] downloadFile(final String url) throws Exception 

Method Source Code

//package com.java2s;
/**//  w  ww.j a v a 2  s  . c o m
 *
 * Licensed under the GNU General Public License (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.gnu.org/licenses/gpl.txt
 *
 * 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.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

public class Main {
    public static byte[] downloadFile(final String url) throws Exception {
        final URL fileUrl = new URL(url);
        final InputStream in = fileUrl.openStream();
        final ByteArrayOutputStream buf = new ByteArrayOutputStream();
        final byte[] readBuf = new byte[1024];
        int read = in.read(readBuf);
        while (read > 0) {
            buf.write(readBuf, 0, read);
            read = in.read(readBuf);
        }
        return buf.toByteArray();
    }
}

Related

  1. downloadBinary(URL BaseURL, String Name, File TargetDirectory)
  2. downloadData(String url, String params)
  3. downloadDirectory(URL dirUrl, File destDir)
  4. downloadFile(File parent, String prefix, String suffix, URL url)
  5. downloadFile(File target, String urlStr)
  6. downloadFile(String fileURL, String localFileName, String destinationDir)
  7. downloadFile(String fileURL, String saveDir)
  8. downloadFile(String fileURL, String savePath)
  9. downloadFile(String fileURL, String targetDirectory)