Announcement

Collapse
No announcement yet.

Network-Programmierung

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Network-Programmierung

    Hallo Leute!

    ich habe ein folgendes Problem zu lösen:

    ich möchte auf einen Web-Server namens "http://ejb.webserv.de/yourdoc" die
    folgende Datei auf meinen Rechner "command.xml" "get"en.

    Wie kann ich es machen? Bitte auch detaillierte Hinweise geben, da ich mit
    Network-Programmierung unter Java noch nicht vertraut bin.

    MFG
    Tung Dang

  • #2
    Schau' Dir das hier genauer an:

    import java.net.*;
    import java.io.*;
    import java.lang.*;
    import java.util.*;

    public class HTTPGet {

    // Length of buffer for input-stream reading
    public static final int READ_BUFFER_LENGTH = 8292;
    // Default filename for directory list (if any) returned by Web-Server
    public static final String DEFAULT_FILENAME = "index.html";
    // Time at start of connection
    public static long startTime=0;
    // Size of remote file
    public static long fileSize=0;
    // Progress output string
    public static String progress = new String();
    // Size of Progress line output
    public static int outputLength=0;

    public static void main (String args[]) {

    System.out.println("HTTPGet. Example of using the HttpURLConnection-Class.\n");
    if ( args.length == 0 ) {
    System.out.println("Usage: java HTTPGet <url>");
    return;
    }

    try {
    // Get current Time
    startTime = System.currentTimeMillis();
    // Get the URL-String passed on command-line
    String url = new String(args[0]).trim();
    // Create a URL-Object for the URL specified
    URL docURL = new URL(url);
    // Connect to remote-server
    HttpURLConnection remote = (HttpURLConnection)docURL.openConnection();

    // Exit if requested URL does not exist
    int response = remote.getResponseCode();
    if ( response == HttpURLConnection.HTTP_NOT_FOUND ) {
    System.out.println("\nThe requested URL "+url+" does not exist.");
    System.exit(1);
    }
    // Exit if access not allowed
    if ( response == HttpURLConnection.HTTP_FORBIDDEN ||
    response == HttpURLConnection.HTTP_UNAUTHORIZED) {
    System.out.println("\nAccess forbidden: "+url+" is passwort- or read protected.");
    System.exit(1);
    }

    // Get the size of the remote object (file on remote server)
    // Web server returns -1 if directory-list returned
    fileSize=(remote.getContentLength()!=-1)?remote.getContentLength():0;

    // Extract the filename from URL for local storage
    String dstFilename = args[1]; //url.substring(url.lastIndexOf('/')+1);

    // Set default-filename in the case remote-directory was requested
    // The Web-Server returns a HTML-file with directory entries
    if (dstFilename.indexOf(".") == -1)
    dstFilename = DEFAULT_FILENAME;

    // Create local file for output
    File dstFile = new File(dstFilename);
    dstFile.createNewFile();

    // Initialize input- and output streams
    DataInputStream in = new DataInputStream(remote.getInputStream());
    DataOutputStream out = new DataOutputStream(new FileOutputStream(dstFile));

    // Create input-buffer for reading
    byte[] buffer = new byte[READ_BUFFER_LENGTH+1];
    // bytesReadTotal stores the total amount of bytes read
    int bytesReadTotal = 0;
    // bytesRead stores the amount of bytes read on each loop
    int bytesRead = 0;

    // Read input stream and store the data in local file
    // in.read returns -1 when EOF reached
    long tmpTime = System.currentTimeMillis();
    while ((bytesRead=in.read(buffer, 0, READ_BUFFER_LENGTH)) != -1) {
    out.write(buffer, 0, bytesRead);
    bytesReadTotal += bytesRead;
    // Display transfer status every second
    if( ((System.currentTimeMillis()-tmpTime)/1000) >= 1) {
    showProgress(bytesReadTotal);
    tmpTime = System.currentTimeMillis();
    }
    }
    clearProgress();

    // Close the streams
    out.flush();
    out.close();
    in.close();

    // Output request summary
    long time = (System.currentTimeMillis()-startTime)/1000;
    long speed = bytesReadTotal/ (time==0?1:time);
    System.out.println("Local file : " + dstFilename + "\n" +
    "File size : " + bytesReadTotal+ " bytes\n" +

    Comment


    • #3
      Ups! Sieht ziemlich Sch... aus!
      Kopiere es in eine Datei und formatiere die Ausgabe etwas um.
      So wie es hier steht ist es kaum lesbar

      Comment

      Working...
      X