Do http download, there must resume, using what technology? Or good control?

Category: Delphi -> VCL/Component Author: z85512376 Date: 2006-04-16 17:49:59
 
z85512376
2006-04-16 17:49:59
Same as flashget download several G is not a problem, and can pause a download, and then start the download.
qddhkj
2006-04-16 17:57:53

This is because the http 1.1 support
ycpoon
2006-04-16 18:08:55
using Java HTTP (HTTP)
content: ;

(a) The principle of HTTP
(2) Java HTTP achieve critical points
(three) breakpoint Broken kernels achieve
About the Author
Zhong Hua ([email protected])
2001 5

(a) The principle
HTTP HTTP is actually very simple principle, that is, on the Http requests and general download was different.
analogy, the browser requests a file on the server when a request issued as follows:
assume that the server domain name wwww.sjtu.edu.cn, file name down.zip.
GET / down.zip HTTP/1.1
Accept: image / gif, image / x-xbitmap, image / jpeg, image / pjpeg, application / vnd.ms-
excel, application / msword, application / vnd.ms-powerpoint, * / *
Accept-Language: zh-cn
Accept- Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Connection: Keep -Alive

server receives the request, as required to find the requested file, extract the file, and then returned to the browser, the return information is as follows:

200
Content-Length = 106786028
Accept-Ranges = bytes
Date = Mon, 30 Apr 2001 12:56:11 GMT
ETag = W / "02ca57e173c11: 95b "
Content-Type = application / octet-stream
Server = Microsoft-IIS/5.0
Last-Modified = Mon, 30 Apr 2001 12:56:11 GMT

called HTTP, that is, from the start where the file has been downloaded to continue downloading. Therefore, the client browser when passed
Web server to be more an information - where to start.
Here is their series a "Browser" to pass the request information to the Web server, requires 2000070 bytes from the beginning.
GET / down.zip HTTP/1.0
User-Agent: NetFox
RANGE: bytes = 2000070 -
Accept: text / html, ; image / gif, image / jpeg, *; q = .2, * / *; q = .2

closer look will find more than line RANGE: bytes = 2000070 -
meaning of this line is to tell the server down.zip 2000070 bytes from the file began to pass, do not pass in front of bytes.
After the server receives the request and returns the following information:
206
Content-Length = 106786028
Content-Range = bytes 2000070-106786027/106786028
Date = Mon, 30 Apr 2001 12:55:20 GMT
ETag = W / "02ca57e173c11: 95b"
Content-Type = application / octet-stream
Server = Microsoft-IIS/5.0
Last-Modified = Mon, 30 Apr 2001 12:55:20 GMT

and compare the information returned by the server front, they would found to increase a line:
Content-Range = bytes 2000070-106786027/106786028
return code was changed to 206, while 200 is no longer a.

know the above principle, it can be programmed HTTP.
aaaeee12345
2006-04-16 18:12:41
(two) Java realize HTTP key points

(1) the method used to achieve Submit RANGE: bytes = 2000070 -. Of course, the most primitive
Socket is certainly accomplished, but that is too cumbersome, in fact, Java's net package provides this functionality. Code is as follows:

URL url = new URL (");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection ();

/ / set the User-Agent
httpConnection.setRequestProperty ("User-Agent", "NetFox");
/ / Set the start position HTTP
httpConnection . setRequestProperty ("RANGE", "bytes = 2000070");
/ / get input stream
InputStream input = httpConnection.getInputStream ();

extracted from the input stream is a stream of bytes beginning down.zip file from the byte stream 2,000,070.
everyone to see, in fact, HTTP in Java is still very simple to implement it.
The next thing to do is how to get the flow to save the file to go.

method used to save the file.
I used IO package RandAccessFile classes.
operation is quite simple, assuming start saving files from 2,000,070, as follows:
RandomAccess oSavedFile = new RandomAccessFile ("down.zip", "rw");
long nPos = 2000070;
/ / locate the file pointer to the position nPos
oSavedFile.seek (nPos);
byte [] b = new byte [1024];
int nRead;
/ / read from the input stream into a byte stream, and then written to a file
while ((nRead = input.read (b, 0, 1024))> 0)
{
oSavedFile.write (b, 0, nRead);
}

how, is also very simple bar.
next step is to integrate into a complete program. Includes a series of threads of control and so on.

pcwj01
2006-04-16 18:15:23
(three) HTTP kernel implementation
spent six major categories, including a test class.
SiteFileFetch.java responsible for the entire document capture, control the internal thread (FileSplitterFetch class).
FileSplitterFetch.java crawl responsible for part of the file.
FileAccess.java responsible for file storage.
SiteInfoBean.java to crawl file, such as a directory to save the file, name, grab the URL of the file and so on.
Utility.java tools, put some simple methods.
TestMethod.java test class.

following is the source:

/ *
** SiteFileFetch.java
* /
package NetFox;
import java.io. *;
import java.net. *;

public class SiteFileFetch extends Thread {

SiteInfoBean siteInfoBean ; = null; / / File Information Bean
long [] nStartPos; / / start position
long [] nEndPos; / / end position
FileSplitterFetch [] fileSplitterFetch; / / sub-thread object
long nFileLength; / / file length
boolean bFirst = true; / / Is this the first time Fetch Document
boolean bStop = false; / / stop flag
File tmpFile; / / file downloads temporary information
DataOutputStream output; / / output to a file output stream

public SiteFileFetch (SiteInfoBean bean) throws IOException
{
siteInfoBean = bean;
/ / tmpFile ; = File.createTempFile ("zhong", "1111", new File (bean.getSFilePath ()));
tmpFile = new File (bean.getSFilePath () + File.separator + bean.getSFileName () + ". info");
if (tmpFile.exists ())
{
bFirst = false;
read_nPos ();
}
else
{
nStartPos = new long [bean.getNSplitter ()];
nEndPos = ; new long [bean.getNSplitter ()];
}

}

public void run ()
{
/ / get file length
/ / split files
/ / instance FileSplitterFetch
/ / start FileSplitterFetch thread
/ / wait for child thread returns
try {
if (bFirst)
{
nFileLength = getFileSize ();
if (nFileLength == -1)
{
System.err. println ("File Length is not known!");
}
else if (nFileLength == -2)
{
System . err.println ("File is not access!");
}
else
{
for (int i = 0; i <nStartPos. length; i + +)
{
nStartPos [i] = (long) (i * (nFileLength / nStartPos.length));
}
for (int i = 0; i <nEndPos.length-1; i + +)
{
nEndPos [i] = nStartPos [i +1];
}
nEndPos [ nEndPos.length-1] = nFileLength;
}
}
staryuhan
2006-04-16 18:28:56
java principle is very clear, thank you! But the key is how to achieve delphi again?
haolingling116
2006-04-16 18:45:22
Yes, ah, I also use the Internet to write a program to exchange data, but the lack of HTTP controls help you UP! ! !
chaojiji
2006-04-16 18:53:51
do not use J builder. With delphi call?
wasmqq
2006-04-16 19:11:03
with ICS controls,