Hi Francois,

Following example works.....

1-Create this class anywhere you want need.

package com.antilia.demo.manager.img;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.DynamicImageResource;
import org.apache.wicket.model.Model;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequestCycle;
import org.apache.wicket.util.file.Folder;

/**
 *
 * @author  Ernesto Reinaldo Barreiro (reier...@gmail.com)
 *
 */
public abstract class MountedImageFactory {


static int BUFFER_SIZE = 10*1024;
 /**
     * Copies one stream into the other..
 * @param is source Stream
 * @param os destination Stream
 * */
static public void copy(InputStream is, OutputStream os) throws IOException
{
byte[] buf = new byte[BUFFER_SIZE];
while (true) {
int tam = is.read(buf);
if (tam == -1) {
return;
}
os.write(buf, 0, tam);
}
}
 public static  byte[] bytes(InputStream is) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(is, out);
return out.toByteArray();
}
 private static ImageFromFolderWebResource dynamicResource;
 private static class ImageFromFolderWebResource extends
DynamicImageResource {
 private static final long serialVersionUID = 1L;

 private File folder;
 public ImageFromFolderWebResource(File folder, String mountPoint) {
this.folder = folder;
WebApplication.get().getSharedResources().add(mountPoint, this);
WebApplication.get().mountSharedResource(mountPoint,
"org.apache.wicket.Application/"+mountPoint);
}
 @Override
protected byte[] getImageData() {
try {
String name = WebRequestCycle.get().getRequest().getParameter("name");
return bytes(new FileInputStream(new File(getFolder().getAbsolutePath() +
System.getProperty("file.separator")+(name))));
} catch (Exception e) {
//TODO: do this properly
return null;
}
}

public File getFolder() {
return folder;
}
}
 /**
 * @return Folder from where images will be retrieved.
 */
protected abstract Folder getFolder();
 /**
 * @return the URL to mount the dynamic WEB resource.e.g.
 */
protected abstract String getMountPoint();
 public Image createImage(String id, final String imageName) {
if(dynamicResource == null)
dynamicResource = new ImageFromFolderWebResource(getFolder(),
getMountPoint());
return new Image(id) {
 private static final long serialVersionUID = 1L;

@Override
protected void onBeforeRender() {
String path = WebRequestCycle.get().getRequest().getURL();
path = path.substring(0, path.indexOf('/'));
add(new AttributeModifier("src",true, new
Model<String>("/"+path+"/"+getMountPoint()+"?name="+imageName)));
super.onBeforeRender();
}
};
}
}

2- Create a test page.

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.util.file.Folder;

/**
 * @author  Ernesto Reinaldo Barreiro (reier...@gmail.com)
 *
 */
public class TestPage extends WebPage {

private static final MountedImageFactory IMAGE_FACTORY = new
MountedImageFactory() {
 @Override
protected Folder getFolder() {
return new Folder("C:/temp/images");
}
 @Override
protected String getMountPoint() {
return "test";
}
 };
 /**
 *
 */
public TestPage() {
Image img = IMAGE_FACTORY.createImage("img", "test.png");
add(img);
}
}

and the HTML markup

<html xmlns:wicket="org.apache.wicket">
<head></head>
<body>
    <img wicket:id="img" alt="Test"/>
</body>
</html>

3- If you place a "test.png" on your "C:/temp/images" then you should be
able to see the image when you hit the page.

Hope you can adapt this to your needs?

Regards,

Ernesto

2010/1/27 François Meillet <fm...@meillet.com>

> Thank for yours posts.
> I try the solutions, but  I can't figure out how to serve images as static
> images.
> F.
>
> Le 27 janv. 2010 à 16:10, Thomas Kappler a écrit :
>
> > On 01/27/10 15:57, Jonas wrote:
> >> Have you tried the following:
> >>
> >> WebComponent image = new WebComponent("someWicketId");
> >> image.add(new SimpleAttributeModifier("src", "http://.....jpg";));
> >> add(image);
> >>
> >> with markup
> >>
> >> <img wicket:id="someWicketId" />
> >>
> >>
> >> that should work just fine...
> >>
> >> if you cannot hardcode the image url, you can use the following
> >> instead of SimpleAttributeModifier
> >> image.add(new AttributeModifier("src", true new
> >> AbstractReadOnlyModel<String>() {
> >>     public String getObject() {
> >>         String url = ... (fetch the image url from anywhere else)
> >>         // e.g. '/xxx/yyyy/image893748.png'
> >>         return url;
> >>     }
> >> ));
> >
> > Or, maybe a bit nicer, encapsulate it into a component and let the URI
> come from a Model, as usual in Wicket:
> >
> > class ExternalImageUri
> > extends WebComponent
> > {
> >       public ExternalImageUri(String id, IModel<String> uri)
> >       {
> >               super(id, uri);
> >               add(new AttributeModifier("src", true, uri));
> >       }
> >
> >       @Override
> >       protected void onComponentTag(ComponentTag tag)
> >       {
> >               super.onComponentTag(tag);
> >               checkComponentTag(tag, "img");
> >       }
> > }
> >
> >
> > This in the Wiki at
> http://cwiki.apache.org/WICKET/how-to-load-an-external-image.html.
> >
> >
> > -- Thomas
> >
> >
> >> 2010/1/27 François Meillet<fm...@meillet.com>:
> >>> Hi Wicketers,
> >>>
> >>> I have a directory, /xxx/images with uploaded images, which is not
> under the application context root directory.
> >>> How can I serve them as static images ?
> >>>
> >>> I tried the StaticImage class I found in the forum (
> http://old.nabble.com/Plain-IMG-src-urls-td21547371.html#a21547543 )
> >>> but it doesn't work for me. It just work if the image files are under
> the context root directory.
> >>>
> >>> Thanks for your help.
> >>>
> >>> François
> >>>
> >>>
> >>>
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> >>> For additional commands, e-mail: users-h...@wicket.apache.org
> >>>
> >>>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> >> For additional commands, e-mail: users-h...@wicket.apache.org
> >>
> >
> >
> > --
> > -------------------------------------------------------------------
> >  Thomas Kappler                        thomas.kapp...@isb-sib.ch
> >  Swiss Institute of Bioinformatics         Tel: +41 22 379 51 89
> >  CMU, rue Michel Servet 1
> >  1211 Geneve 4
> >  Switzerland                              http://www.uniprot.org
> > -------------------------------------------------------------------
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> > For additional commands, e-mail: users-h...@wicket.apache.org
> >
>
> François Meillet
> fm...@meillet.com
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

Reply via email to