9 Replies Latest reply on Jun 8, 2006 8:57 PM by 843807

    Uploading image to server


      We developed an applet for image cropping, rotating at the client side. We are not using <input type=file ...> for selecting an image. Instead of that, user will clicked on applet to select an image(once user clicked on applet, the FileDialoge will open for selecting an image). Now we want upload this selected image to server when user clicks on submit button. We are able to get the path information of the image (eg: c:\sas\a.jpg). Please any one can help us to solve this problem. If any one needs further information, please let me know. And also I am new to Java technology.

        • 1. Re: Uploading image to server
          All you need to do is write a server side application that recieves raw image data as a parameter. Then you've got to make your Applet write the raw Image data to a String and send this String to the Server by using the
          method replacing the ... an URL pointing the serverside application path and name giving it the raw imagedata as parameter eg. "http://www.fancyimageservice.com/tools/saveimage.php?raw=<imagedatahere>".
          But due to that raw image-data is represented in binary form which means in unreadable control-chars it is a good idea to "URLEncode" the whole String before sending it to the server (use
          String encodedString = new Urlencoder().encode(rawdata,"UTF-8");
          for in the applet) and "URLDecode" it again on the server. Now your server side application just needs to save all the data recieved to a file or into a database.
          Of course this does not consider any security issues which need to be considered given this task.

          But note ther is one thing wich I didn't test with this procedure at all: This procedure can have unexpected results when the lenght of an URL is bound to a maximum lenght (considering images basicly have a lot of data)

          By the way knowledge of the client-side URL is relative because it is NOT possible for an Applet to gain writing access to the client's file-system.
          • 2. Re: Uploading image to server
            Can you pls send me the source code, because we don't know how to create raw image data. Because what you told is good solution seems to be. So please send me the source code for this.

            • 3. Re: Uploading image to server
              Well well try this:
              // the Class referred to and the method 
              // are dummy rplace it by the Instance that 
              // holds the processed Image
              BufferedImage outputImage = yourImageContainer.getImage();
              // retrieving the image's type
              String imageType = outputImage.getType();
              // this stream is used for the String-translation
              ByteArrayOutputStream byteStream = new ByteArrayOutputStream ()
              ImageIO.write(outputImage, imageType, byteStream);
              // this will hold the raw data:
              String rawImageData = byteStream.toString();
              // urlencoding the raw String
              rawImageData = new Urlencoder().encode(rawImageData ,"UTF-8");
              // navigating to the server-side component
              // building the url (replace this by the URL you need)
              // please note: relative urls work differently*
              URL targetURL = new URL("http://www.fancyimagingservice.com/saveimage.php?rawdata="+rawImageData );
              // myApplet is considered to be your applet Instance -> cange!
              and creating the URL using a relative URL:
              // this URL is relative to the current Document
              // (ie. which embedds your Applet)
              URL targetURL = new URL(myApplet.getDocumentBase(), "../saveimage.php?rawdata="+rawImageData );
              I hope that this will help you.
              • 4. Re: Uploading image to server

                Thanks for your extreme help in this regard. outputImage.getType(); is returning integer type. But you mentioned it as string. Can you clarify what can I do regarding this. Also if you don't mind, can you please send me the .php file to retrieve the raw data in the server side. Your help in this regard is highly appreciable. Eagarly waiting for your reply.
                • 5. Re: Uploading image to server
                  Consider reading the API documentation on this topic (it can be found right there <------ on this side in the navigation menu. Try to find out what this integer means exactly then try to tanslate it to String or better: take the extension of the filename on the client side and put it in as the type-name, that should work after all although it is not the best solution therefore since I don't exactly know what kind of type-name ImageIO.write(...) expects (also read the api documentation therefore.)

                  Just as a consideration. I only knew that ImageIO writes images to a Stream. The rest of the code given is created after looking it for in the api documentation. If you use this docuents the right way it will be no problem for you to find out what to do next.
                  Or do you want me to write all your code? ;-)
                  • 6. Re: Uploading image to server
                    I already mentioned that I am new to java. So if you send me the full source code, that will be more helpfull to me. We are able to encode the image on the client side because of your help. But in the server side, we don't know how to decode and write the image to server side.

                    If you send the code for server side manipulation, it will be great helpful to me.
                    • 7. Re: Uploading image to server
                      Refert to www.php.net for doing this. As I suggested use php on the serverside to do this, because the documetation provided on this site gives some good samples of how to do somethin like this (ie. retrieving parameters from the url and writing somethin to disk into a particular file).

                      And even when you are new to programming - not only java - it means that you WILL have to read and learn a lot to do it properly. So if you are unsure and you want to have something done the right way get yourself professional help (of course you will have to pay for that). If you don't want to pay there is no other way than to learn software creation for yourself.

                      (If you refer to my profile you can see that I am learnig how to do this, because I am a student of computer science. Which is of course not only done from now till then, but rather in a longer period of time.)
                      • 8. Re: Uploading image to server

                        Thanks for your sincere reply and help. we got the solution by using MultiPartFormOutputStream class from client side to send the data to the server. In server side, we used .Net file to recieve that uploaded image.
                        But we are facing different kind of problem in Netscape 4.8.First user will enter number of images to be upload. Once user selects the number of images, click on submit, we will display number of applet that user enters. Its working fine in IE and Netscape 7.x. But we are facing problem in Netscape4.x. I will send you the code and exception we are getting when we try to open in Netscape 4.8. Pls go through it and give some solution.


                        <P>Please Enter the Number of image you are going to submit
                        <form name="frm">
                        <INPUT id=text1 name=text1>   <INPUT id=Submit type=button value=Submit name=Click onclick='fun_app()'>  
                        <SCRIPT LANGUAGE=javascript>
                        function fun_app()
                             var val=document.frm.text1.value;
                             window.document.write("<form name='frm1'>");
                             for(var i=1;i<=val;i++)
                                            window.document.write('<applet code="ImageUploadControl.class" width="400" height="430" viewAsText name=Applet'+i+'> <param name="imginit" value="sos-logo-xsmall.jpg"></applet>');

                        error we are getting in netscape 4.8

                        java.net.MalformedURLException: unknown protocol: wysiwyg at java.net.URL.<init>(URL.java:586) at java.net.URL.<init>(URL.java:476) at java.net.URL.<init>(URL.java:425) at sun.plugin.AppletViewer.getDocumentBase(AppletViewer.java:969) at sun.plugin.AppletViewer.getCodeBase(AppletViewer.java:1023) at sun.plugin.AppletViewer.appletInit(AppletViewer.java:545) at sun.plugin.viewer.LifeCycleManager.initAppletPanel(LifeCycleManager.java:171) at sun.plugin.viewer.WNetscapePluginObject$Initer.run(WNetscapePluginObject.java:299) java.lang.NullPointerException at sun.plugin.AppletViewer.getCodeBase(AppletViewer.java:1027) at sun.plugin.AppletViewer.appletInit(AppletViewer.java:545) at sun.plugin.viewer.LifeCycleManager.initAppletPanel(LifeCycleManager.java

                        • 9. Re: Uploading image to server

                          the upload works, but unfortunately the upload breaks whenever the file is more largely than 20 MB.

                          Error: Exception in thread "Thread-3" java.lang.OutOfMemoryError: Java heap space