How can I send a picture in Java ME? - image

How can I send a picture in Java ME ? Maybe using base64 decode and the send in post form via http request

I think you looking for this image base64 encoding in J2me

Yes , I'm use that to send the base64 ,putting in
// write your picture data to os
os.write (data);
where data = String with a base64 image , the problem is that i don't have a base64 library that works in java and asp in the same way.

Have you considered using a "multipart/form-data" encoding type? If you so, then there is no need for Base64 encoding. See here and here for instructions on how to build the request.
If you still want to do the Base64 encoding, then you will be able to easily find many source implementations on the Web. If you are developing both the server and client side, you could use your own implementation on both ends.

There are plenty of coders in Base64 for Java. Apache commons has one, here is a standalone one.

HttpConnection connection = (HttpConnection) Connector.open("http://www.myserver.com");
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("Content-Type","image/png");
OutputStream os = connection.getOutputStream();
// write your picture data to os
os.flush();
if (connection.getResponseCode() == HttpConnection.HTTP_OK)
{

Related

How to create FileUpload

I'm currently trying to send an image through JDA but can't get it to work. The only thing I can find on Google is the addFile (java.io.File) method, which is no longer available in the most recent version of JDA. I am currently struggling to find a solution using the addFiles() method.
This is my current attempt.
byte[] buffer = ((DataBufferByte)(img).getRaster().getDataBuffer()).getData();
FileUpload upload = new FileUpload(new ByteArrayInputStream(buffer));
c.sendMessage("Image:").addFiles(upload).complete();
I am getting this error message in my IDE, but I don't know what to make of it.
thanks guys.
You have to use the factory method FileUpload.fromData:
FileUpload upload = FileUpload.fromData(buffer, "image.png");
Make sure you properly encode the image to something like JPG or PNG, otherwise this will not work.

How to download file in Xamarin.Forms that are in Base64 format?

I am creating an application that download and upload attachment from the Xamarin.Forms application.
I am using .Net standard project and try to use with the "CrossDownloadManager" Nuget but it is not compatible and other reference for download file are using url but in this situation I have base64byte string.
I also try with This reference but it doesn't work for me.
Application works on Android and iOS.
I attached sample response Here:
Can anyone look into this and suggest me what should I have to do in that?
download a file given a url
var client = new HttpClient();
var data = await client.GetStringAsync(url);
if data is Base64, decode it
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
var decoded = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
save decoded string
File.WriteAllText(filepath, decoded);

How to send an email with multiple attachments from Gmail using API client library for .NET

My app uses Google API client library for .NET to send emails with attachments.
When using Send(), I'm facing some limitations when it comes to file size of the attachments. So, I guess switching to Resumable upload as upload method may help. But it's pretty much undocumented.
Looking into source code, I guess using different Send() overload may be the way forward, but I can't figure out how to use it properly.
So, instead of attaching the files into message and calling it like this:
var gmailResult = gmail.Users.Messages.Send(new Message
{
Raw = base64UrlEncodedMessage
}, "me").Execute();
I should not attach the files to message and do something like following?
var gmailResult = gmail.Users.Messages.Send(new Message
{
Raw = base64UrlEncodedMessage
}, "me", fileStream, contentType).Upload();
The second version does not return any API error, but does nothing. I'm obviously missing something here.
How do I attach more than one attachment?
This is kind of an old question, but putting an answer here just in case anyone else needs it:
I was able to achieve this by converting my mime message into a stream (attachment(s) included), and then calling this overload on Send:
UsersResource.MessagesResource.SendMediaUpload googleSendRequest = service.Users.Messages.Send(null, "youremail#gmail.com", mimeMessageStream, "message/rfc822");
IUploadProgress created = googleSendRequest.Upload();
This will upload all of the attachments with the email message content and then send it off. I was able to send two 5 megabyte attachments in an email. Previously I was not able to send even one of those via the other Send method that takes in a base64 encoded mime message.

Loading and saving JSON files in a GWT web application

We are developing an GWT web-application with an Python webapp2 webserver. At this point we want to load and save files within the web-application. We cannot use Flash for this task.
Saving
The current approach is to use a form upload using the target "_blank" and set the correct MIME to make the browser download the file. This solution works, but since the webapp2 webserver does not support streaming (thus the mime type cannot be validated by the browser in a short time), a new browser-window is opened each time. Is there a better solution e.g. using iFrames?
Loading
Again using form upload and parsing the response (JSON). We use the content-type "text/html; charset=UTF-8". This solution works perfectly in IE9 but does not work in Chrome and FireFox. It seems, that the JSON response gets corrupted in some way or is there anything from when parsing a JSON response from a form upload response? We use piriti for JSON (de)serialization.
For loading, you can use RestyGWT library, it has capability encoding or decoding Java Object to JSON:
import javax.ws.rs.POST;
...
public interface PizzaOrderCodec extends JsonEncoderDecoder<PizzaOrder> {
}
// GWT will implement the interface for you
PizzaOrderCodec codec = GWT.create(PizzaOrderCodec.class);
// Encoding an object to json
PizzaOrder order = ...
JSONValue json = codec.encode(order);
// decoding an object to from json
PizzaOrder other = codec.decode(json);
For saving, if you have to send JSON file using file upload, you can see following link:
http://www.jroller.com/hasant/entry/fileupload_with_gwt
http://www.celinio.net/techblog/?p=1207
Have a nice time.

Can't download from youtube encoding error

it's a week that I'm trying to write a program that download correctly from youtube
a video
the problem is this:
I've done via httpwebrequest a simple downloader and all work well.
I've tried download a mp4 video from a local server (uniform server) and all goes well, video is the same, work and is valid mp4.
When I download the same video from youtube something strange happens:
It fill some characters in downloaded file with sequence EF BF BD
for example when encounter character E2 (I've compared the two files one from youtube and the original). Googling around I discover that a server return this kinda sequence when encounterd a invalid char for your encoding, now I'm on wp7 and I can't set
Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.7
here's others useful data: I've try with these:
_webRequest.Method = "GET"; //also POST
_webRequest.UserAgent = #"Mozilla/5.0 (Windows; U; Windows NT 6.1; it; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16";
_webRequest.Accept = #"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
_webRequest.BeginGetResponse(_onDownload, userState);
format url from youtube
Any Idea?
thanks
Can you set the header using the _webRequest.Headers method? See these related questions:
Cannot set some HTTP headers when using System.Net.WebRequest
Constructing a POST in C#

Resources