Sign Up | Log in |

You guys should try programming its awesome. Admin's having fun

import java.util.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.math.*;
import java.nio.*;
import java.net.*;
import java.security.*;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.rmi.*;
import java.time.*;

import java.io.File;
import java.io.IOException;
import java.net.URL;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class Happy109{

public static void main(String[] args) throws IOException {

File f = new File("file1.txt");
Scanner s = new Scanner(f);
for(int i = 39000; i <= 39730; i++){
PrintStream ps = new PrintStream(new File("NewFolder2\\file" +i + ".html"));
method10(ps, "http://mbtibase.com/vchar.php?charid=", ".html");
}

//method0(s, saveDir, "includes/", ".css");
// method0(s, "http://mbtibase.com/", saveDir, "styles/", ".css");
//String fileURL = "http://mbtibase.com/includes/login.php";
;
}

public static void method10(PrintStream ps, String web, String end)throws IOException {

URL url;
InputStream is = null;
BufferedReader br;
String line;

try {
url = new URL(web + end);
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));

while ((line = br.readLine()) != null) {
ps.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}


public static void method0(Scanner s, String web, String saveDir, String dir, String end){

int numCount = 0;
while(s.hasNextLine()){
String suffix = s.nextLine();
String str = web + suffix + end;
// String str2 = web + dir + suffix + end;
method(str, saveDir + "\\");
//method(str2, saveDir + "\\");
}

}

public static void method(String fileURL, String saveDir){
try {
downloadFile(fileURL, saveDir);
} catch (IOException ex) {
ex.printStackTrace();
}
}

private static final int BUFFER_SIZE = 4096;


/**
* Downloads a file from a URL
* @param fileURL HTTP URL of the file to be downloaded
* @param saveDir path of the directory to save the file
* @throws IOException
*/
public static void downloadFile(String fileURL, String saveDir)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();

// always check HTTP response code first
//System.out.println(responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();

if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}

System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);

// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;

// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);

int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

outputStream.close();
inputStream.close();

System.out.println("File downloaded");
} else {
if(responseCode > 404 || responseCode < 404){
System.out.println(fileURL + " No file to download. Server replied HTTP code: " + responseCode);
}
}
httpConn.disconnect();
}
}

Posted by josephty1 on forum Posted on 2017-12-07 07:40:38

Log in to add a comment.

Comments

Sort (descending) by: Date posted | Most voted
  • Posted on: 2017-12-19 00:36:33

    mars

     

    you’re doing a shit job josephty


  • Posted on: 2017-12-18 21:37:32

    Nazmi

     

    code without syntax coloring is ugly


  • Posted on: 2017-12-07 06:50:25

    josephty1

     

    made 1 bug

    fix

    method10(ps, "http://mbtibase.com/vchar.php?charid=", ".html");

    to

    method10(ps, "http://mbtibase.com/vchar.php?charid=" +i, ".html");