Skip to contentSkip to navigationSkip to topbar
On this page

Receive and Download Images on Incoming Media Messages with Java


You know how to receive and reply to incoming SMS messages. What if you receive an MMS message containing an image you'd like to download? Let's learn how we can grab that image and any other incoming MMS media using Java.


Create MMS processing project

create-mms-processing-project page anchor

When Twilio receives a message for your phone number, it can make an HTTP call to a webhook that you create. The easiest way to handle HTTP requests in Java is to use Spark web framework(link takes you to an external page).

Twilio expects, at the very least, for your webhook to return a 200 OK response if everything is peachy. Often, however, you will return some TwiML in your response as well. TwiML is just a set of XML commands telling Twilio how you'd like it to respond to your message. Rather than manually generating the XML, we'll use the twilio helper library to facilitate generating TwiML and the rest of the webhook plumbing.


Receive MMS message and images

receive-mms-message-and-images page anchor

Get incoming message details

get-incoming-message-details page anchor

When Twilio calls your webhook, it sends a number of parameters about the message you just received.

Most of these, such as the To phone number, the From phone number, and the Body of the message are available as properties of the request parameter to the Spark views.

Get number of attachments

get-number-of-attachments page anchor

We may receive more than one media per message, this parameter informs us how many we received. We used a custom class parseBody to get the value and cast it to an Integer, to be used in a following loop.

1
Map<String, String> parameters = parseBody(req.body());
2
String numMediaStr = parameters.get("NumMedia");
3
int numMedia = Integer.parseInt(numMediaStr);

Since an MMS message can have multiple attachments, Twilio will send us form variables named MediaUrlX, where X is a zero-based index. So, for example, the URL for the first media attachment will be in the MediaUrl0 parameter, the second in MediaUrl1, and so on.

In order to handle a dynamic number of attachments, we loop through all the available URLs:

1
while (numMedia > 0) {
2
numMedia = numMedia - 1;
3
String mediaUrl = parameters.get(String.format("MediaUrl%d", numMedia));
4
}

Determine content type of media

determine-content-type-of-media page anchor

Attachments to MMS messages can be of many different file types. JPG(link takes you to an external page) and GIF(link takes you to an external page) images as well as MP4(link takes you to an external page) and 3GP(link takes you to an external page) files are all common. Twilio handles the determination of the file type for you and you can get the standard mime type from the MediaContentTypeX parameter. If you are expecting photos, then you will likely see a lot of attachments with the mime type of image/jpeg.

1
while (numMedia > 0) {
2
numMedia = numMedia - 1;
3
String mediaUrl = parameters.get(String.format("MediaUrl%d", numMedia));
4
String contentType = parameters.get(String.format("MediaContentType%d", numMedia));
5
}

Depending on your use case, storing the URLs to the images (or videos or whatever) may be all you need. There's two key features to these URLs that make them very pliable for your use in your apps:

  1. They are publicly accessible without any need for authentication to facilitate sharing.
  2. They are permanent (unless you explicitly delete the media, see later).

For example, if you are building a browser-based app that needs to display the images, all you need to do is drop an <img src="twilio url to your image"> tag into the page. If this works for you, then perhaps all you need is to store the URL in a database character field.

Save media to local file system

save-media-to-local-file-system page anchor

If you want to save the media attachments to a file, then you will need to make an HTTP request to the media URL and write the response stream to a file. If you need a unique filename, you can use the last part of the media URL. For example, suppose your media URL is the following:

https://api.twilio.com/2010-04-01/Accounts/ACxxxx/Messages/MMxxxx/Media/ME27be8a708784242c0daee207ff73db67

You can use that last part of the URL as a unique filename. Figuring out a good extension to use is a little tricker. If you are only expecting images, you could just assume a ".jpg" extension. For a little more flexibility, you can lookup the mime type and determine a good extension to use based on that.

Here's the complete code for our controller that saves each MMS attachment to the App_Data folder:

Saves MMS attachment with Java

saves-mms-attachment-with-java page anchor
1
package com.twilio.app;
2
3
import static spark.Spark.*;
4
5
import java.util.Map;
6
import java.util.HashMap;
7
import java.io.File;
8
import java.io.InputStream;
9
import java.io.UnsupportedEncodingException;
10
import java.net.URL;
11
import java.net.URLDecoder;
12
13
import org.apache.tika.mime.MimeType;
14
import org.apache.tika.mime.MimeTypes;
15
16
import org.apache.commons.io.FileUtils;
17
import org.apache.http.HttpResponse;
18
import org.apache.http.client.methods.HttpGet;
19
import org.apache.http.impl.client.CloseableHttpClient;
20
import org.apache.http.impl.client.HttpClients;
21
import org.apache.http.impl.client.LaxRedirectStrategy;
22
23
import com.twilio.twiml.MessagingResponse;
24
import com.twilio.twiml.messaging.Body;
25
import com.twilio.twiml.messaging.Message;
26
27
public class App {
28
public static void main(String[] args) {
29
post("/sms", (req, res) -> {
30
Map<String, String> parameters = parseBody(req.body());
31
String numMediaStr = parameters.get("NumMedia");
32
int numMedia = Integer.parseInt(numMediaStr);
33
34
if (numMedia > 0) {
35
while (numMedia > 0) {
36
numMedia = numMedia - 1;
37
38
// Get all info
39
String mediaUrl = parameters.get(String.format("MediaUrl%d", numMedia));
40
String contentType = parameters.get(String.format("MediaContentType%d", numMedia));
41
String fileName = mediaUrl.substring(mediaUrl.lastIndexOf("/") + 1);
42
MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
43
MimeType mimeType = allTypes.forName(contentType);
44
String fileExtension = mimeType.getExtension();
45
File file = new File(fileName + fileExtension);
46
47
// Download file
48
URL url = new URL(mediaUrl);
49
CloseableHttpClient httpclient = HttpClients.custom()
50
.setRedirectStrategy(new LaxRedirectStrategy())
51
.build();
52
HttpGet get = new HttpGet(url.toURI());
53
HttpResponse response = httpclient.execute(get);
54
InputStream source = response.getEntity().getContent();
55
FileUtils.copyInputStreamToFile(source, file);
56
}
57
}
58
59
// Send message back
60
String message = (numMedia > 0) ? String.format("Thanks for sending us %s file(s)!", numMedia) : "Send us an image!";
61
res.type("application/xml");
62
Body body = new Body
63
.Builder(message)
64
.build();
65
Message sms = new Message
66
.Builder()
67
.body(body)
68
.build();
69
MessagingResponse twiml = new MessagingResponse
70
.Builder()
71
.message(sms)
72
.build();
73
return twiml.toXml();
74
75
});
76
}
77
78
// Body parser help
79
public static Map<String, String> parseBody(String body) throws UnsupportedEncodingException {
80
String[] unparsedParams = body.split("&");
81
Map<String, String> parsedParams = new HashMap<String, String>();
82
for (int i = 0; i < unparsedParams.length; i++) {
83
String[] param = unparsedParams[i].split("=");
84
if (param.length == 2) {
85
parsedParams.put(urlDecode(param[0]), urlDecode(param[1]));
86
} else if (param.length == 1) {
87
parsedParams.put(urlDecode(param[0]), "");
88
}
89
}
90
return parsedParams;
91
}
92
93
public static String urlDecode(String s) throws UnsupportedEncodingException {
94
return URLDecoder.decode(s, "utf-8");
95
}
96
}

Another idea for these image files could be uploading them to a cloud storage service like Azure Blob Storage(link takes you to an external page) or Amazon S3(link takes you to an external page). You could also save them to a database, if necessary. They're just regular files at this point. Go crazy.

Delete media from Twilio

delete-media-from-twilio page anchor

If you are downloading the attachments and no longer need them to be stored by Twilio, you can delete them by sending an HTTP DELETE request to the media URL. You will need to be authenticated to do this. The code sample below demonstrates how to make this request.

Delete a MediaLink to code sample: Delete a Media
1
// Install the Java helper library from twilio.com/docs/java/install
2
3
import com.twilio.Twilio;
4
import com.twilio.rest.api.v2010.account.message.Media;
5
6
public class Example {
7
// Find your Account SID and Auth Token at twilio.com/console
8
// and set the environment variables. See http://twil.io/secure
9
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
10
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
11
12
public static void main(String[] args) {
13
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
14
Media.deleter("MM800f449d0399ed014aae2bcc0cc2f2ec", "ME557ce644e5ab84fa21cc21112e22c485")
15
.delete();
16
}
17
}
(warning)

Protect your webhooks

Twilio supports HTTP Basic and Digest Authentication. Authentication allows you to password protect your TwiML URLs on your web server so that only you and Twilio can access them. Learn more about HTTP authentication and validating incoming requests here.


All the code, in a complete working project, is available on GitHub(link takes you to an external page). If you need to dig a bit deeper, you can head over to our API Reference and learn more about the Twilio webhook request and the Media resource. Also, you will want to be aware of the pricing(link takes you to an external page) for storage of all the media files that you keep on Twilio's servers.

We'd love to hear what you build with this.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.