On Wednesday 19 February 2003 03:27 pm, Henk Hangyi wrote:
> Hi,
>
> Before i start writing it myself ... does anybody have a piece of Java /
> JSP code by which images / attachments can be uploaded to MMBase in
> batch mode?

requires rmmci to be "on"

import org.mmbase.bridge.*;

import java.util.*;
import java.io.*;

public class ImportImages{
    public static int count = 0;
    public static long startTime = System.currentTimeMillis();
    
    public static void main(String[] argv) throws Exception{
        CloudContext cloudContext= ContextProvider.getCloudContext("rmi://127.0.0.1:1111/remotecontext");
        HashMap user = new HashMap();
        user.put("username", "admin");
        user.put("password", "admin2k");
        Cloud cloud = cloudContext.getCloud("mmbase","name/password",user);
        NodeManager nodeManager = cloud.getNodeManager("images");
        new ImportImages(new File("/home/keesj/out"),cloud);
    }
    
    public ImportImages(File file , Cloud cloud) throws IOException{
        fetch(file,cloud);
        System.err.println("import time " + ((System.currentTimeMillis() - startTime) / 1000));
    }
    
    public void fetch(File file,Cloud cloud) throws IOException{
        if (file.isDirectory()){
            File [] files = file.listFiles();
            for (int x =0 ; x < files.length; x++){
                fetch(files[x], cloud);
            }
        } else {
            if (file.getName().endsWith(".jpg")){
                count ++;
                System.err.println(count + ":import " + file.getPath() );
                NodeManager nm = cloud.getNodeManager("images");
                Node node = nm.createNode();
                node.setStringValue("title", file.getPath());
                node.setByteValue("handle",getFileData(file));
                node.commit();
                
            }
        }
    }
    
    public byte[] getFileData(File file) throws IOException {
        InputStream in = new FileInputStream(file);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[200];
        int length;
        while( (length = in.read(buffer)) > 0){
            baos.write(buffer,0,length);
        }
        in.close();
        byte[] data = baos.toByteArray();
        baos.close();
        return data;
    }
}

Reply via email to