Link to home
Start Free TrialLog in
Avatar of mac1416
mac1416

asked on

Get byte data from image to post to script

Hi,

Can someone please help me with this. I need to get the pixel data from a BufferImage object (or Image if makes easier) into a byte[] array so that it can be sent to a php or asp script to be saved onto server as jpeg if possible. How do I one get the image data into a byte[] array and how can I turn that data into jpeg format so it can simple be written to server once script recieves post data? btw it's an applet so all the security restrictions apply.Thanks.
Avatar of girionis
girionis
Flag of Greece image

 It would be easier if the other end was a Java server since you could open a socket connection, read the bytes one by one and reconstruct the image on the other side and save it as JPEG using a JPEGImageEncoder.

  Anyway to get the pixels out of a BufferedImage you need to do the following (assuming that your BufferedImage instance is called "bi")

int width = bi.getWidth(null);
int height = bi.getHeight(null);
int[] rgbs = new int[width*height];
rgbs = bi.getRGB(0, 0, width, height, rgbs, 0, w);
Avatar of mac1416
mac1416

ASKER

how wud i send that to a php script and save to server as jpeg or gif or whatever image format is possible?
 You can always open a URLConnection to the site and send the data using connection's OutputStream. Not sure how you coudl read the data on the PHP side though.
Avatar of mac1416

ASKER

i have everything in place to send to php script thats all working fine. Only prob is i just get some short 8-10 byte string. How can I process all the data in the applet so that when the script recieves it all that has to be done is write it to the disk. I have seen similar things done on other sites like this one http://www.lawrencegoetz.com/programs/signature/. maybe i am going about this wrong? i'm not familar with this sort of thing in java sorry so could you please help me with what i should be doing if you can. thanks alot.
 There are three things you can usually do.

  The first is to serialize the image and send it to the server. This is impossible in our case since neither BufferedImage nor Image are serializable.

  The second is to read the Image byte by byte and reconstruct it byte by byte on the server side. To send the image to the server byte by byte you have to obtain the OutputStream of the URLConnection object in your applet in order to write to the PHP script. USe the write() method of the OutputStream().

  The third is to convert the image into a byte array and send the byte array (by chunks or all at once) to the server.

  Can you also post the code from which you are trying to send the image to the server?

  And btw the applet URL in your previous post does not work.
Avatar of mac1416

ASKER

i have a BufferImage object currentImage which holds all the currentImage being displayed data. Here is the code i'm currently trying to do something with. basically just when user clicks save button it does whatever needs to be done to data and sends to server.

if(e.getSource() == btnSave)
                        {
                              BufferedOutputStream f = new BufferedOutputStream();
                              //f.write(buf);
                              int pix[] = new int[iWIDTH*iHEIGHT];
                              byte buf[] = new byte[iWIDTH*iHEIGHT];
                              PixelGrabber pg = new PixelGrabber(currentImage, 0, 0,
                                    iWIDTH, iHEIGHT, pix, 0, iWIDTH);
                              
                              //InputStream fis;
                              try
                              {
                                    pg.grabPixels();

                              //OutputStream fos = null;
                                    // Encode as a JPEG
                                    JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(f);
                                    jpeg.encode(currentImage);
                                    //fis.read(buf);
                                    f.write(buf);
                                    f.close();
                              }
                              catch(FileNotFoundException fe){System.out.println(e);}
                              catch(IOException ioe){System.out.println(ioe);}
                              catch(InterruptedException ie){System.out.println(ie);}
                              
                              
                              if ((pg.getStatus() & ImageObserver.ABORT) != 0)
                              {
                                    System.err.println("image fetch aborted or errored");
                                    return;
                              }


                              try
                              {
                                    URL url;
                                    URLConnection urlConn;
                                    DataOutputStream printout;
                                    DataInputStream input;
                                    url = new URL (getCodeBase().toString() + "JProcess.php");
                                    urlConn = url.openConnection();
                                    urlConn.setDoInput (true);
                                    urlConn.setDoOutput (true);
                                    urlConn.setUseCaches (false);
                                    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                                    // Send POST output.
                                    printout = new DataOutputStream (urlConn.getOutputStream());
                                    // Write bytes to post data
                                    printout.writeBytes ("image="+buf);
                                    printout.flush ();
                                    printout.close ();
                                    // Get response data.
                                    input = new DataInputStream (urlConn.getInputStream ());
                                    String str;
                                    // Output data from JProcess.php
                                    while (null != ((str = input.readLine())))
                                    {
                                          System.out.println (str);
                                    }
                                    input.close ();
                              }
                              catch(MalformedURLException ue)
                              {
                                    System.err.println(ue);
                              }
                              catch(IOException ie)
                              {
                                    System.err.println(ie);
                              }
                        }

