cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew M <and...@jibeya.com>
Subject Re: POSTing Binary Data to Cocoon
Date Tue, 23 Nov 2004 19:14:35 GMT
Mike,
i use hibernate to do read in an image as a byte array. This should 
point you in the right direction a to reading in a *.wav file.

1:
<?xml version="1.0"?>

<xsp:page
	language="java"
	xmlns:xsp="http://apache.org/xsp"
	xmlns:xsp-session="http://apache.org/xsp/session/2.0"
	create-session="true"
	xmlns:util="http://apache.org/xsp/util/2.0"
	xmlns:xsp-cookie="http://apache.org/xsp/cookie/2.0"
	xmlns:xsp-request="http://apache.org/request/util/2.0"
	xmlns:sendmail="http://apache.org/cocoon/sendmail/1.0"
	>
   <xsp:structure>
    <xsp:include>test.GraphicLoader</xsp:include>
    <xsp:include>test.ArtistImages</xsp:include>
  </xsp:structure>

   <page>
     <title>Hello</title>
     <content>
			<xsp:logic>
				GraphicLoader graph = new GraphicLoader();
				ArtistImages newImages = new ArtistImages();
				newImages.setArtistPhoto(graph.readImage("Logic1.jpg"));
				
			</xsp:logic>

     </content>
   </page>
</xsp:page>

2:
GraphicLoader.java

import java.io.*;

public class GraphicLoader{

	public final byte[] readImage(String imageName) throws IOException{

		FileInputStream fis = new FileInputStream("specify path to save file" 
+ imageName);
		byte[] buf = new byte[4096];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		int len = 0;
		
		while ((len = fis.read(buf)) != -1){
		
			baos.write(buf,0,len);
		}
		
		return baos.toByteArray();
	}
}

3:
Hibernate Map:

	<class name="ArtistImages" table="artistimagestbl">
		<id name="ID" column="image_id">
			<generator class="assigned"/>
		</id>
		<property name="ArtistPhoto" column="artist_photo" type="binary"/>
	</class>

4:
Persistent class:

public class ArtistImages implements Serializable{

	private byte[] ArtistPhoto;

	public byte[] getArtistPhoto() {
		return ArtistPhoto;
	}
	public void setArtistPhoto(byte[] newArtistPhoto) {
		this.ArtistPhoto = newArtistPhoto;
	}

Hope this helps.


Andrew

This class reads the image in as a class array. To read it back, 
convert the bytearray into base64

On 23 Nov 2004, at 18:51, Mike Dickson wrote:

> Background:   I have a .wav file being POSTed to my Cocoon app from a 
> remote service I have not controll over.  I am trying to use Hibernate 
> to save the .wav file to a BLOB in mySQL using a custom Cocoon Action.
>  
> Question: In my custom Action the parameters I get from the pipeline 
> are all Strings.  What is the proper way in Coccon to get the .wav out 
> of the parameter as binary data? 
>   
> Would something like this work?
>     String paramValue = parameters.getParameter("wav"); 
>      byte [] bytes = paramValue.getBytes(); 
>  
> I also see there is a java.sql.Blob class, should I use that instead?
>  
>  
> Thanks,
>  
> Mike D.
>

Mime
View raw message