It's not a secret anymore!*
The moose likes Applets and the fly likes Send file to applet from PHP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Send file to applet from PHP" Watch "Send file to applet from PHP" New topic
Author

Send file to applet from PHP

Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
Hi everyone!!, in this time...

I am very stuck with this thing... i need to use an applet to digitally sign pdf files in a system writen in PHP... those files are stored in a mysql Data Base, the applet can not access directly to the DB... so my solution is to have an applet that recives an array of IDs of files... then the applet calls send a POST to other PHP module that expects an id and returns a file... so, the applet calls the module one time for each id in the array... once the applet have all the files, it calls the signature method... and sign all the files, finally it sends each signed file to another PHP module... this module get the file and an Id and save the file in the database.

1- I dont know how to do to recive the array of ids in the applet.
2- I dont know how to do to recive the file in the applet.
3- I dont know how to send the file and the id to the php module at the same time...

Somebody can help me?.

Best Regards,
JM Diaz
Daniel Rodriguez Hernandez
Greenhorn

Joined: Nov 26, 2009
Posts: 13
Hi Juanma.

To send information from HTML to Applet, you must use the tag <PARAM=...>
To get a parameter in the applet, use "getParameter()" method..

Here you have an example..
http://java.sun.com/docs/books/tutorial/deployment/applet/param.html

To pass information from Applet to PHP, I think that use "Applet".showDocument and using GET method... but it is very limited... I don't know if exists another method.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41096
    
  44
An applet can use the URLConnection class to access any server HTTP resource it wants to, provided it's on the same server where the applet is hosted.

For file downloads to the applet, the server can just stream them.

For file uploads from the applet, check out the Apache Commons HttpClient library.


Ping & DNS - my free Android networking tools app
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
Hi there!!, i will try that tomorrow... i will tell you about the results!!

Regards,
JM Diaz
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
Well, i did my tests, this are the results,

I can get the file from the PHP page.

i can sign the file.

Now i need to send the file and the fileĀ“s id to another PHP page wich update the field in the database... but i dont know how to send it using URLConnection the id and the file at the same time... any ideas??

Best Regards,
JM Diaz
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41096
    
  44
Not sure about the specifics, but if a standard HTML form can upload text fields and files at the same time, then so can the HttpClient library; check its documentation for examples.
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
Well... i do my best, but i cant make it works.

Everything works fine, but the file that i get in the applet has 2 more bytes than the original file (fuc"$%"#$%!!!)...

I try with differents methods to convert the inputStream from the connection to a byteArray to sign the file.

These are my files:

PHP to get the file, this php receives an ID and returns the fileContent for the id in the database:



PHP to update the file, this php receives a signed file and an ID... it updates the signed file column for the id.



At last, two java methods of the applets, one for get the files an another to update them. I suppose that the problem is in the getFileForSign method, in the line:



But not sure.

This are the methods:


Best Regards,
JM Diaz
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41096
    
  44
First guess would be that the encoding is off somewhere. The Java code doesn't handle encoding anywhere where strings are converted into byte[] (and vice versa), meaning that the platform default encoding will be used - which may very well not be the same encoding the file is in.

I've no idea what PHP does in this respect.
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
Hi again!!, first of all, HAPPY NEW YEAR!!!...

The 2010 comes but does not have more solutions than 2009!!... i am still in the same place... that fuc..ing two bytes are still there... now i am trying with a diferent environmet of testing... any idea about what could be happening here?

Best Regards and thanks in advance,

JM Diaz
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41096
    
  44
As I said: it might be the encoding. What have you tried to eliminate that possibility?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Juan Manuel Diaz wrote:Everything works fine, but the file that i get in the applet has 2 more bytes than the original file...

But the question really is: When you download the result of that PHP from your browser, instead of using the applet, does it also have 2 more bytes? If that's the case then the problem is in your PHP code, or else in your understanding of what the PHP code returns. You have two processes here, PHP and applet, and I don't see any reason to focus on the applet.
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
Hi, thanks for answer quickly... i modified the php to download the pdf... and works fine...


The modified getFile.php:


I realized that there are not only 6 bytes more, the characters between stream tag inside the pdf file have strange characters, sounds like enconding character problems..., I tried to re install php, apache2, mysql from scratch, i set everything to ISO-8859-1, but i have the same error... the exceded 6 bytes are blank spaces at the end of the file... the real problem is the character enconding that modifies only the characters between the stream and endstream tag... thats very strange...

I dont know what to think... please help god of PHP and JAVA!!! hehehehe

Regards,

JM Diaz
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Juan Manuel Diaz wrote:Hi, thanks for answer quickly... i modified the php to download the pdf... and works fine...
So it works fine now?
I realized that there are not only 6 bytes more, the characters between stream tag inside the pdf file have strange characters, sounds like enconding character problems..., I tried to re install php, apache2, mysql from scratch, i set everything to ISO-8859-1, but i have the same error... the exceded 6 bytes are blank spaces at the end of the file... the real problem is the character enconding that modifies only the characters between the stream and endstream tag... thats very strange...
Or it doesn't work fine now? Which is it? What is working right and what is working wrong?

One other thing... a PDF file is a binary file. Perhaps your PHP code is assuming it's a text file? I don't know any PHP so I can't comment on that.
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
HI,

Get the file directly from the php in a browser like firefox works fine... i call http://localhost/getFile.php?fileId=1 and I get the pdf file for save or open... when i open it i can see the content right...

When I try to get the file from the applet... the file has more bytes than the original file and has the characters inside the stream tags (you can try to open a pdf file with a text editor and you could see what are the stream tags in the pdf) with encoding errors...

I dont know what to do... i am not a PHP developer... just a Java developer... so i can not do strange things with PHP....

This thing is make me nervous.

I hope someone could help me.... any file or any question are available if it is required.

Regards,

A nervous JM Diaz
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Juan Manuel Diaz wrote:Get the file directly from the php in a browser like firefox works fine... i call http://localhost/getFile.php?fileId=1 and I get the pdf file for save or open... when i open it i can see the content right...

Okay. But here you are using the GET method. In your applet code you use the POST method.
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
Thats not correct... i dont execute that page... i am sorry... i force the id to 1 in the php and call http://localhost/getFile.php
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
I am sorry again... it was very late night when i did that post!!! hahaha... you are right, but how can i do to call the php with GET method instead of POST?, it could be a way to resolve it??.. i am not sure...

Guys, this is a very strange behavior, i do not know what to do next...

Best Regards,

JM Diaz
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
Good news guys!!, I make it works...

I change the line:



to:



In the method getFileForSign()...

It was just that!!...

Thanks to all!,

Juan M. Diaz
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41096
    
  44
So it was the encoding ... which I suggested might be the problem on December 12th.
Juan Manuel Diaz
Ranch Hand

Joined: Mar 23, 2009
Posts: 53
It is clearly obvious that was a problem related to encoding, and I knew it... the purpose of the post was how to fix it!!! I apreciate your colaboration.

Thanks again to all!!,

Best regards,
JM Diaz
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Send file to applet from PHP
 
Similar Threads
FTP Help
Signing an Applet & FTP
Byte[] to File without saving on disk
How to create a method to accomodate array in method parameter?
to find the count of text files in a directory..please help..