Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import android.os.Environment;

import android.util.Log;

public class Main {
    /**
     * Tag used on log messages.
     */
    static final String TAG = "ODMCommonUtilities";
    static String gDEBUG = "";

    public static void DownloadFile(String u) {
        try {
            Logd(TAG, "Starting download of: " + u);
            URL url = new URL(u);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();
            //File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            checkStorageDir();
            File storageDir = new File(
                    Environment.getExternalStorageDirectory() + "/Android/data/com.nowsci.odm/.storage");
            File file = new File(storageDir, getFileName(u));
            Logd(TAG, "Storage directory: " + storageDir.toString());
            Logd(TAG, "File name: " + file.toString());
            FileOutputStream fileOutput = new FileOutputStream(file);
            InputStream inputStream = urlConnection.getInputStream();
            byte[] buffer = new byte[1024];
            int bufferLength = 0;
            while ((bufferLength = inputStream.read(buffer)) > 0) {
                fileOutput.write(buffer, 0, bufferLength);
            }
            fileOutput.close();
            Logd(TAG, "File written");
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void Logd(String inTAG, String message) {
        if (gDEBUG.equals("true"))
            Log.d(inTAG, message);
    }

    public static void checkStorageDir() {
        File storageDir = new File(Environment.getExternalStorageDirectory() + "/Android");
        if (!storageDir.exists())
            storageDir.mkdir();
        storageDir = new File(Environment.getExternalStorageDirectory() + "/Android/data");
        if (!storageDir.exists())
            storageDir.mkdir();
        storageDir = new File(Environment.getExternalStorageDirectory() + "/Android/data/com.nowsci.odm");
        if (!storageDir.exists())
            storageDir.mkdir();
        storageDir = new File(Environment.getExternalStorageDirectory() + "/Android/data/com.nowsci.odm/.storage");
        if (!storageDir.exists())
            storageDir.mkdir();
    }

    public static String getFileName(String extUrl) {
        URL url = null;
        String path = extUrl;
        try {
            url = new URL(extUrl);
            path = url.getPath();
        } catch (MalformedURLException e) {
            path = extUrl;
        }
        String filename = "";
        String[] pathContents = path.split("[\\\\/]");
        if (pathContents != null) {
            int pathContentsLength = pathContents.length;
            System.out.println("Path Contents Length: " + pathContentsLength);
            String lastPart = pathContents[pathContentsLength - 1];
            String[] lastPartContents = lastPart.split("\\.");
            if (lastPartContents != null && lastPartContents.length > 1) {
                int lastPartContentLength = lastPartContents.length;
                String name = "";
                for (int i = 0; i < lastPartContentLength; i++) {
                    if (i < (lastPartContents.length - 1)) {
                        name += lastPartContents[i];
                        if (i < (lastPartContentLength - 2)) {
                            name += ".";
                        }
                    }
                }
                String extension = lastPartContents[lastPartContentLength - 1];
                filename = name + "." + extension;
            }
        }
        return filename;
    }
}