Guest
Title : Untitled
Guest on 20th September 2024 05:07:09 AM
Syntax: MARKDOWN | Views: 9 | Your IP: 3.16.82.194

<?xml version="1.0" encoding="utf-8"?>

.java:

package com.example.project2;

import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button downloadButton = findViewById(R.id.downloadButton);
    downloadButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            downloadFile("http://212.183.159.230/50MB.zip");
        }
    });
}

private void downloadFile(String urlString) {
    try {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            String fileName = getFileNameFromURL(urlString);
            String destinationPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/" + fileName;

            InputStream inputStream = connection.getInputStream();
            OutputStream outputStream = new FileOutputStream(destinationPath);

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.flush();
            outputStream.close();
            inputStream.close();

            Toast.makeText(this, "File downloaded successfully!", Toast.LENGTH_SHORT).show();
        } else {
            Log.e(TAG, "Server returned HTTP error code: " + responseCode);
            Toast.makeText(this, "Failed to download file.", Toast.LENGTH_SHORT).show();
        }

    } catch (Exception e) {
        Log.e(TAG, "Error downloading file: " + e.getMessage());
        Toast.makeText(this, "Failed to download file.", Toast.LENGTH_SHORT).show();
    }
}

private String getFileNameFromURL(String urlString) {
    String[] parts = urlString.split("/");
    return parts[parts.length - 1];
}

}


Edit Paste Earn Money
Raw Paste

What is JustPasteIT.ORG?

JustPasteIT.ORG is a secure and ad-free online platform for sharing code snippets, text, and notes. It allows users to create and share content anonymously by default, using a simple text editor with formatting features. The platform also offers short URLs for easy sharing and the option to save content as text files. It's designed for developers, students, and anyone who needs a quick and private way to share text online without distractions.