Maybe this is easier, my version uses a database but you can just adapt it to
read from the file system:
/**
* A component to retrieve and render a given image
* @author Peter Stavrinides
*
*/
public class PersonPhoto extends PageBase{
@Inject
private ComponentResources resources_;
@Parameter(name = "pId", required = true)
private int pId_;
/** The alternative title of the image */
@Parameter(name = "title", required = true)
private String title_;
/**
* Renders a photo
*/
@BeginRender
protected boolean beginRender(MarkupWriter writer) throws SQLException {
ImageIcon image = null;
//this simply returns a byte[]
image = new ImageIcon(photo.getPhoto())
// Write an action link for this photo
final Link link = resources_.createEventLink("photo", new
Object[] { pId_ });
writer.element("img", "src", link, "title", title_, "alt",
"Loading "
+ title_, "height", image.getIconHeight(),
"width", image
.getIconWidth());
resources_.renderInformalParameters(writer);
writer.end();
return true;
}
/**
* Ends the rendering of the image tag
*
* @param writer
* the mark-up write
*/
@AfterRender
protected void afterRender(MarkupWriter writer) {
//writer.end();
}
/**
* Render the photo to a stream.
* @return the Stream response
*/
public StreamResponse onPhoto() {
//once again the byte array
final ByteArrayInputStream stream = new
ByteArrayInputStream(photo
.getPhoto());
return new StreamResponse() {
public String getContentType() {
return "image/jpg";
}
public InputStream getStream() {
return stream;
}
public void prepareResponse(Response response) {
// Nothing to be done
}
};
}
}
Its that simple.
Peter
----- Original Message -----
From: "Andy Pahne" <[email protected]>
To: "Tapestry users" <[email protected]>
Sent: Wednesday, 11 March, 2009 14:27:53 GMT +02:00 Athens, Beirut, Bucharest,
Istanbul
Subject: Re: Image Asset Help Needed
Peter Kanze schrieb:
> Hi Andy,
>
> Yes I read that thread, I started that thread as well.
> I will vote for the issue, but the problem is I need a solution now..
>
> Maybe I add a good old servlet that returns the image....
> Any other ideas?
>
>
Yeah, that was my workaround.
> Regards,
> Peter
>
>
> On Wed, Mar 11, 2009 at 12:49 PM, Andy Pahne <[email protected]>wrote:
>
>
>> Did you read the thread "How to load image Asset from filesystem"?
>>
>> You might want to vote for the issue.
>>
>> Regards,
>> Andy
>>
>>
>> Peter Kanze schrieb:
>>
>> Here am I again.
>>
>>> I am struggling for 2 days now to load an image from the filesystem into
>>> my
>>> webpage.
>>> The image needs to be loaded based on the product id.
>>>
>>> The steps I did was:
>>>
>>> 1. Implemented a FileSystemAssetFactory based on the ClasspathAssetFactory
>>> 2. Implemented a FileSystemResource;
>>> See the code below:
>>> In my html the following code is generated:
>>>
>>> <img src="
>>>
>>> /advertaza/assets/classpath/6675930334be219a/C:/tmp/pictures/8/71/1001/thumb1.jpg
>>> " alt=""/>
>>>
>>> As you can see the file system path is exposed into the webpage. I don't
>>> want this.
>>> The second problem is that the image is not loaded/visible in the webpage.
>>>
>>> What am I doing wrong? Are there tapestry guru's who can help me out?
>>>
>>> Thanks,
>>> Peter
>>>
>>> /*
>>> *My quick example code
>>> */
>>>
>>> public String getThumbnailPath() {
>>> Asset asset = assetSource.getAsset(null,
>>> "file:/8/71/1001/thumb1.jpg", null);
>>> return asset.toClientURL();
>>> }
>>> public class FileSystemAssetFactory implements AssetFactory {
>>>
>>> private final ClasspathAssetAliasManager aliasManager;
>>>
>>> private final Map<Resource, String> resourceToDefaultPath =
>>> CollectionFactory.newConcurrentMap();
>>>
>>> private final FileSystemResource rootResource;
>>>
>>> private final AssetPathConverter converter;
>>>
>>> private final boolean invariant;
>>>
>>>
>>> public FileSystemAssetFactory(ClasspathAssetAliasManager aliasManager,
>>> AssetPathConverter converter) {
>>> this.aliasManager = aliasManager;
>>> this.converter = converter;
>>>
>>> rootResource = new FileSystemResource("C:/tmp/pictures/");
>>> invariant = converter.isInvariant();
>>> }
>>>
>>>
>>> private String clientURL(Resource resource) {
>>> String defaultPath = resourceToDefaultPath.get(resource);
>>>
>>> if (defaultPath == null) {
>>> defaultPath = buildDefaultPath(resource);
>>>
>>> resourceToDefaultPath.put(resource, defaultPath);
>>> }
>>>
>>> return converter.convertAssetPath(defaultPath);
>>> }
>>>
>>> private String buildDefaultPath(Resource resource) {
>>> //boolean requiresDigest = cache.requiresDigest(resource);
>>>
>>> String path = resource.getPath();
>>> int lastdotx = path.lastIndexOf('.');
>>> path = path.substring(0, lastdotx + 1) + path.substring(lastdotx);
>>>
>>> return aliasManager.toClientURL(path);
>>> }
>>>
>>> public Asset createAsset(final Resource resource) {
>>> return new AbstractAsset(invariant) {
>>> public Resource getResource() {
>>> return resource;
>>> }
>>>
>>> public String toClientURL() {
>>> return clientURL(resource);
>>> }
>>> };
>>> }
>>>
>>> public Resource getRootResource() {
>>> return rootResource;
>>> }
>>>
>>> }
>>>
>>> public class FileSystemResource extends AbstractResource {
>>>
>>> private static final int PRIME = 37;
>>>
>>> /**
>>> * Constructor with the root path
>>> * @param path the root
>>> */
>>> public FileSystemResource(String path) {
>>> super(path);
>>> }
>>>
>>> /**
>>> *
>>> */
>>> @Override
>>> protected Resource newResource(String path) {
>>> return new FileSystemResource(path);
>>> }
>>>
>>> @Override
>>> public URL toURL() {
>>> String filePath = getPath();
>>> File file = new File(filePath);
>>>
>>> if (file != null && file.exists()) {
>>> try {
>>> return file.toURL();
>>> }
>>> catch (MalformedURLException ex) {
>>> throw new RuntimeException(ex);
>>> }
>>> }
>>> return null;
>>> }
>>>
>>> @Override
>>> public String toString() {
>>> return getPath();
>>> }
>>>
>>> @Override
>>> public int hashCode() {
>>> return PRIME * getPath().hashCode();
>>> }
>>>
>>> @Override
>>> public boolean equals(Object obj) {
>>> if (this == obj)
>>> return true;
>>> if (obj == null)
>>> return false;
>>> if (getClass() != obj.getClass())
>>> return false;
>>>
>>> final FileSystemResource other = (FileSystemResource) obj;
>>>
>>> return getPath().equals(other.getPath());
>>> }
>>>
>>> }
>>>
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
>>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]