问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/17484864/picture-download-from-url-via-lotus-script