I am writing a simple Java code that simply downloads a file from remote server. I am not using much Java classes. I am creating a simple java.net.Socket
and writing raw HTTP
code on the output stream as follows:
Downloader.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Downloader {
Socket socket;
InputStream istream;
Scanner scanner;
OutputStream ostream;
PrintWriter writer;
String request;
public Downloader() {
socket = null;
istream = null;
scanner = null;
ostream = null;
writer = null;
}
public static void main(String[] args) throws IOException {
Downloader downloader = new Downloader();
downloader.initDownload("cdn.mysql.com", 80, "/Downloads/MySQL-5.6/mysql-5.6.23-osx10.8-x86_64.tar.gz");
downloader.download();
downloader.close();
}
public void initDownload(String host, int port, String file) throws IOException {
// Connecting the socket.
socket = new Socket(host, port);
// Generating the HTTP request
request = "GET "+file+" HTTP/1.1\r\n";
request += "Host: "+host+":"+port+"\r\n";
request += "User-Agent: \r\n";
request += "Connection: Close\r\n";
request += "Range: bytes=0-1000\r\n"; // This line doesn't work properly.
request += "\r\n";
}
public void download() throws IOException {
ostream = socket.getOutputStream();
writer = new PrintWriter(ostream);
writer.println(request);
writer.flush();
read();
}
public void read() throws IOException {
istream = socket.getInputStream();
scanner = new Scanner(istream);
// Print the HTTP response by the server on the terminal.
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
if(line.length() == 0) // break out of the loop when we encounter an empty line. i.e., The blank line between HTTP headers and body.
break;
}
// Print the data in the file "mysql-5.6.23-osx10.8-x86_64.tar.gz"
FileOutputStream fostream = new FileOutputStream("mysql-5.6.23-osx10.8-x86_64.tar.gz", true);
while(scanner.hasNextLine())
fostream.write(scanner.nextLine().getBytes());
fostream.close();
}
public void close() throws IOException {
istream.close();
scanner.close();
ostream.close();
writer.close();
socket.close();
}
}
The code is quiet clear I suppose. I am trying to read a file from the server using plain sockets. The code works fine. The response header from the server is output on the terminal window, while the data is output in the file. But the Range
header doesn't give proper output.
When I run the code, this is the HTTP server response
printed:
HTTP/1.1 206 Partial Content
Server: Apache
Accept-Ranges: bytes
Last-Modified: Tue, 20 Jan 2015 06:15:42 GMT
ETag: "5f989776b8f8766c7f43c27c1a09c032:1422821798"
Date: Tue, 03 Mar 2015 13:35:54 GMT
Content-Range: bytes 0-1000/176674931 // Looke here. The output is fine here.
Content-Length: 1001
Connection: close
Content-Type: application/x-tar-gz
While the data goes in the file mysql-5.6.23-osx10.8-x86_64.tar.gz
.
But when I right click the file mysql-5.6.23-osx10.8-x86_64.tar.gz
to check it's size, it's size is 1,835 bytes
instead of 1,001 bytes
.
Why is this happening? What should I do to overcome it? Please help.
HTTP
. Thanks in advance. Refer- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html . It suggests that it is possible for the server to ignore range requests which is what is happening in your case I suppose.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.