- From: <kabir@ciaoweb.it>
- Date: Tue, 28 Nov 2000 10:51:08 +0100
- To: <www-lib@w3.org>
I'm Mauro Landro, working for Progesi
In the file below there is a java class for Put method with http protocol.
unfortunately I don't know where to find and how to compile or use the following packages:
www.w3c.mime.* , www.w3c.http.* ,
www.w3c.protocol.http.*.
can anybody help me?
thank you very much, best reguards
// upload.java
import java.io.*;
import java.net.*;
import w3c.www.mime.*;
import w3c.www.http.*;
import w3c.www.protocol.http.*;
/**
* Upload a single file, using HTTP.
*/
public class upload {
protected static boolean debug = false;
/**
* Copy the given source of given type, to given location.
* @param type The MIME type of the source.
* @param src The source to copy.
* @param dst The destination URL.
*/
protected static boolean copy(MimeType type, File src, URL dst)
throws IOException, HttpException
{
HttpManager manager = HttpManager.getManager();
// Get a handle to the source stream:
InputStream in = new BufferedInputStream(new FileInputStream(src));
// Prepare an HTTP request:
Request request = manager.createRequest();
request.setMethod("PUT");
request.setURL(dst);
request.setContentLength((int) src.length());
request.setContentType(type);
request.setOutputStream(in);
if ( debug )
request.dump(System.out);
// Run the request:
Reply reply = manager.runRequest(request);
if ( debug )
reply.dump(System.out);
in.close();
// Check the reply status:
return (reply.getStatus() / 200 == 2);
}
public static void usage() {
System.out.println("upload [-t <mime-type>] [-d] <src> <dst>");
System.out.println("src: URL of source.");
System.out.println("dst: URL of destination.");
System.exit(1);
}
public static void main(String args[]) {
File src = null;
URL dst = null;
MimeType type = MimeType.TEXT_HTML;
// Check args:
for (int i = 0 ; i < args.length ; i++) {
if ( args[i].equals("-t") && (i+1 < args.length)) {
try {
type = new MimeType(args[++i]);
} catch (Exception ex) {
System.out.println("Invalid mime type: "+args[i]);
}
} else if ( args[i].equals("-d") ) {
debug = true;
} else if ( i + 2 == args.length ) {
// Get the source:
src = new File(args[i]);
if ( ! src.exists() ) {
System.out.println("Couldn't find "+args[i]);
System.exit(1);
}
// Get the destination:
try {
dst = new URL(args[++i]);
} catch (Exception ex) {
System.out.println(args[i]+": invalid URL.");
}
} else {
usage();
}
}
if ((src == null) || (dst == null))
usage();
// Perform the task:
try {
if ( copy(type, src, dst) )
System.out.println("Copy failed.");
else
System.out.println("Copy done.");
} catch (Exception ex) {
ex.printStackTrace();
}
System.exit(0);
}
}
_________________________________________________________________________
Ciaoweb ha una veste completamente nuova, scoprila su www.ciaoweb.it
Received on Tuesday, 28 November 2000 04:53:42 UTC