Sorry its a bit messy right now. have been trying all sorts of things to make it work and ended up as that lol. Here is the php script the simply when it receives the data writes it to a file.

<?php
      
      // File writing
      $fp = fopen("image01.jpg", "w") or die("Couldn't open file image01.jpg");
      fwrite($fp, $image);
      fclose($fp);
      print "Image data = $image";
?>

I dont know if this is the right sort of way to do it or not. Please feel free to correct or re-write as you see fit for what needs to be accomplished. Thanks heaps for your help.
 I wouldn't expect it to work since the "buf" variable never gets populated with data. You just give it an initial size but you never actually assign any bytes to it.

  Can you post a small compilable example so I can try it on my computer? Otherwise I will try to simulate the whole process and come up with a solution. It would also be helpful if you could tell me what JDK/Java plug in you are using with your applet.
Avatar of mac1416

ASKER

sorry its all part of big project so hard to post compilable example. All that has to happen is a BufferedImage obejct has to be converted to whatever data format or variable and sent through URLConnection to a php script. I am using java 2 1.4.1. Could you please show me an example of code so that when save button clicked it performs whatever conversion it needs to on the BufferedImage so it just gets sent through urlconnection to php script that just writes whatever data it recieves to file image01.jpg (or gif,png whatever is easiest format to accomplish this in). Here is very simple frame of basically what happens in applet.

public class PaintApplet extends JApplet implements ActioListenter
{
     private BufferedImage currentImage;
     
     public void init()
     {
          currentImage = new BufferedImage(iWIDTH, iHEIGHT, BufferedImage.TYPE_INT_ARGB);
         
          // Rest of code for buttons etc here....
     }
     
     // ActionPerformed....
     if(e.getSource() == btnSave)
     {
          // Code to send image to script and save
     }
}

Can you show me what code I need to put into btnSave event so it does what needs to be done to convert and send. Thanks.
Avatar of mac1416

ASKER

just a thought...would this be easier to do with a servlet? I know a bit of asp and small ammount of jsp and have recently got access to tomcat server so if it would make it easier to do with any of those please let me know and let me know what i would have to do to get it to work.
 Using JDk1.4.1 makes our life a lot easier since we can use the new javax.imageio.*; package :-) Unfortunatelly though I do not have jdk1.4.1 installed where I am so evrything I will say is off the top of my head, try to experiment a bit.

  Assuming you have open a URLConnection to the script you want to send the file and you have obtained the output stream by doing: OutputStream os = urlConn.getOutputStream(); then you can convert your BufferedImage image into a JPEG image on the fly and send it to the connection by doing:

// ActionPerformed....
if(e.getSource() == btnSave)
{
    // Code to send image to script and save
    // Not sure if the conversion to RenderedImage is necessary.
    // Try it with both converting it and not converting it.
    ImageIO.write((RenderedImage) currentImage, "jpg", os);
    os.flush();
    os.close();
    InputStream is = urlConn.getInputStream();
}

  The above should decode the BufferedImage into a JPEG image and sent it to the URLConnection output stream.

  Now on the other side if you had a JSP page or a Servlet you had to do something like:

mypage.jsp

<html>
    <body>
    <%
        InputStream is = request.getInputStream();
        BufferedImage image = ImageIO.read(is);
        // Save it in a file
        File file = new File("myimage.jpg");
        ImageIO.write(image, "jpg", file);
        is.close();
    %>
    </body>
</html>

  The above should receive the image from the applet and save it into a file called "myimage.jpg" file. The image should be a JPEG image.

  If you still want to go with the PHP script or with earlier versions of JDK then we can devise another solution of how to send the image. let me know if you need more help.
Avatar of mac1416

ASKER

the applet seems to compile ok but when i hit the save button and read the console with the response from jsp page it says java.io.IOException: Server returned HTTP response code 500 for URL: .../mypage.jsp. Any ideas? This seems the better way to go i think. I havn't got a lot of experience with jsp pages and servlets so please give detailed examples if you can. thanks.
Avatar of mac1416

ASKER

another thing just tried is going to jsp page in ie and it return the following errors..

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 2 in the jsp file: /jsp/mypage.jsp

Generated servlet error:
    [javac] Compiling 1 source file

C:\Tomcat 4.1\work\Standalone\localhost\examples\jsp\mypage_jsp.java:44: cannot resolve symbol
symbol  : class InputStream
location: class org.apache.jsp.mypage_jsp
       InputStream is = request.getInputStream();
       ^



An error occurred at line: 2 in the jsp file: /jsp/mypage.jsp

Generated servlet error:
C:\Tomcat 4.1\work\Standalone\localhost\examples\jsp\mypage_jsp.java:45: cannot resolve symbol
symbol  : class BufferedImage
location: class org.apache.jsp.mypage_jsp
       BufferedImage image = ImageIO.read(is);
       ^



An error occurred at line: 2 in the jsp file: /jsp/mypage.jsp

Generated servlet error:
C:\Tomcat 4.1\work\Standalone\localhost\examples\jsp\mypage_jsp.java:45: cannot resolve symbol
symbol  : variable ImageIO
location: class org.apache.jsp.mypage_jsp
       BufferedImage image = ImageIO.read(is);
                             ^



An error occurred at line: 2 in the jsp file: /jsp/mypage.jsp

Generated servlet error:
C:\Tomcat 4.1\work\Standalone\localhost\examples\jsp\mypage_jsp.java:47: cannot resolve symbol
symbol  : class File
location: class org.apache.jsp.mypage_jsp
       File file = new File("myimage.jpg");
       ^



An error occurred at line: 2 in the jsp file: /jsp/mypage.jsp

Generated servlet error:
C:\Tomcat 4.1\work\Standalone\localhost\examples\jsp\mypage_jsp.java:47: cannot resolve symbol
symbol  : class File
location: class org.apache.jsp.mypage_jsp
       File file = new File("myimage.jpg");
                       ^



An error occurred at line: 2 in the jsp file: /jsp/mypage.jsp

Generated servlet error:
C:\Tomcat 4.1\work\Standalone\localhost\examples\jsp\mypage_jsp.java:48: cannot resolve symbol
symbol  : variable ImageIO
location: class org.apache.jsp.mypage_jsp
       ImageIO.write(image, "jpg", file);
       ^
6 errors


      at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:130)
      at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:293)
      at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:340)
      at org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
      at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:474)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:184)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
      at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:261)
      at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:360)
      at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:632)
      at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:590)
      at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:707)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:530)
      at java.lang.Thread.run(Thread.java:536)



--------------------------------------------------------------------------------

Apache Tomcat/4.1.18

Dont know if thats of any help or not but there it is anyway.
Avatar of mac1416

ASKER

ahhh....i have to import the classes. sorry bout that but like i said i dont have much experiance with jsp pages. Ok now is runs ok and returns the <html>... back to applet so i know executed properly but doesn't save any file. Please advise.
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mac1416

ASKER

it seems that exception was becuase i hadn't imported the classes into the page. the following code excuted without any errors but doesn't actually save any file.

<html>
   <body>
<%@ page contentType="text/html"
    import="javax.imageio.*,java.awt.image.*,java.io.*"
%>
    <%
           InputStream is = request.getInputStream();
           BufferedImage image = ImageIO.read(is);
          // Save it in a file
          File file = new File("myimage.jpg");
          ImageIO.write(image, "jpg", file);
         is.close();
  %>
   </body>
</html>

Im using Apache Tomcat/4.1.18. How do I find the output to console? Here is the code i am now using for when save button clicked.

if(e.getSource() == btnSave)
                    {
                         try
                         {
                              URL url;
                              URLConnection urlConn;
                              DataOutputStream os;
                              DataInputStream input;
                              url = new URL ("http://localhost/examples/jsp/mypage.jsp");
                              urlConn = url.openConnection();
                              urlConn.setDoInput (true);
                              urlConn.setDoOutput (true);
                              urlConn.setUseCaches (false);
                              urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                              // Send POST output.
                              os = new DataOutputStream (urlConn.getOutputStream());
                             
                              // Code to send image to script and save
                            // Not sure if the conversion to RenderedImage is necessary.
                            // Try it with both converting it and not converting it.
                            ImageIO.write((RenderedImage)currentImage, "jpeg", os);
                            os.flush();
                            os.close();
                           
                              // Encode as a JPEG
                         
                              // Write bytes to post data
                              // Get response data.
                              input = new DataInputStream (urlConn.getInputStream ());
                              String str;
                              // Output data from JProcess.php
                              while (null != ((str = input.readLine())))
                              {
                                   System.out.println (str);
                              }
                              input.close ();
                         }
                         catch(MalformedURLException ue)
                         {
                              System.err.println(ue);
                         }
                         catch(IOException ie)
                         {
                              System.err.println(ie);
                         }
                    }
