Java tutorial
//package com.java2s; /* Copyright 2005 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: http://developer.sun.com/berkeley_license.html $Id: SlideshowUtil.java,v 1.1 2005/11/19 00:53:52 inder Exp $ */ import java.io.*; import java.net.URL; public class Main { /** * The URL looks like a request for a resource, such as a JavaScript or CSS file. Write * the given resource to the response output in binary format (needed for images). */ public static void readWriteBinaryUtil(String sxURL, String resource, OutputStream outStream) throws IOException { DataOutputStream outData = null; DataInputStream inData = null; int byteCnt = 0; byte[] buffer = new byte[4096]; // get full qualified path of class System.out.println("RW Base Directory - " + sxURL); // remove class and add resource relative path sxURL = getResourceURL(sxURL, resource); System.out.println("RW Loading - " + sxURL); try { outData = new DataOutputStream(outStream); inData = new DataInputStream(new URL(sxURL).openConnection().getInputStream()); while ((byteCnt = inData.read(buffer)) != -1) { if (outData != null && byteCnt > 0) { outData.write(buffer, 0, byteCnt); } } } catch (IOException e) { throw e; } finally { try { if (inData != null) { inData.close(); } } catch (IOException ioe) { } } } public static String getResourceURL(String sxURL, String resource) { return sxURL.substring(0, sxURL.lastIndexOf("/")) + resource; } }