PHP webservice file upload & download logic with Android client - java

First, this is my first time building both of webservice and android client at the same time, so please kindly help me. My goal is to be able to upload a file from android apps and download it from the webservice.
This is what i did for upload (working, but im not sure this is a best way) :
Convert the image to base64 String in android apps
Send that converted string (form image) to the webservice.
Decode that string with base64_decode function in the webservice.
Save the image from the decoded string, using file_put_contents.
Input the decoded string to the BLOB field in my database.
What make me confuse is, if i already saved the image file (step 4), do i still need to save it to the BLOB column (step 5)?
Now, i got confuse with the download part. This is what im planning to do :
Get the image file from step 4 in upload using file_get_contents (return string)
Send the result (string) to my android apps
Get the string and convert it to the image (how?)
Is it all i need to download?
Do i need to use base64_decode or base64_encode in download? When do i use it?
Sorry if my question sounds silly, i still dont get the logic.
Thanks a lot for your time, all help is appreciated.

What make me confuse is, if i already saved the image file (step 4), do i still need to save it to the BLOB column (step 5)?
No you don't. You can store in DB just a reference to that file, like filename or full path.
Now, i got confuse with the download part. This is what im planning to do :
Get the image file from step 4 in upload using file_get_contents (return string)
and base64_encode it
Get the string and convert it to the image (how?)
You need to base64 decode it, because you encode it before sending it from the server. (step 1)

Related

How to save image from android to java server

I need to upload an image from android app to a java server. I tried to define the type image like a string and to convert the image, that the user can upload from documents or take with camera, into a base64 string. But when i convert this image in base64 and put this inside a string to save it in database, server give me a 500 internal sever error and this error message:
"com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'image' at row 1"
The Db is in Mysql and i use this from java server.
So, how can I save a base64 image to my server? Can I use a different type?
Thank you!
you can use volley library. for more details these links are useful
first_link and second_link

Java as HTTP server - Is possible get a image via POST method?

I would like to know if is possible get the image via POST method with a HTTP server implemented in Java (With a simple input file form). I already implemented the Java server but I can only get text files via POST method it's because that the my application only copies the file content to another empty file creating the same file with the same characteristics. This does not work with image file or other files, this can only work with text file.
Anyone know how to implement it with images?
Some coordinates would be of great help!
Thanks in advance!
As far as i know you should create something like it:
Server-side: If you use a servlet that receive data in post you have to get the outputStream from the response. Once you have it it is done because you write the data image on the stream.
For example let's suppose your image is a file stored in the server you could do:
response.setContentLength((int) fileSize);
byte b[] = new byte[1024];
while ( fOutStream.read(b) != -1)
response.getOutputStream().write(b);
fOutStream.close() ;
Where the fOutStream is the source stream (your image).

Create Empty CloudBlockBlob in Azure

I'm hoping the answer to this question is quite simple, but I can't get it working after looking at the Azure Java API documentation.
I am trying to create an empty CloudBlockBlob, which will have blocks uploaded to it at a later point. I have successfully uploaded blocks before, when the blob is created upon the first block being uploaded, but I can't seem to get anything other than ("the specified blob does not exist") when I try to create a new blob without any data and then access it. I require this because in my service, a call is first made to create the new blob in Azure, and then later calls are used to upload blocks (at which point a check is made to see if the blob exists). Is it possible to create an empty blob in Azure, and upload data to it later? What have I missed?
I've not worked with Java SDK so I may be wrong but I tried creating an empty blob using C# code (storage client library 2.0) and if I upload an empty input stream an empty blob with zero byte size is created. I did something like the following:
CloudBlockBlob emptyBlob = blobContainer.GetBlockBlobReference("emptyblob.txt");
using (MemoryStream ms = new MemoryStream())
{
emptyBlob.UploadFromStream(ms);//Empty memory stream. Will create an empty blob.
}
I did look at Azure SDK for Java source code on Github here: https://github.com/WindowsAzure/azure-sdk-for-java/blob/master/microsoft-azure-api/src/main/java/com/microsoft/windowsazure/services/blob/client/CloudBlockBlob.java and found this "upload" function where you can specify an input stream. Try it out and see if it works for you.