Can you please modify code as needed to make work. Thanks.
 I do not have jdk1.4.1 so I cannot try it here. Can you try adding the exception and tell me if there is any output? The Tomcat console whould be the window from which you started Tomcat.
Avatar of mac1416

ASKER

There is no exceptions generated when i put that in. I cant seem to get the tomcat output. With version 4.1.1.8 it just starts as an nt service. I found this message in the iis_redirector.log file -
[Thu Mar 27 20:18:42 2003]  [jk_isapi_plugin.c (789)]: HttpFilterProc [/jpaint2/meta-inf/services/javax.imageio.spi.imagereaderspi] points to the web-inf or meta-inf directory.
Somebody try to hack into the site!!!

Are there any settings i have to set for tomcat to enable all this to work?
 I see... you run Tomcat as an NT service. Usually there must be something under Tomcat's log folder, there are files such as error.txt and so on. It might also be useful if you can change the System.out.println to System.err.println since this will force the buffer to go to the error stream.
 I see... you run Tomcat as an NT service. Usually there must be something under Tomcat's log folder, there are files such as error.txt and so on. It might also be useful if you can change the System.out.println to System.err.println since this will force the buffer to go to the error stream.
Avatar of mac1416

ASKER

ok in the stderr.log file the following line is added each time i run the script.

Mar 27, 2003 10:46:27 PM org.apache.jk.common.ChannelSocket processConnection
INFO: connection timeout reached

Gets added with or without err.pri.. or any output from the page. I have moved the jsp page from examples folder to root dir of tomcat and web-inf... log message is being generated anymore. It seems the script is working and running just for some reason not actaully writing the image file.
Avatar of mac1416

ASKER

does the content type have anything to do with it? when i send it im sending as urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") and the jsp page has <%@ page contentType="text/html". Could this be effecting it at all?
Avatar of mac1416

ASKER

if use the following code i get the binary image data displayed in the console from the jsp returning data to applet

<%@ page contentType="image/jpeg"
    import="javax.imageio.*,java.awt.image.*,java.io.*,
     com.sun.image.codec.jpeg.*,java.util.*,java.awt.*"
%>
    <%
     try
     {
                InputStream is = request.getInputStream();
                BufferedImage image = ImageIO.read(is);
               // Save it in a file
               File file = new File("myimage.jpg");
               ImageIO.write(image, "jpg", file);
              is.close();

          // Send back image
          JPEGImageEncoder encoder =
                 JPEGCodec.createJPEGEncoder(response.getOutputStream());
          encoder.encode(image);
     }
     catch(Exception e)
     {
          System.err.println("Exception: " + e);
     }
%>

applet code..

URL url;
                              URLConnection urlConn;
                              DataOutputStream os;
                              DataInputStream input;
                              url = new URL ("http://www.behemoth.gotdns.com/mypage.jsp");
                              urlConn = url.openConnection();
                              urlConn.setDoInput (true);
                              urlConn.setDoOutput (true);
                              urlConn.setUseCaches (false);
                              urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                              // Send POST output.
                              os = new DataOutputStream (urlConn.getOutputStream());
                             
                              // Code to send image to script and save
                            // Not sure if the conversion to RenderedImage is necessary.
                            // Try it with both converting it and not converting it.
                            ImageIO.write((RenderedImage)currentImage, "jpg", os);
                            os.flush();
                            os.close();
                              // Get response data.
                             
                              input = new DataInputStream (urlConn.getInputStream ());
                              String str;
                   
                              while (null != ((str = input.readLine())))
                              {
                                   System.out.println (str);
                              }
                              input.close ();

Its generating all the jpeg data just not saving it. Sorry to keep posting long comments like this just only way. Please let me know what i should do.
Avatar of mac1416

ASKER

Sorry was away for few days. Got it finnally!! thanks alot for all your help really appreciate it.
 Thank you. I forgot about that too, too many things to do. Glad you solved it eventually :-)
Avatar of mac1416

ASKER

hey sorry to use this question again just you helped me before with this. ill start a new question if you want and give you more points if you can help me. I need to use this on ms java now not just java 2 1.4. can you please help me and find a way to send the data to the server without useing the ImageIO.write which requires java 2. also i may be using asp for server side page so if that comes into it please let me know. I was thinking of using java program called by asp so has to be ms java too. just another way of getting an image object into jpeg format that can just be written as a file basicly is what im asking. thanks.
 MS Java only supports jdk1.1 a really old version more than five years ago. Is that what you are trying to achieve or you want to use any MS specific libraries that probably support newer versions of MS JVM.

  Also for the ASP script, there shouldn't be any problem since the server side component shouldn't really care about what is sending the data from the client.
