-
Java errors
package WebEft;
import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.*;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCred entials;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.MultipartPos tMethod;
public BasicDownloadExample() {
super(); }
public static void main(String[] args) {
// Check for Arguments - Less than 3 arguments?
if ( args.length < 3 ) {
System.out.println( "Usage: java BasicDownloadExample URL username password"
);
System.out.println( "https://fsgtest6.finra.org/ BD# BD's-password");
System.out.println( "https://fsgtest6.finra.org/archive BD# BD's-password");
System.out.println( "https://fsgtest6.finra.org/archive/filename BD# BD's-password");
System.out.println( "https://fsgtest6.finra.org/processed BD# BD's-password");
System.out.println( "https://fsgtest6.finra.org/reports BD# BD's-password");
System.out.println( "Eg: java BasicDownloadExample " +
https://fsgtest6.finra.org/reports/filename.zip BD# BD's-password filename.zip");
System.exit( 0 );
}
URL aURL = new URL(args[0]);
String username = args[1];
String password = args[2];
String fileName;
// If user specified 4 argument for given filename
if (args.length == 4) {
fileName = args[3];
} else
fileName = "";
String file;
int STInterface = 1; // To turn of STInterface, set to 0 which will be slower because of html tags being downloaded also.
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
}
System.out.println("path = " + aURL.getPath());
System.out.println("filename = " + aURL.getFile());
// Get basename of the URL
String bn = aURL.getFile(); int index = bn.lastIndexOf("/"); bn =
bn.substring(index + 1);
System.out.println("basename = " + bn);
// Convert URL to String String url = (String)aURL.toString();
HttpClient client = new HttpClient();
// pass our credentials to HttpClient, they will only be used for
// authenticating to servers with realm "FileDriveWWW" on the host
// "aURL.getHost()", to authenticate against
// an arbitrary realm or host change the appropriate argument to null.
client.getState().setCredentials(
new AuthScope(aURL.getHost(), 443, "FileDriveWWW"),
new UsernamePasswordCredentials(username, password)
);
// create a GET method that reads a file over HTTPS, we're assuming
// that this file requires basic authentication using the realm above.
GetMethod get = new GetMethod(url);
try {
// execute the GET
int status = client.executeMethod( get );
// print the status and response //
System.out.println(status + "\n" + get.getStatusCode() + "\n"+ url);
// Get the name of file in URL or set the name for the directory listing of URL
if (fileName.length() != 0) {
file = fileName;
// Set to fileName which user specified
} else {
// Get daytime stamp to tag to file for directory listing to save to disk
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyyMMdd-HHmmss";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
// Set filename for saving the directory listing of root directory of URL to disk
if (bn == "" || bn.length() == 0) { file = "c:\\temp\\downloadfile-" +sdf.format(cal.getTime())+".log";
} else { // Set filename for saving the directory listing of subdirectories of URL
if (getExtension(bn) == "") {
file = "c:\\temp\\" + bn + sdf.format(cal.getTime())+ ".log";
} else { // Set filename for saving the file specified in URL to disk
file = "c:\\temp\\" + bn;
}
}
}
// Initialize file to be save to disk
OutputStream out = new FileOutputStream(file);
if (get.getStatusCode() == HttpStatus.SC_OK) { // if StatusCode == 200
// Specify the USER-AGENT to make the download more efficient
if (STInterface == 1) {
get.setRequestHeader("USER-AGENT", "SecureTransport");
}
// Getting the URL
client.executeMethod( get );
// Saving the URL response to Stream Object
InputStream in = get.getResponseBodyAsStream();
byte[] b = new byte[1024];
int len;
while ((len = in.read(b)) != -1) {
//write bytes to file on disk
out.write(b, 0, len);
}
System.out.println(url + " downloaded to " + file);
in.close();
} else { // if StatusCode is not 200
System.out.println("Return Code: " + get.getStatusLine().toString());
}
} finally {
// release any connection resources used by the method
GetMethod logout = new
GetMethod(aURL.getProtocol()+"://"+aURL.getHost()+"/?logout");
client.executeMethod( logout );
get.releaseConnection();
}
}
// Utility to determine if the basename of the URL is a directory or a filename with extension
public static String getExtension(final String filename) {
String suffix = "";
String shortFilename = filename;
int lastDirSeparator = filename.lastIndexOf(File.separatorChar);
if(lastDirSeparator > 0){
shortFilename = filename.substring(lastDirSeparator + 1);
}
int index = shortFilename.lastIndexOf('.');
if (index > 0 && index < shortFilename.length() - 1) {
suffix = shortFilename.substring(index + 1);
}
return suffix;
}
}
I have the above code. I am receiving a great deal of compliler errors for example on the first section
public BasicDownloadExample() {
super(); }
I receive the following Multiple markers at this line
- Syntax error on token ")", delete this token
- Syntax error on token "public", @ expected after
this token
this type of error is throughout the code, but thought I would start with this. I am using Eclipse Version: 3.5.1. I am extremely new to java. What might I be missing? Thank you!!
-
Weclome to the forum.next time use Code tags.and try having a better indentation to expect answers fast.
Code:
package WebEft;
import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.*;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCred entials;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.MultipartPos tMethod;
public BasicDownloadExample() {
super(); }
public static void main(String[] args) {
// Check for Arguments - Less than 3 arguments?
if ( args.length < 3 ) {
System.out.println( "Usage: java BasicDownloadExample URL username password"
);
System.out.println( "https://fsgtest6.finra.org/ BD# BD's-password");
System.out.println( "https://fsgtest6.finra.org/archive BD# BD's-password");
System.out.println( "https://fsgtest6.finra.org/archive/filename BD# BD's-password");
System.out.println( "https://fsgtest6.finra.org/processed BD# BD's-password");
System.out.println( "https://fsgtest6.finra.org/reports BD# BD's-password");
System.out.println( "Eg: java BasicDownloadExample " +
https://fsgtest6.finra.org/reports/filename.zip BD# BD's-password filename.zip");
System.exit( 0 );
}
URL aURL = new URL(args[0]);
String username = args[1];
String password = args[2];
String fileName;
// If user specified 4 argument for given filename
if (args.length == 4) {
fileName = args[3];
} else
fileName = "";
String file;
int STInterface = 1; // To turn of STInterface, set to 0 which will be slower because of html tags being downloaded also.
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
}
System.out.println("path = " + aURL.getPath());
System.out.println("filename = " + aURL.getFile());
// Get basename of the URL
String bn = aURL.getFile(); int index = bn.lastIndexOf("/"); bn =
bn.substring(index + 1);
System.out.println("basename = " + bn);
// Convert URL to String String url = (String)aURL.toString();
HttpClient client = new HttpClient();
// pass our credentials to HttpClient, they will only be used for
// authenticating to servers with realm "FileDriveWWW" on the host
// "aURL.getHost()", to authenticate against
// an arbitrary realm or host change the appropriate argument to null.
client.getState().setCredentials(
new AuthScope(aURL.getHost(), 443, "FileDriveWWW"),
new UsernamePasswordCredentials(username, password)
);
// create a GET method that reads a file over HTTPS, we're assuming
// that this file requires basic authentication using the realm above.
GetMethod get = new GetMethod(url);
try {
// execute the GET
int status = client.executeMethod( get );
// print the status and response //
System.out.println(status + "\n" + get.getStatusCode() + "\n"+ url);
// Get the name of file in URL or set the name for the directory listing of URL
if (fileName.length() != 0) {
file = fileName;
// Set to fileName which user specified
} else {
// Get daytime stamp to tag to file for directory listing to save to disk
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyyMMdd-HHmmss";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
// Set filename for saving the directory listing of root directory of URL to disk
if (bn == "" || bn.length() == 0) { file = "c:\\temp\\downloadfile-" +sdf.format(cal.getTime())+".log";
} else { // Set filename for saving the directory listing of subdirectories of URL
if (getExtension(bn) == "") {
file = "c:\\temp\\" + bn + sdf.format(cal.getTime())+ ".log";
} else { // Set filename for saving the file specified in URL to disk
file = "c:\\temp\\" + bn;
}
}
}
// Initialize file to be save to disk
OutputStream out = new FileOutputStream(file);
if (get.getStatusCode() == HttpStatus.SC_OK) { // if StatusCode == 200
// Specify the USER-AGENT to make the download more efficient
if (STInterface == 1) {
get.setRequestHeader("USER-AGENT", "SecureTransport");
}
// Getting the URL
client.executeMethod( get );
// Saving the URL response to Stream Object
InputStream in = get.getResponseBodyAsStream();
byte[] b = new byte[1024];
int len;
while ((len = in.read(b)) != -1) {
//write bytes to file on disk
out.write(b, 0, len);
}
System.out.println(url + " downloaded to " + file);
in.close();
} else { // if StatusCode is not 200
System.out.println("Return Code: " + get.getStatusLine().toString());
}
} finally {
// release any connection resources used by the method
GetMethod logout = new
GetMethod(aURL.getProtocol()+"://"+aURL.getHost()+"/?logout");
client.executeMethod( logout );
get.releaseConnection();
}
}
// Utility to determine if the basename of the URL is a directory or a filename with extension
public static String getExtension(final String filename) {
String suffix = "";
String shortFilename = filename;
int lastDirSeparator = filename.lastIndexOf(File.separatorChar);
if(lastDirSeparator > 0){
shortFilename = filename.substring(lastDirSeparator + 1);
}
int index = shortFilename.lastIndexOf('.');
if (index > 0 && index < shortFilename.length() - 1) {
suffix = shortFilename.substring(index + 1);
}
return suffix;
}
}
I have the above code. I am receiving a great deal of compliler errors for example on the first section
Code:
public BasicDownloadExample() {
super(); }
I receive the following Multiple markers at this line
- Syntax error on token ")", delete this token
- Syntax error on token "public", @ expected after
this token
this type of error is throughout the code, but thought I would start with this. I am using Eclipse Version: 3.5.1. I am extremely new to java. What might I be missing? Thank you!!
-
ok btw am just curious where did the class name and heading go?
public class ....{......}
look at static void main look at where its ending brace is thats why your getting compile errors for System.out.println because they're getting declared in thin air.
You can solve alot of errors by having a proper indentation.
Indentation guide
Java Style Guide: Indentation