edit LDAP Photo attribute, spring-ldap InvalidAttributeValueException

I develop an LDAP interface program which can modify person attributes, but when I try to modify the value of the photo attribute with an url string, I have this exception :
org.springframework.ldap.InvalidAttributeValueException: [LDAP: error code 21 - photo: no validator for syntax 1.3.6.1.4.1.1466.115.121.1.23];
I think I must send a JPG photo file to ldap but I don't know how to do it...
Anyone has an idea?
First, when you encounter such an error with a syntax OID, you can submit it to the OID repository. Here it will give you the following information "Values in this syntax are encoded as if they were octet strings".
This means that you have to encode your JPG or PNG file in Base64 and set the attribute with this value (in fact an array).
Second, in my understanding, there are 3 attributes to store photo-Data jpegPhoto, Photo and thumbnailPhoto. But for me it's better to store photos on a file system or a database and put in the Directory ans URL or something like that.
You'll find here a tutorial to handle them with java.

Running a JavaScript command from MATLAB to fetch a PDF file

I'm currently writing some MATLAB code to interact with my company's internal reports database. So far I can access the HTML abstract page using code which looks like this:
import com.mathworks.mde.desk.*;
wb=com.mathworks.mde.webbrowser.WebBrowser.createBrowser;
wb.setCurrentLocation(ReportURL(8:end));
pause(1);
s={};
while isempty(s)
s=char(wb.getHtmlText);
pause(.1);
end
desk=MLDesktop.getInstance;
desk.removeClient(wb);
I can extract out various bits of information from the HTML text which ends up in the variable s, however the PDF of the report is accessed via what I believe is a JavaScript command (onClick="gotoFulltext('','[Report Number]')").
Any ideas as to how I execute this JavaScript command and get the contents of the PDF file into a MATLAB variable?
(MATLAB sits on top of Java, so I believe a Java solution would work...)
I think you should take a look at the JavaScript that is being called and see what the final request to the webserver looks like.
You can do this quite easily in Firefox using the FireBug plugin.
https://addons.mozilla.org/en-US/firefox/addon/1843
Once you have found the real server request then you can just request this URL or post to this URL instead of trying to run the JavaScript.
Once you have gotten the correct URL (a la the answer from pjp), your next problem is to "get the contents of the PDF file into a MATLAB variable". Whether or not this is possible may depend on what you mean by "contents"...
If you want to get the raw data in the PDF file, I don't think there is a way currently to do this in MATLAB. The URLREAD function was the first thing I thought of to read content from a URL into a string, but it has this note in the documentation:
s = urlread('url') reads the content
at a URL into the string s. If the
server returns binary data, s will
be unreadable.
Indeed, if you try to read a PDF as in the following example, s contains some text intermingled with mostly garbage:
s = urlread('http://samplepdf.com/sample.pdf');
If you want to get the text from the PDF file, you have some options. First, you can use URLWRITE to save the contents of the URL to a file:
urlwrite('http://samplepdf.com/sample.pdf','temp.pdf');
Then you should be able to use one of two submissions on The MathWorks File Exchange to extract the text from the PDF:
Extract text from a PDF document by Dimitri Shvorob
PDF Reader by Tom Gaudette
If you simply want to view the PDF, you can just open it in Adobe Acrobat with the OPEN function:
open('temp.pdf');
wb=com.mathworks.mde.webbrowser.WebBrowser.createBrowser;
wb.executeScript('javascript:alert(''Some code from a link'')');
desk=com.mathworks.mde.desk.MLDesktop.getInstance;
desk.removeClient(wb);

Categories

Resources