Reputation: 1066
What is the best way to upload image files to web server in java considering IOS9 and swift 2.
Upon searching, I realised that similar question was asked and many different methods are available online to upload images. However I am not sure on what is the best method to follow.
I tried the following approaches and noticed that it takes about a minute to upload an image.
****Approach 1:****
Using NSURLSession and invoking “POST” method of the java servlet Send as JSON as stated in this example
Append image to httpbody itself as stated in this example
****Approach 2:****
Using NSURL and invoking a javascript method as in this example
I also read about AFNetworking 2.0 (ex) and I am not sure if it can bring any performance increase to the uploading process.
Our users might take around 100 of pictures everyday and the images needs to be uploaded to web server for further processing.
Please review and suggest your views. Many thanks.
Upvotes: 1
Views: 2871
Reputation: 51
Create base64 String out of your image and send it to web server.
let imageData = UIImageJPEGRepresentation(imageURL.image!, 0.5)
let base64String = imageData!.base64EncodedStringWithOptions([])
In your web server, read the input image string and parse it as follows.
byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);
Then you can do whatever you like with the bytes like:
BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));
Upvotes: 1
Reputation: 131
I actually work on a social app and to upload a photo on the API i personally use the base64.
Simply i encode the image, so i have a encoded string and the API decoded and put it on a AWS.
I am on Swift 2.0 and iOS 9. If you want a piece of code :
// You create a NSData from your image
var imageData = UIImageJPEGRepresentation(imageURL.image!, 0.5)
// You create a base64 string
let base64String = imageData!.base64EncodedStringWithOptions([])
// And you encode it in order to delete any problem of specials char
let encodeImg = base64String.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) as String!
Upvotes: 1