W3C home > Mailing lists > Public > www-jigsaw@w3.org > November to December 1996

Re: 'Upload-Applet' for HTTP PUT and persitant storage

From: Anselm Baird_Smith <abaird@www43.inria.fr>
Date: Wed, 20 Nov 1996 14:08:18 +0100 (MET)
Message-Id: <199611201308.OAA09014@www43.inria.fr>
To: "Kai Schmidt" <serkom@schwaben.de>
Cc: www-jigsaw@w3.org
Kai Schmidt writes:
 > On Tue, 19 Nov 1996 17:14:40 +0100 (MET), Anselm Baird_Smith wrote:
 > >You can use PUT rather then obsolete file upload (BTW Its much better)
 > 
 > Could you show me HOW? Some Javacode to actually use the HTTP PUT
 > on the client side without a browser?

Find an 'upload.java' class enclosed below.

 > >
 > > > 2.) What is the best (easiest) way of storing persistant information? I have a postable resource 
 > > > (a poll to collect Yes and No) and would like to display the given numbers of pros and cons.
 > >
 > >I would use a Resource, but I am probably biased. You can alwaays use
 > >files, or properties
 > 
 > My resource-form has a YES and a NO Button (responding to some questions), and I would like to count the users total 
 > opinion and display it back on the dynamic form. 
 > I understand to work with FILES but since Jigsaw has some internal mechanism to store information I would like to use them. 
 > Are there some examples/doku? Sorry if I ask , but Jigsaw is so much more complicated. :=(
 > 
 > With my current www-server (Goserve) I just stored this in a file. HOW can I use 'properties' or 'resources' as persistant and 
 > global variable-store.

Well, you can still store that in a file, no problems...

Anselm.

// 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);
    }

}
Received on Wednesday, 20 November 1996 08:08:28 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 9 April 2012 12:13:25 GMT