Converting url data to base 64
I'm assuming you have some way to handle other image formats, like .bmp or .gif; if not to actually handle them, at least a way to tell the user "this service does not support that image type" or something of the sort.



public Object getAsObject(FacesContext arg0, UIComponent arg1, String urlStr) {


Looks like you've got some overhead in there, you don't use the arg0 or arg1, why are they in here? Why is your method name getAsObject when instead you're doing something like downloadImageFromUrlToBase64?

If your framework is troubling you like this, consider putting the actual work in a separate class or function and having a simple method that only passes what you need. That way you're less attached to the framework which you are not using.



String extension = urlStr.substring(urlStr.lastIndexOf(".")+1);


Here you have a bug; if I pass "jpeg" in here, you say 'the last index of "." is non-existent, so the return value will be -1, add one, is 0'. Meaning we substring from the start of the string, returning the full string.

You'll silently swallow the resulting MalformedURLException, so if there is going to be any feedback to the user, it's going to be along the lines of "something went wrong".



            final URL url = new URL(urlStr);
            final HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");

            // I'm not sure I should be doing that here
            BufferedImage image = ImageIO.read(connection.getInputStream());

            urlImage.setBuffImg(image);

            connection.disconnect();


Here you indeed have the option of moving this to a separate method. I think it would help.

private BufferedImage downloadImageFromUrl(String url){ ... }




String str;

os = new ByteArrayOutputStream();
ImageIO.write(image, extension, Base64.getEncoder().wrap(os));
str =  os.toString(StandardCharsets.ISO_8859_1.name());


This looks like it has seen some heavy editing. There's a variable str declared, but oh wait, gotta do something else first, namely filling the buffer, and only then can we put it in the string. There's also a double space between the = and os.toString, which means (to me) that you should probably run your autoformatter more often or configure it to fix double spaces like that.

Anyway, there's no need to create a local variable before you're going to use it in this case, so just reorder the statements:

os = new ByteArrayOutputStream();
ImageIO.write(image, extension, Base64.getEncoder().wrap(os));
String str = os.toString(StandardCharsets.ISO_8859_1.name());


And give the local a better name too, I almost thought you were appending 'ISO_8859_1' to your urlImage.

os = new ByteArrayOutputStream();
ImageIO.write(image, extension, Base64.getEncoder().wrap(os));
String imageInBase64 = os.toString(StandardCharsets.ISO_8859_1.name());




Lastly, I don't know what you're doing with that user-agent string, but I don't think you're 4 browsers at once and it probably needs a comment.