Skip to main content

Sockets: http, get index file

1 reply [Last post]
chopficaro
Offline
Joined: 2010-05-01
Points: 0

i need to get the index file from a web address that's just a directory, i tried doing this directly with

import java.net.URL;
import java.net.URLConnection;
import java.io.FileOutputStream;

but they need the file name, they cant get the index from just the directory. i could loop through all the possible names like index.htm, index.html, index.php, i think there's index.asp, and there's probably more, but that just seems slow and sloppy to me. however, you can do this with sockets by only specifying the directory right?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chopficaro
Offline
Joined: 2010-05-01
Points: 0

i got it, a am a little curious as to why the buffer fills with different ammounts for the same file though
[code]import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;

public class SourceViewer3
{
public static void main (String[] args) throws Exception
{
for (int i = 0; i < args.length; i++)
{
try
{
URL u = new URL(args[i]);
HttpURLConnection uc = (HttpURLConnection)u.openConnection( );
int code = uc.getResponseCode( );
String response = uc.getResponseMessage( );
System.out.println("HTTP/1.x " + code + " " + response);
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
if (contentLength == -1)
{
throw new IOException("Error: No File Found.");
}

for (int j = 1; ; j++)
{
String header = uc.getHeaderField(j);
String key = uc.getHeaderFieldKey(j);
if (header == null || key == null) break;
System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
}
InputStream in = new BufferedInputStream(uc.getInputStream( ));

byte[] data = new byte[contentLength];
int bytesRead = 0;
int offset = 0;
while (offset < contentLength)
{
bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1)
{
break;
}
offset += bytesRead;
System.out.println("bytes read ="+ bytesRead +"\n");
}
in.close();

if (offset != contentLength)
{
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
}
String dirText = "text";
String dirTarget = "target";
String filename = u.getFile().substring(u.getFile().lastIndexOf('/') + 1);
String dirAndFile;
String dirNoSlashes = "run_from_main";
// determine weather the file is a target or is to be parsed for more files
if(contentType.startsWith("text/"))
{
dirAndFile = dirNoSlashes + "\\" + dirText + "\\" + filename;
}
else
{
dirAndFile = dirNoSlashes + "\\" + dirTarget + "\\" + filename;
}
FileOutputStream out = new FileOutputStream(dirAndFile);
out.write(data);
out.flush();
out.close();

}
catch (MalformedURLException ex)
{
System.err.println(args[0] + " is not a parseable URL");
}
catch (IOException ex)
{
System.err.println(ex);
}
}
}
}[/code]