Avatar of mac1416

ASKER

I need it to work with people who already have whatever the last Java VM that ms made before that sun suit that forced people who don't already have java to use sun's version. I'm trying to compile with Microsoft SDK for Java 4.0 so whatever supports that. At very least if could support jdk 1.3.1. Thanks for your help.
 I see... You need to make it work with JDK1.1. then. Let me think about it and I will reply back probably later on today :-)
Avatar of mac1416

ASKER

ok thanks alot.
Avatar of mac1416

ASKER

hey i've got hold of a couple of encoders for various formats. How can i read the image on the jsp end without having to use ImageIO.read(...) and can use something like the com.sun...JpegImageEncoder that comes with jdk 1.2? I am sending the image data with class that works basicly like Encoder = new Encoder(Image, OutpurStream) with a URLConnection - how can i read that data so i can use same encoder on jsp side and just write it using FileOutputStream or soemthing? please help!!!
 Hello mac1416 I forgot about, sorry :-(

> How can i read the image on the jsp end without having to use ImageIO.read(...)

  You can do that by obtaining the InputStream of the Servlet. InputStream is = request.getInputStream(); This should obtain a stream that you can read from.

> can use something like the com.sun...JpegImageEncoder that comes with jdk 1.2?

  Hmm.. not sure if this is a godo idea. If you want to use pre-Java2 classes then I'd advise you not to use the JPegImageEncoder since it *could* not be compatible with jdk1.1.8. What you can try is to put jdk1.2 classes in the classpath but this *could* give you a Invalid bytecode error.

> - how can i read that data so i can use same encoder on jsp side and just write it using FileOutputStream or soemthing?

  What is this Encoder class you are using? Is it a standard jdk class? Is it the java.beans.Encoder class?

 
Avatar of mac1416

ASKER

i've managed to get it all working without using any java 2 features. using a pngencoder i picked up. i have another question if you don't mind. sorry to keep asking you just you've offer best advice so far is all. Will assign some points if you like for this.

Seeing as how i've got whole thing to work wihtout need of java 2 can i somehow write this into a java program and call it from an asp script? If i put it into trustlib dir then asp script can call it so how can i make it all work with that if you know? code is below for what the jsp page is currently using. basicly the encoder saves as byte[] array and just send that through outputstream to script from applet. all that has to be done is save the data to a file so how can i do this with either pure asp or what i think is more likly calling a java program froma sp script.

current jsp code -

String sFileName = "untitled.png";     // Filename of saved file
     String sFileType = "png";               // Format to save as. Values: jpg or png
     
     DataInputStream input = new DataInputStream (request.getInputStream());
     FileOutputStream fout = new FileOutputStream(getServletContext().getRealPath("\\JPaint\\" + sFileName));
     
    int ch;
     while ((ch = input.read()) != -1)
     {
          fout.write(ch);
     }
    input.close();

applet code is something like this -

byte[] imgData = encoder.encode();
outStream.write(imgData);

if you want i'll open this as a new question but you've been helping me with so far so apreciate if you could keep doing so. thanks alot.
> can i somehow write this into a java program and call it from an asp script?

  Well it depends. If you write a standard java application I am not sure how you can call it from an ASP page. You might need to look into a JNI solution. However if you write it as a Java Servlet/JSP page then you shouldn't have problems calling it from ASP. You see when send a request to a web based component over HTTP it does not matter what the other end is, since it only makes a connection to a standard HTTP server. The only thing that concerns me here is the data types of an ASP page. I am not very familiar with how data types work in ASP but you could be having problems if Java's data types are different than ASP's data types.

> If i put it into trustlib dir then asp script can call it so how can i make it all work with that if you know?

  Well ASP can call the JSP as long as it is somewhere on a web server. Then in your ASP page you need to get the input stream from the JSP page and write the data to a file. Not sure though how the syntax works in ASP but it the concept is similar to using a JSP page.

> all that has to be done is save the data to a file so how can i do this with either pure asp

  So basically you want to call the ASP script from the Applet? In theory it should be the same as JSP. Follow the same logic and just translate the JSP commands into ASP commands. For example instead of Java's DataInputStream class use ASP's corresponding class. Instead of FileOutputStream use ASP's object that creates a file and so on.