Task Four:
write a java program that reads the file from urls, keep them in array and saves them in to different direcotry under One Root directory with their content type.
some of urls are given below:
1: https://opendata.ecdc.europa.eu/covid19/casedistribution/csv:
2: https://covid19.mohp.gov.np/d9d95102796d3bb61b21b32f615cfb17.png
3: https://www.ntchosting.com/images/jpeg-example.jpg
4: https://file-examples.com/wp-content/uploads/2017/02/file_example_XLSX_10.xlsx
5: http://www.africau.edu/images/default/sample.pdf
some hints:
1. Urls -> Array Collection as String
2. Use Looping for those Array collection
3. For each urls, download function -> Parameter (UrlString)
4. Download function:
-> Root Dir path -> allocate
-> hit url, get contentType. Then check the contentType and detect the file type(pdf,png,jpg,...).
-> Once fileType is detected, allocation it's folder and download the file inside it.
************************code starts here*****************
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class CreatFolder {
public static void downloadSection1(String fileUrl) throws IOException {
//this is the root directory where you have to save the file
final String rootDirectory = "C:\\Users\\tej ghising\\Desktop\\office\\images";
//Calling URl class
URL url = new URL(fileUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//getting server response back
int serverReseponse = con.getResponseCode();
//getting file name using substring mthod in java
String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1, fileUrl.length());
//from here we make the diffrent directory under root directory and save them by s=checking their content type
if (serverReseponse == HttpURLConnection.HTTP_OK) {
if (fileName.contains("pdf")) {
File pdfDir = new File(rootDirectory + "/pdfDir");
if (!pdfDir.exists()) {
pdfDir.mkdir();
}
File file = new File(pdfDir + "/" + fileName);
InputStream inputStream = con.getInputStream();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(inputStream.read());
outputStream.close();
inputStream.close();
} else if (fileName.contains("csv")) {
File txtDir = new File(rootDirectory + "/txtDir");
if (!txtDir.exists()) {
txtDir.mkdir();
}
File file = new File(txtDir + "/" + fileName);
InputStream inputStream = con.getInputStream();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(inputStream.read());
outputStream.close();
inputStream.close();
} else if (fileName.contains("png")) {
File pngDir = new File(rootDirectory + "/pngDir");
if (!pngDir.exists()) {
pngDir.mkdir();
}
File file = new File(pngDir + "/" + fileName);
InputStream inputStream = con.getInputStream();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(inputStream.read());
outputStream.close();
inputStream.close();
} else if (fileName.contains("xlsx")) {
File xlsDir = new File(rootDirectory + "/xlsDir");
if (!xlsDir.exists()) {
xlsDir.mkdir();
}
File file = new File(xlsDir + "/" + fileName);
InputStream inputStream = con.getInputStream();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(inputStream.read());
outputStream.close();
inputStream.close();
} else if (fileName.contains("jpg")) {
File jpgDir = new File(rootDirectory + "/jpgDir");
if (!jpgDir.exists()) {
jpgDir.mkdir();
}
File file = new File(jpgDir + "/" + fileName);
InputStream inputStream = con.getInputStream();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(inputStream.read());
outputStream.close();
inputStream.close();
}else {
System.out.println("jason api "+serverReseponse);
}
}
System.out.println("file is saved sucessfully ");
}
}
**********************main method****************************
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class main {
public static void main(String[] args) throws IOException {
//keep all urls in array
List<String> basurl = new ArrayList<String>();
String text = "https://opendata.ecdc.europa.eu/covid19/casedistribution/csv";
String png = "https://covid19.mohp.gov.np/d9d95102796d3bb61b21b32f615cfb17.png";
String jpg = "https://www.ntchosting.com/images/jpeg-example.jpg";
String xlsx = "https://file-examples.com/wp-content/uploads/2017/02/file_example_XLSX_10.xlsx";
String pdf = "http://www.africau.edu/images/default/sample.pdf";
basurl.add(text);
basurl.add(pdf);
basurl.add(png);
basurl.add(jpg);
basurl.add(xlsx);
//loop through the array
for (int i = 0; i < basurl.size(); i++) {
String fileUrl = basurl.get(i);
CreatFolder.downloadSection1(fileUrl);
}
}
}
1 Comments
Nice
ReplyDelete