Results 1 to 4 of 4
  1. #1
    contest man is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Expansion on download url question

    I want to add an ebook to my website that visitors can download. Want to know how to set up visitor download capability on my website. Hopefully this will clarify my question. Thanks for the rapid response. Sears

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you just need to hotlink the file on your server, the users click it and their web browser will ask them if they wish to download it.

    btw this is a java forum and your question has nothing to do with java.

  3. #3
    contest man is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default What do I do with this info to create a download url

    I joined this forum to learn how to get the most out of my java experience.

    Now, I am a raw newbie wanting to learn.

    I did not understand anything you said in your email, which I thank you for. Would it make a difference if I told you I am 66 years of age and starting a new adventure in my life on the internet? I should have been more specific in the beginning.

    Thanks Sears

    Download.java
    import java.io.*;
    import java.net.*;
    import java.util.*;

    // This class downloads a file from a URL.
    class Download extends Observable implements Runnable {

    // Max size of download buffer.
    private static final int MAX_BUFFER_SIZE = 1024;

    // These are the status names.
    public static final String STATUSES[] = {"Downloading",
    "Paused", "Complete", "Cancelled", "Error"};

    // These are the status codes.
    public static final int DOWNLOADING = 0;
    public static final int PAUSED = 1;
    public static final int COMPLETE = 2;
    public static final int CANCELLED = 3;
    public static final int ERROR = 4;

    private URL url; // download URL
    private int size; // size of download in bytes
    private int downloaded; // number of bytes downloaded
    private int status; // current status of download

    // Constructor for Download.
    public Download(URL url) {
    this.url = url;
    size = -1;
    downloaded = 0;
    status = DOWNLOADING;

    // Begin the download.
    download();
    }

    // Get this download's URL.
    public String getUrl() {
    return url.toString();
    }

    // Get this download's size.
    public int getSize() {
    return size;
    }

    // Get this download's progress.
    public float getProgress() {
    return ((float) downloaded / size) * 100;
    }

    // Get this download's status.
    public int getStatus() {
    return status;
    }

    // Pause this download.
    public void pause() {
    status = PAUSED;
    stateChanged();
    }

    // Resume this download.
    public void resume() {
    status = DOWNLOADING;
    stateChanged();
    download();
    }

    // Cancel this download.
    public void cancel() {
    status = CANCELLED;
    stateChanged();
    }

    // Mark this download as having an error.
    private void error() {
    status = ERROR;
    stateChanged();
    }

    // Start or resume downloading.
    private void download() {
    Thread thread = new Thread(this);
    thread.start();
    }

    // Get file name portion of URL.
    private String getFileName(URL url) {
    String fileName = url.getFile();
    return fileName.substring(fileName.lastIndexOf('/') + 1);
    }

    // Download file.
    public void run() {
    RandomAccessFile file = null;
    InputStream stream = null;

    try {
    // Open connection to URL.
    HttpURLConnection connection =
    (HttpURLConnection) url.openConnection();

    // Specify what portion of file to download.
    connection.setRequestProperty("Range",
    "bytes=" + downloaded + "-");

    // Connect to server.
    connection.connect();

    // Make sure response code is in the 200 range.
    if (connection.getResponseCode() / 100 != 2) {
    error();
    }

    // Check for valid content length.
    int contentLength = connection.getContentLength();
    if (contentLength < 1) {
    error();
    }

    /* Set the size for this download if it
    hasn't been already set. */
    if (size == -1) {
    size = contentLength;
    stateChanged();
    }

    // Open file and seek to the end of it.
    file = new RandomAccessFile(getFileName(url), "rw");
    file.seek(downloaded);

    stream = connection.getInputStream();
    while (status == DOWNLOADING) {
    /* Size buffer according to how much of the
    file is left to download. */
    byte buffer[];
    if (size - downloaded > MAX_BUFFER_SIZE) {
    buffer = new byte[MAX_BUFFER_SIZE];
    } else {
    buffer = new byte[size - downloaded];
    }

    // Read from server into buffer.
    int read = stream.read(buffer);
    if (read == -1)
    break;

    // Write buffer to file.
    file.write(buffer, 0, read);
    downloaded += read;
    stateChanged();
    }

    /* Change status to complete if this point was
    reached because downloading has finished. */
    if (status == DOWNLOADING) {
    status = COMPLETE;
    stateChanged();
    }
    } catch (Exception e) {
    error();
    } finally {
    // Close file.
    if (file != null) {
    try {
    file.close();
    } catch (Exception e) {}
    }

    // Close connection to server.
    if (stream != null) {
    try {
    stream.close();
    } catch (Exception e) {}
    }
    }
    }

    // Notify observers that this download's status has changed.
    private void stateChanged() {
    setChanged();
    notifyObservers();
    }

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    What is the purpose of having the java program handle your downloads for you? and What email? I never sent you an email...maybe you subscribed to posts

Similar Threads

  1. JTree Programmatic Node Expansion and Selection Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 01-16-2013, 07:23 AM
  2. How to load a directory in JTree with Children(On expansion)
    By aneesahamedaa in forum AWT / Swing
    Replies: 0
    Last Post: 10-13-2008, 01:23 PM
  3. bizarre auto expansion
    By fishtoprecords in forum Suggestions & Feedback
    Replies: 2
    Last Post: 07-31-2008, 10:52 PM
  4. Replies: 0
    Last Post: 07-25-2007, 09:04 AM
  5. I could download JDK 1.5
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-13-2007, 03:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •