使用Java通过http下载文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Java通过http下载文件相关的知识,希望对你有一定的参考价值。
This thread downloads file via url.
package core.spider; import java.io.*; import java.net.*; import java.util.*; // This class downloads a file from a URL. // Max size of download buffer. private static final int MAX_BUFFER_SIZE = 1024; // These are the status names. "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 int size; // size of download in bytes private int downloaded; // number of bytes downloaded private int status; // current status of download // Constructor for Download. this.url = url; size = -1; downloaded = 0; status = DOWNLOADING; // Begin the download. download(); } // Get this download's URL. 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.start(); } // Get file name portion of URL. return fileName.substring(fileName.lastIndexOf('/') + 1); } // Download file. public void run() { try { // Open connection to URL. HttpURLConnection connection = // 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.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(); } error(); } finally { // Close file. if (file != null) { try { file.close(); } // Close connection to server. if (stream != null) { try { stream.close(); } } } // Notify observers that this download's status has changed. private void stateChanged() { setChanged(); notifyObservers(); } try { Download d = new Download(new URL("http://hiphotos.baidu.com/happy_51qq/pic/item/1fec21c589a7108c8326ac9b.jpg")); d.run(); // TODO Auto-generated catch block e.printStackTrace(); } } }
以上是关于使用Java通过http下载文件的主要内容,如果未能解决你的问题,请参考以下文章