In: Computer Science
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files from the Internet. This program is very similar to wget utility in Unix/Linux environment.The synopsis of SimpleWebGet is: java SimpleWebGet URL. The URL could be either a valid link on the Internet, e.g., www.asu.edu/index.html, or gaia.cs.umass.edu/wireshark-labs/alice.txt or an invalid link, e.g., www.asu.edu/inde.html. ww.asu.edu/inde.html. The output of SimpleWebGet for valid links should be the same as wget utility in Linux, except the progress line highlighted within the red rectangle: 100% [============================>] 152,138 248.78K/s.The outputs of SimpleWebGet for not-found hosts or non-found files should be the same as wget utility in Linux. Dynamics progress bar and percentages are shown.The program could successfully download a HTML file or other Web objects.The program could report the Resolving line.The program could report the Connecting line.The program could report the HTTP request and response line.The program could report the Length line.The program could report the accurate data transfer rate.The program could handle the host/servname not known error. The program could handle the file not found error.
CODE:
/The code with comments
package Nov20;
import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;
public class JavaWGetCommand
{
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the URL to be downloaded like WGET of
Unix: ");
String inputUrl = sc.next();
InputStream inputStream = null;
try {
//input stream of the URL link
inputStream = new URL(inputUrl).openStream();
//copy the content of URL file to your local pc file.
Files.copy(inputStream, Paths.get("C:\\Users\\{your username here
please}\\myFile.txt"), StandardCopyOption.REPLACE_EXISTING);
}catch (MalformedURLException mfe){ //exception handling similar to
Wget command.
System.out.println("The URL is not valid");
mfe.printStackTrace();
}catch (IOException ioe){
ioe.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
if(inputStream!=null)
inputStream.close();
}
}
}