0

Friends,

I have to upload an image from IOS to Java web server. So, my IOS program, encodes the image to Base64 Image format and encode it in order to delete any problem of specials char as follows.

    let imageData = UIImageJPEGRepresentation(myImageView.image!, 0.5)
    let base64String = imageData!.base64EncodedStringWithOptions([])
    let encodeImg = base64String.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) as String!

The encoded image data string is then sent to the web server as a JSON object.

    let params = ["image":[ "content_type": "image/jpeg", "filename":"testing", "file_data": base64String]]

    do {
        let postBody = try NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.init(rawValue: 0))
        request.HTTPBody = postBody

        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            print("Response: \(response)")})
        task.resume()

    } catch {
        print(error)
    }

Now the Java server decodes the base64 image data string to image as

InputStream stream = new ByteArrayInputStream(Base64.decode(image.getBytes(), Base64.DEFAULT));

and write the stream to a image.jpeg file. When we try to open the file, we get file format not supported error.

When I tried to print the Base64 encoded image data string, I could find that it is as follows.

%2F9j%2F4AAQSkZJRgABAQAASABIAAD%2F4QBMRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAAqACAAQAAAABAAAQwKADAAQAAAABAAALIAAAAAD%2F7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+%2F+ICQElDQ19QUk9GSUxFAAEBAAACMEFEQkUCEAAAbW50clJHQiBYWVogB9AACAALABMAMwA7YWNzcEFQUEwAAAAAbm9uZQAAAAAAAAAAAAAAAAAAAAAAAPbWAAEAAAAA0y1BREJFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKY3BydAAAAPwAAAAyZGVzYwAAATAAAABrd3RwdAAAAZwAAAAUYmtwdAAAAbAAAAAUclRSQwAAAcQAAAAOZ1RSQwAAAdQAAAAOYlRSQwAAAeQAAAAOclhZWgAAAfQAAAAUZ1hZWgAAAggAAAAUYlhZWgAAAhwAAAAUdGV4dAAAAABDb3B5cmlnaHQgMjAwMCBBZG9iZSBTeXN0ZW1zIEluY29ycG9yYXRlZAAAAGRlc2MAAAAAAAAAEUFkb2JlIFJHQiAoMTk5OCkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAADzUQABAAAAARbMWFlaIAAAAAAAAAAAAAAAAAAAAABjdXJ2AAAAAAAAAAECMwAAY3VydgAAAAAAAAABAjMAAGN1cnYAAAAAAAAAAQIzAABYWVogAAAAAAAAnBgAAE+lAAAE...

If we can note, it contains many percentage"%" symbols in it as I used

base64String.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()

in swift before uploading.

Can someone guide me in decoding the percentage encoded image string.

I went through the link on Base64 data decoding in Java. With that we can decode base64 strings but how to go about percentage encoded strings.

I would like to learn on decoding percentage encoded strings. Please suggest your views. Meanwhile, I try to upload image without percentage encoding and see if I can decode it successfully in Java.

Many thanks in advance.

Community
  • 1
  • 1
PK20
  • 1,066
  • 8
  • 19

2 Answers2

1

Your last line in swift that i gave you is for URL Encode. So, your server must in order : URL Decode, Base64 Decode and put on JPG.

nekflamm
  • 131
  • 12
1

Do not percentage encode image Strings. You may directly send as base64EncodedString. Java can easily decode it.

Void
  • 51
  • 3