CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2001
    Posts
    18

    Applet should write JPG-Image to Webserver

    hi everyone,

    i have a big problem, writing a image from my applet to my apache webserver. i tried three way's of writing that file. every way was described in forums to solve this problem, but non of them worked and i don't know why. i'll give you the code of my writing-methods and describe, what happen when i test them, in order someone of you can give me a usefull tip, where the problem is.

    as inputparameter i give my method a new URL referring to http://localhost/test.jpg (this is the same directory, where my applet is loaded from, so i should have reading and writing permission, havn't i? while i'm developing, my applet runs on the same pc as my webserver, just in case you're wondering about localhost) and a selfmade BufferedImage (i already testet if it is not null and shows the correct things ... all ok).

    1. try:
    Code:
    private void writeImageToServer(URL fileURL,BufferedImage img){
        try {
          URLConnection urlConnection = fileURL.openConnection();
          urlConnection.setDoOutput(true);
    
          OutputStream urlout = urlConnection.getOutputStream();
    
          BufferedOutputStream out = new BufferedOutputStream(urlout);
    
          ImageIO.write(img,"jpg",out);
    
          out.close(); // i also tried without this line -> same result
                             // additionally a question: do i need out.close()?
        }
        catch( IOException e ){
          e.printStackTrace();
        }
      }
    result:
    test.jpg doesn't appear in the webroot. but some very strange messages in the error.log of apache:

    [Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not exist: c:/programme/apache group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageOutputStreamSpi
    [Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not exist: c:/programme/apache group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageReaderSpi
    [Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not exist: c:/programme/apache group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageInputStreamSpi
    [Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not exist: c:/programme/apache group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageWriterSpi
    [Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not exist: c:/programme/apache group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageTranscoderSpi

    i cannot explain this lines to myself, because my apache should have nothing todo with java. all my java is on the client side in the applet. does this mean i have to add the ImageIO package from the sdk to my jar-applet. maybe the jre, used by my iexplorer doesn't contain this files, but that's version 1.4.2_03, which contains the corresponding classfiles at javax.imageio.spi (but not the listet files in the meta-inf/services directory). i'm realy confused by this messages.


    2. try:
    Code:
    private void writeImageToServer(URL fileURL,BufferedImage img){
        try {
          URLConnection urlConnection = fileURL.openConnection();
          urlConnection.setDoOutput(true);
    
          OutputStream urlout = urlConnection.getOutputStream();
    
          BufferedOutputStream out = new BufferedOutputStream(urlout);
    
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    
          encoder.encode(img);
    
    
          out.close(); // same comments as above
        }
        catch( IOException e ){
          e.printStackTrace();
        }
      }
    result:
    nothing. no error-messages in the error.log, no exceptions in the java-console and no test.jpg in the webroot. i searched my whole harddrives for it: nothing. isn't this the way, the JPEGImageEncoder works?


    3. try:
    Code:
    private void writeImageToServer(URL fileURL,BufferedImage img){
        try {
    
          File file  = new File(fileURL.toString);
          file.createNewFile();
    
          BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    
          encoder.encode(img);
    
    
          out.close(); // same comments as above
        }
        catch( Exception e ){
          e.printStackTrace();
        }
      }
    result:
    the SecurityManager denies this action with "access denied" while calling createNewFile(). ok, i'd have to sign my applet to get the rights to do this, or i can edit java.policy on my client, what i don't want, because i cannot do this on every client, the applet will run, when i'm finished with it. this brings me to the question: does anybody know's how to sign my applet and give it full access to the harddrive and the webserver without paying 400$ to VeriSign for a commercial CA? i want to do this by myself, without paying anything and without giving a lot of information to another company.


    i would realy appreciate, if someone could give me a hint where i am wrong or how to do this correct.

    thank you very much

    wayne

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    why not just install an ftp server on the webserver computer, connect a socket to the ftp server, and quote the necessary commands to upload a file, then when asked to send the file, merely send jpeg encoded data down the socket from a run-time jpeg encoder?

    at the webserver end you have a program that is capable of receiving a stream of bytes and turning it into a file.. that's the ftp server
    so all you do is connect a socket to the ftp server and make it believe youre sending a file; well, you ARE sending a file, just not one on the local hard disk.. generate the bytes straight out of data held in memory, and upload them.. the ftp server will save it as a file

    actually, you might not even need the socket (though it is guaranteed to work) - have a look in my signature for FTP example - a very simple way of sending data to an ftp server
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Nov 2001
    Posts
    18
    thanks very much, for trying to help me. i think, you're right. installing a ftp server would solve the problem or make it lot easier to solve. but the thing is, that my applet is just a small part of a greater system. installing a ftp-server just because i don't find the right way of transfering the image via HTTP isn't arguable (to my boss). and what about security. i think, it would open a new security hole on the server. i don't know, how i could explain that to the users (the target group is very very sensitive to security. they will have the server running on one of their machines in their LAN).

    yesterday, after some frustrating day's of fruitless search for a proper solution, i found a workaround, that might do it: in my applet i can call JavaScript functions of the document the applet was loaded from. i'm now trying to pass the image as a string as parameter of a function. this function will hand over the string to a php-script, that saves the imagedata into a file on the server. if this won't work, i'll save the image into the temp-dir of the current user and call a document (or a JavaScript-function that does) with a php-script that'll do the transfer to the server. ok, the last solution isn't much better, than involving a new technology (FTP) on the server, but i think it's justifiable for the users and it'll work.

    i add some usefull code about that stuff, for anyone who needs help with this:

    1. calling a JavaScript function and passing parameter values:
    Code:
    import netscape.javascript.JSObject; //will work on IE too
    
    ...
    
    JSObject window = JSObject.getWindow(myApplet);
    window.call("myJSFunction",new Object[] {"aStringContent"});
    a little more help on using JavaScript from a Java applet can be found at http://www.apl.jhu.edu/~hall/java/Ja...from-Java.html

    2. sign an applet by yourself (without paying anything to anyone), to break out the sandbox:
    Code:
    import java.security.*;
    
    ...
    
    AccessController.doPrivileged(new PrivilegedAction(){
        public Object run(){
             // add code here, that doesn't work, when you are in a sandbox
            return null;
        }
    });
    after editing your code and making a jar-file of it, open a terminal-window of your OS and call:
    c:\jdk\bin\keytool -genkey -alias myCertificat
    c:\jdk\bin\jarsigner myApplet.jar myCertificat

    you'll find a more detailed description how to do this at:
    http://www.raditha.com/java/sandbox/

    greetings
    wayne

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    i'm sorry to say that i think you'll fail on both counts; the proposed solutions are fiddly and rely heavily on unreliable technologies being bullied into doing jobs for which they were not designed..

    your boss is prepared to run a webserver with cgi, but not an ftp server? i think you need to have a chat about that.

    ultimately, it would be far easier, than either of your proposed solutions, to write a small java program, that sits on the web server on a custom port. after some simple authentication (which may involve calls to the webserver to see if the user is allowed), it will accept a raw stream of bytes and save it to a jpeg file in a predetermined location. the location may not be specified by the user, only by the admins of the server, so it is as secure as the webserver is. it is not capable of serving files, only receiving.. etc

    but to be honest, its just extra work.. you can very quickly configure a ftp server to accept logins based on realtime users of a system, dump them into a default directory that they have no DELE, LIST or RETR permission on, and allow them to upload only. the data is uploaded blind; the only thing that the applet decides is the filename.. youve achieved near the same level of functionality as writing your own server, with no work apart from 30 mins config

    heres a little golden rule that you should always bear in mind for enterprise apps: never, ever, EVER even THINK about trying to hack around a problem by doing something on the client side. someone somewhere will break it and screw you over..

    if you require details on this proposal, then put some of your imagination into it, and ask me a few more questions

    ps; javascript? in a proper application? noo...
    Last edited by cjard; June 17th, 2004 at 12:32 PM.
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  5. #5
    Join Date
    Nov 2001
    Posts
    18
    guess you're right and my solutions won't work. but i'm still not convinced of using a FTP, because i found a lot of applet's in the web, that are uploading their data with HTTP (like JUpload or Rad Upload). what i see is, that it's possible to make that without involving another technology. with Rad Upload comes sample php-code, but sadly no code of the applet, so i don't know, how they did the filetransfer into the global field, they're accessing in their upload.php. anyway, that's exactly the solution, i'm looking for.

    1. give an url to my applet as parameter, where the server-script is, that handles the upload
    2. let the applet initialise a global field in the context of the applet/website/response or whatever
    3. applet writes the imagecode into that field
    4. applet calls the server-script

    when i don't have a running version soon, i'll try your solutions
    (with the ftp or with the serverside-java-app, that listens on ports). i tell ya, this whole thing is killing my nerves.

    thanks for your help
    wayne

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    you didnt indicate, initially, that you could have PHP write the file.. but i do still propose FTP because it's a tried and tested, and secure technology. FTP was invented for transferring files, and curiously enough, youre transferring a file! HTTP uploading, on the other hand, wasnt originally designed for binary, and by making one component do everything, youre creating moe of a security hazard.. using separate components is OO practice, so why balk at making it your security practice too? it saves you time etc etc

    but yes, you are right, it is perfectly possible for using HTTP to upload data, because at the end of the day, it is merely a socket connection with some ascii header junk that makes it HTTP instead of FTP
    If you want to write a PHP that accepts a connection, reads the HTTP header and it is of the form that indicates an upload (HTTP PUT), then the PHP can pull the body of the message and write it to file for you.. but then remember that this is running in a WebServer - a things that serves up web pages, not writing files.. you may find yourself writing a lot of the security code yourself, wheras it is already implicit in tools that are dedicated to doing the job of transferring files...

    ...i.e. dont assume that just because youre going to re-use an existing technology to do something for which it was not designed, that it will be any more secure than a proper solution doing the job for which it was designed.. the converse is more likely to be true
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    consider too, that by using a standard HTTP port for everything, that youre making that port more busy, and eventually it may refused connections.. you also have a single point of failure.

    in a real world sense, how many major countries have just one sea-port? few
    and, what happens if there is a single point of failure (like in the UK, when fuel prices rose to ridiculous levels, a couple of guys parked big trucks across the gates to the few major refineries, the supply of fuel to the country was killed and gradually, the country literally ground to a halt)
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  8. #8
    Join Date
    Nov 2001
    Posts
    18
    finaly i did it, generating a solution for the problem, by myself.

    heres the methods of my applet, that are involved tranfering the image data:
    Code:
    public void writeImageToServer(URL scriptURL,BufferedImage img){
        try {
            ByteArrayOutputStream jpegOutput = new ByteArrayOutputStream();
    
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jpegOutput);
    
            encoder.encode(img); // imagedata are coded here
    
            HttpURLConnection urlConnection = (HttpURLConnection)
                                                       fileURL.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setUseCaches(false);
    
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-type",
                                            "application/x-www-form-urlencoded");
    
            byte[] imageData = jpegOutput.toByteArray();
    
            String imageString = byteToString(imageData);
    
    	//jpeg-imagedata has to be coded in UTF-8 or other, depends on the 
             //serverscript
            String content = "img=" + URLEncoder.encode (imageString,"UTF-8");
    
            urlConnection.setRequestProperty("Content-length",
                                             String.valueOf(content.length()));
    
            OutputStream urlout = urlConnection.getOutputStream();
    
            DataOutputStream printOut = new DataOutputStream(urlout);
            printOut.writeBytes(content); // writing data to the serverscript
    
            urlout.flush();
            urlout.close();
            printOut.flush();
            printOut.close();
            jpegOutput.flush();
            jpegOutput.close();
    
            urlConnection.disconnect();
    
            System.err.println("Serverresponse: "+
                               urlConnection.getResponseMessage());
        }
        catch( IOException e ){
            e.printStackTrace();
        }
    }
    
    // transforms an byte[] to String
    private String byteToString(byte[] data){
        String text = new String();
        for ( int i = 0; i < data.length; i++ ){
            text += (char) ( data[i] & 0x00FF );
        return text;
    }
    the following PHP-Script (or any other script, that does the same) location is passed in the parameter scriptURL of the method above (it has to start with http://)
    Code:
    <?php
    global $img;
    
    $file = "images/imagefile.jpg";
    
    $pic = utf8_decode($img);
    
    $fp = fopen("$file", "w+");
    fwrite($fp, "$pic");
    fclose($fp);	
    ?>
    i hope, that helps some of you.

    good luck

    wayne

  9. #9
    Join Date
    Mar 2000
    Location
    Vancouver, BC, Canada
    Posts
    278
    usually when you see an applet transfer files to a web server, the servlet is not writing directly to the hard drive on the network server. Instead, it's sending a stream of data to the server, and lets the server figure out how to write the file to the local drive.
    the server opens a servletinputstream inside a servlet. the applet is not writing directly to the server, it is (as cjard suggested) sending data directly to the servlet on thewebserver, which in turn writes to a file on its local drive using a fileoutputstream.

    the other option (again, cjard's kudos) is to open an inputstream on a listening application on the server. this can even be as simple as an rpc server (about 20 lines of code pasted from the internet) with a download handler. the applet connects to the server's rpc server and opens a rpc connection to it.
    the listening handler receives binary data , and writes to the hard drive using a fileoutputstream

  10. #10
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Thumbs up

    Quote Originally Posted by wayne0101
    finaly i did it, generating a solution for the problem, by myself.

    heres the methods of my applet, that are involved tranfering the image data:
    Code:
    public void writeImageToServer(URL scriptURL,BufferedImage img){
        try {
            ByteArrayOutputStream jpegOutput = new ByteArrayOutputStream();
    
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jpegOutput);
    
            encoder.encode(img); // imagedata are coded here
    
            HttpURLConnection urlConnection = (HttpURLConnection)
                                                       fileURL.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setUseCaches(false);
    
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-type",
                                            "application/x-www-form-urlencoded");
    
            byte[] imageData = jpegOutput.toByteArray();
    
            String imageString = byteToString(imageData);
    
    	//jpeg-imagedata has to be coded in UTF-8 or other, depends on the 
             //serverscript
            String content = "img=" + URLEncoder.encode (imageString,"UTF-8");
    
            urlConnection.setRequestProperty("Content-length",
                                             String.valueOf(content.length()));
    
            OutputStream urlout = urlConnection.getOutputStream();
    
            DataOutputStream printOut = new DataOutputStream(urlout);
            printOut.writeBytes(content); // writing data to the serverscript
    
            urlout.flush();
            urlout.close();
            printOut.flush();
            printOut.close();
            jpegOutput.flush();
            jpegOutput.close();
    
            urlConnection.disconnect();
    
            System.err.println("Serverresponse: "+
                               urlConnection.getResponseMessage());
        }
        catch( IOException e ){
            e.printStackTrace();
        }
    }
    
    // transforms an byte[] to String
    private String byteToString(byte[] data){
        String text = new String();
        for ( int i = 0; i < data.length; i++ ){
            text += (char) ( data[i] & 0x00FF );
        return text;
    }
    the following PHP-Script (or any other script, that does the same) location is passed in the parameter scriptURL of the method above (it has to start with http://)
    Code:
    <?php
    global $img;
    
    $file = "images/imagefile.jpg";
    
    $pic = utf8_decode($img);
    
    $fp = fopen("$file", "w+");
    fwrite($fp, "$pic");
    fclose($fp);	
    ?>
    i hope, that helps some of you.

    good luck

    wayne
    so.. what's the address of this php script then? I'll have a few hundred drones execute a small program (take me about 2 minutes to knock up) that will send your script a continuous stream of garbage.. if you dont notice and go home over the weekend.. well... lets just hope your server has a big hard drive, because if it doesnt.. you'll be looking at a dead system on monday.

    no swap space? what?

    now, what was that your boss was saying about doing everything via a nasty hack of an HTTP script being best for security etc?

    you can see how problems are engineered into systems rather than out of them, by failing to provide multiple, redundant services, doing their own job in a controlled environment
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  11. #11
    Join Date
    Aug 2004
    Posts
    1

    FTP Server

    You solution suits my problem since i need to transfer many images to my server. I ve installed my FTP server but i have not idea hot to implement what you suggest. I was on java.sun.com to get some ideas but could't get any. Coul you give a hand with that. Thanks
    Luana

    Quote Originally Posted by cjard
    why not just install an ftp server on the webserver computer, connect a socket to the ftp server, and quote the necessary commands to upload a file, then when asked to send the file, merely send jpeg encoded data down the socket from a run-time jpeg encoder?

    at the webserver end you have a program that is capable of receiving a stream of bytes and turning it into a file.. that's the ftp server
    so all you do is connect a socket to the ftp server and make it believe youre sending a file; well, you ARE sending a file, just not one on the local hard disk.. generate the bytes straight out of data held in memory, and upload them.. the ftp server will save it as a file

    actually, you might not even need the socket (though it is guaranteed to work) - have a look in my signature for FTP example - a very simple way of sending data to an ftp server

  12. #12
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    if you have installed an ftp server on the same computer that the applet is downloaded from (web server) then you should look in my signature, at the FTP?link

    beyond that.. i need more details before i can help.. exactly where are you getting stuck?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  13. #13
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: Applet should write JPG-Image to Webserver

    Hi
    I want to transfer .exe file from server to clint ,
    i.e my applet should fetch the file from specific address on the server , Save it on particular location , nd after that execute the file .

    Is it possible?. If so then please if you have any sample code please send me. Because i am new to Java and Web Technology.


    Thanks!
    Anant

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured