1

I want to download one picture from url to my Lotus Notes application. I can get text field from url, but image is difficult. I try to put pic to a rich text field, but it doesn't work. Any idea?

Community
  • 1
  • 1
HuMaN
  • 55
  • 1
  • 7

1 Answers1

5

You can download an image from URL via LotusScript with the help of a little Script Library of type "Java".

Create a Script Library "GetImageFromUrl" of Type "Java" and put in following code:

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class GetImageFromUrl {

    public static boolean getImageFromUrl(String imageUrl, String filePath) {
        try {
            URL url = new URL(imageUrl);
            InputStream is = url.openStream();
            OutputStream os = new FileOutputStream(filePath);
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
            is.close();
            os.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

Then you can use the method getImageFromUrl(imageUrl, filePath) in your LotusScript code to download the image to a file. From there you can attach the image file to a RichText item with rtitem.EmbedObject(EMBED_ATTACHMENT, "", "c:/temp/image.jpg").

Option Declare

UseLSX "*javacon"  

Use "GetImageFromUrl"

Sub Initialize
    dim jSession As New JavaSession
    dim jClass As JavaClass
    Set jClass = jSession.GetClass( "GetImageFromUrl" )     
    If jClass.getImageFromUrl("https://your.url", "c:/temp/image.jpg") Then
        MessageBox "File is downloaded"
    End If
End Sub
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
  • I've noticed the filePath is given with forward slashes "/", is this necessary for Java, or is it acceptable to use the Windows convention with backslash "\"? – Sam Sirry Jul 30 '17 at 01:24