import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class ImagesDownloader {
public static void main(String[] args) throws IOException {
getImages("cars", "bikes", "motors");
}
private static void getImages(String... folders) throws IOException {
String imagesRootPath = "http://www.mysite.com/images";
for (String folder : folders) {
for (int i = 1; i < 31; i++) {
URL url = new URL(String.format("%s/%s/%d.gif", imagesRootPath, folder, i));
InputStream in = url.openStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream(String.format("images/%s/%d.gif", folder, i)));
for (int b; (b = in.read()) != -1; ) {
out.write(b);
}
out.close();
in.close();
System.out.println(String.format("Image %d.gif from folder %s has been successfully downloaded.", i, folder));
}
}
}
}
Refactorings
No refactoring yet !
Alberto
February 2, 2011, February 02, 2011 05:54, permalink
Brige Download .jpeg
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class ImagesDownloader {
public static void main(String[] args) throws IOException {
getImages("cars", "bikes", "motors");
}
private static void getImages(String... folders) throws IOException {
String imagesRootPath = "http://listsoplenty.com/pix/archives/5177/popsicle-stick-bridges";
for (String folder : folders) {
for (int i = 1; i < 31; i++) {
URL url = new URL(String.format("%s/%s/%d.gif", imagesRootPath, folder, i));
InputStream in = url.openStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream(String.format("images/%s/%d.gif", folder, i)));
for (int b; (b = in.read()) != -1; ) {
out.write(b);
}
out.close();
in.close();
System.out.println(String.format("Image %d.gif from folder %s has been successfully downloaded.", i, folder));
}
}
}
}
keenjee nama
April 13, 2011, April 13, 2011 15:17, permalink
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
public class ImagesDownloader {
public static void main(String[] args) throws IOException {
getImages("cars", "bikes", "motors");
}
private static void getImages(String... folders) throws IOException {
String imagesRootPath = "http://spaghettioh.com/wp-content/review_mirrors_edge/mirrors_edge_rooftop.jpg";
for (String folder : folders) {
for (int i = 1; i < 31; i++) {
URL url = new URL(String.format("%s/%s/%d.gif", imagesRootPath, folder, i));
InputStream in = url.openStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream(String.format("images/%s/%d.gif", folder, i)));
for (int b; (b = in.read()) != -1; ) {
out.write(b);
}
out.close();
in.close();
System.out.println(String.format("Image %d.gif from folder %s has been successfully downloaded.", i, folder));
}
}
}
}
raj
January 7, 2012, January 07, 2012 21:51, permalink
i want the refactoring code for this
import java.util.List;
import java.io.*;
import java.io.IOException;
// (1) Apply some Object oriented and refactoring principles to make it simple. Cleaner within the problem context
// (2) Rename class and method names where applicable
// (3) Ignore the compilation errors
// (4) Assume PayRollProcessor & GovernMentAgency are available within the code don't build the logic
// to get those classes
//This class responsibility is to process the employee compensation.
//Global variables
public class Employee
{
int getId() {return id;}
double getSalary() {return salary;}
int getBonus() {return bonus;}
int getOrigin() {return origin;}
public boolean isSeniorManagement()
{
return "S".equals(type);
}
}
/**
* Description:
* <p/>
* User: Ajmal
* Date: Mar 4, 2010
*/
public class GovernmentAgency {
public void report(int id, String value) {
// :-) Magically it gets reported to gov. , Don't implement
}
}
/**
* Description:
* <p/>
* User: Ajmal
* Date: Mar 4, 2010
*/
public class PayRollProcessor
{
public void processSalary(double salary, String s, int id)
{
// :-) Magically it gets processed. Don't implement
}
public void processTax(int bonus, int tax, String s, int id)
{
// :-) Magically it gets processed. Don't implement
}
}
public class Processor extends PayRollProcessor,GovernmentAgency,Employee
{
// public PayRollProcessor payRollProcessor;
// public GovernmentAgency governMentAgency;
/**
* Purpose of the method is to process the compensation for the employees and also report the
* information to Government agencies.
*/
public void processData(List<Employee> employees)
{
for (Employee emp : employees)
{
if (emp.isSeniorManagement())
{
salary = emp.getSalary();
id = emp.getId();
PayRollProcessor.processSalary(salary, "Senior", emp.getId());
bonus = emp.getBonus();
tax = 25;
if (bonus > 1000 && bonus < 10000)
{
tax = 30;
}
else if (bonus > 10000)
{
tax = 40;
double salary1 =salary(); //emp.getSalary();
if (salary1 > 20000)
{
tax = 50;
}
}
payRollProcessor.processTax(bonus,tax,"Senior",emp.getId());
value = "D";
if (emp.getOrigin().equals("Asia"))
{
value = "A";
}
else if (emp.getOrigin().equals("North America"))
{
value = "N";
}
else if (emp.getOrigin().equals("European"))
{
value = "E";
}
governMentAgency.report(emp.getId(), value);
}
else {
salary = emp.getSalary();
id = emp.getId();
if (salary < 15000)
{
salary = 15000;
}
payRollProcessor.processSalary(salary, "Normal", emp.getId());
bonus = emp.getBonus();
tax = 25;
if (bonus > 1000 && bonus < 5000)
{
tax = 26;
}
else if (bonus > 5000)
{
tax = 27;
}
payRollProcessor.processTax(bonus, tax, "Normal", emp.getId());
String value = "D";
if (emp.getOrigin().equals("Asia"))
{
value = "A";
}
else if (emp.getOrigin().equals("North America"))
{
value = "N";
} else if (emp.getOrigin().equals("European"))
{
value = "E";
}
governMentAgency.report(emp.getId(),value);
}
}
}
}
raj
January 7, 2012, January 07, 2012 21:52, permalink
i want the refactoring code for this
import java.util.List;
import java.io.*;
import java.io.IOException;
// (1) Apply some Object oriented and refactoring principles to make it simple. Cleaner within the problem context
// (2) Rename class and method names where applicable
// (3) Ignore the compilation errors
// (4) Assume PayRollProcessor & GovernMentAgency are available within the code don't build the logic
// to get those classes
//This class responsibility is to process the employee compensation.
//Global variables
public class Employee
{
int getId() {return id;}
double getSalary() {return salary;}
int getBonus() {return bonus;}
int getOrigin() {return origin;}
public boolean isSeniorManagement()
{
return "S".equals(type);
}
}
/**
* Description:
* <p/>
* User: Ajmal
* Date: Mar 4, 2010
*/
public class GovernmentAgency {
public void report(int id, String value) {
// :-) Magically it gets reported to gov. , Don't implement
}
}
/**
* Description:
* <p/>
* User: Ajmal
* Date: Mar 4, 2010
*/
public class PayRollProcessor
{
public void processSalary(double salary, String s, int id)
{
// :-) Magically it gets processed. Don't implement
}
public void processTax(int bonus, int tax, String s, int id)
{
// :-) Magically it gets processed. Don't implement
}
}
public class Processor extends PayRollProcessor,GovernmentAgency,Employee
{
// public PayRollProcessor payRollProcessor;
// public GovernmentAgency governMentAgency;
/**
* Purpose of the method is to process the compensation for the employees and also report the
* information to Government agencies.
*/
public void processData(List<Employee> employees)
{
for (Employee emp : employees)
{
if (emp.isSeniorManagement())
{
salary = emp.getSalary();
id = emp.getId();
PayRollProcessor.processSalary(salary, "Senior", emp.getId());
bonus = emp.getBonus();
tax = 25;
if (bonus > 1000 && bonus < 10000)
{
tax = 30;
}
else if (bonus > 10000)
{
tax = 40;
double salary1 =salary(); //emp.getSalary();
if (salary1 > 20000)
{
tax = 50;
}
}
payRollProcessor.processTax(bonus,tax,"Senior",emp.getId());
value = "D";
if (emp.getOrigin().equals("Asia"))
{
value = "A";
}
else if (emp.getOrigin().equals("North America"))
{
value = "N";
}
else if (emp.getOrigin().equals("European"))
{
value = "E";
}
governMentAgency.report(emp.getId(), value);
}
else {
salary = emp.getSalary();
id = emp.getId();
if (salary < 15000)
{
salary = 15000;
}
payRollProcessor.processSalary(salary, "Normal", emp.getId());
bonus = emp.getBonus();
tax = 25;
if (bonus > 1000 && bonus < 5000)
{
tax = 26;
}
else if (bonus > 5000)
{
tax = 27;
}
payRollProcessor.processTax(bonus, tax, "Normal", emp.getId());
String value = "D";
if (emp.getOrigin().equals("Asia"))
{
value = "A";
}
else if (emp.getOrigin().equals("North America"))
{
value = "N";
} else if (emp.getOrigin().equals("European"))
{
value = "E";
}
governMentAgency.report(emp.getId(),value);
}
}
}
}
Hi guys,
This code downloads some images from the given URL to the hard drive.
In my case I know the URL of the images and the number images of each folder :).