cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amazoone <amazo...@free.fr>
Subject RE: Download - MultiPart - Java heap space
Date Thu, 07 Jul 2011 09:40:54 GMT
Hi, sure ! this is my code.
I hope it will be usefull

# The interface

*@WebService
public interface MediaService {

	/**
	 * Provides a service wich allow to get a specific mp3
	 */
	public byte[] getMp3() throws FileNotFoundException, IOException;

}*

# The implementation

*public class MediaServiceImpl implements MediaService {

	private ResourceBundle properties;
	
	public MediaServiceImpl() {
		super();
		try {
			this.properties = ResourceBundle.getBundle("config");
		} catch (java.util.MissingResourceException mre) {
			mre.printStackTrace();
		}
	}

	/**
	 * Provides a service wich allow to get a specific mp3
	 * @author mickaƫl.camelot
	 */
	@GET
	@Produces("audio/x-mpeg")
	public byte[] getMp3() throws IOException {
		final java.io.File fichier = new File(properties.getString("mp3uri"));
		final FileInputStream f = new FileInputStream(fichier);
		return FileUtils.inputStreamToByteArray(f);
	}
}*

# The Utilities Class

public class FileUtils {
	
	/**
	 * Convert a byte[] to a File
	 * @param bytes
	 * @param file
	 * @throws java.io.IOException
	 */
	public static void byteToFile(final byte[] bytes, final String file) throws
java.io.IOException {
		try {
			final FileOutputStream fileOutputStream = new FileOutputStream(file);
			for (int i = 0; i < bytes.length; i++) {
				fileOutputStream.write((int) bytes[i]);
			}
			fileOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Convert a Stream to a byte[]
	 * @param inStream the media converted to a Stream
	 * @return byte[] the stream converted to a byte[]
	 * @throws IOException
	 */
	public static byte[] inputStreamToByteArray(final InputStream inStream)
throws IOException {
	    final ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
	    final byte[] buffer = new byte[8192];
	    int bytesRead;
	    while ((bytesRead = inStream.read(buffer)) > 0) {
	        byteArrayOutputStream.write(buffer, 0, bytesRead);
	    }
	    return byteArrayOutputStream.toByteArray();
	}
}

# The cfx file
*
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://cxf.apache.org/core"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jaxws="http://cxf.apache.org/jaxws"

	xsi:schemaLocation="http://www.springframework.org/schema/beans
	       http://www.springframework.org/schema/beans/spring-beans.xsd
		   http://cxf.apache.org/core 
		   http://cxf.apache.org/schemas/core.xsd
	       http://cxf.apache.org/jaxrs
		   http://cxf.apache.org/schemas/jaxrs.xsd
	       http://cxf.apache.org/jaxws
	       http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
  <jaxrs:server id="media"
      serviceClass="com.partage.services.MediaServiceImpl"
      address="/mediaService"/>
  
</beans>*

# the web.xml
*
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>CFX Partage</display-name>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:com/partage/services/cxf.xml</param-value>
	</context-param>

	<listener>
	    <listener-class>
	      org.springframework.web.context.ContextLoaderListener
	    </listener-class>
  	</listener>
  
	<servlet>
	    <servlet-name>CXFServlet</servlet-name>
	    <servlet-class>
	        org.apache.cxf.transport.servlet.CXFServlet
	    </servlet-class>
  	</servlet>
  
	<servlet-mapping>
	    <servlet-name>CXFServlet</servlet-name>
	    <url-pattern>/services/*</url-pattern>
  	</servlet-mapping>
  
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>*

--
View this message in context: http://cxf.547215.n5.nabble.com/Download-MultiPart-Java-heap-space-tp4552691p4560497.html
Sent from the cxf-user mailing list archive at Nabble.com.

Mime
View raw message