MyTest.DownloadFileTest.java Source code

Java tutorial

Introduction

Here is the source code for MyTest.DownloadFileTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MyTest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class DownloadFileTest {

    public static void main(String args[]) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String url = "http://www.myexperiment.org/workflows/16/download/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow?version=7";
        System.out.println(url.charAt(50));
        HttpGet httpget = new HttpGet(url);
        HttpEntity entity = null;
        try {
            HttpResponse response = httpclient.execute(httpget);
            entity = response.getEntity();
            if (entity != null) {
                InputStream is = entity.getContent();
                String filename = "testdata/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow";
                BufferedInputStream bis = new BufferedInputStream(is);
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filename)));
                int readedByte;
                while ((readedByte = bis.read()) != -1) {
                    bos.write(readedByte);
                }
                bis.close();
                bos.close();
            }
            httpclient.close();
        } catch (IOException ex) {
            Logger.getLogger(DownloadFileTest.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}