Hi!
I’ll like to know if there is some Java code to download a file given a URL (e.i http://www.serverlotus.com/dir/file.doc)
Thanks
Sonny
Hi!
I’ll like to know if there is some Java code to download a file given a URL (e.i http://www.serverlotus.com/dir/file.doc)
Thanks
Sonny
Subject: download a file
I WROTE THE FOLLOWING JAVA LIBRARY THEN I CALLED THIS LIBRARY FROM LOTUS NOTES AGENT (LS SCRIPT)
import java.util.*;
import java.net.*;
import java.io.*;
public class GetFileFromURL
{
public String DownLoadFile(String SProxy,int IPort, String SUrl, String SUserName, String SPassword, String SOutFPath)
{
String OutputString;
try {
URL url;
if(SProxy.length() > 0) {
url = new URL("http",SProxy,IPort,SUrl);
}
else {
url = new URL(SUrl);
}
URLConnection connection = url.openConnection();
if (SProxy.length()>0) {
connection.setRequestProperty("Proxy-Authorization", "Basic " + Base64Converter.encode(SUserName + ":" + SPassword));
}
InputStream stream = connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(stream);
FileOutputStream file = new FileOutputStream(SOutFPath);
BufferedOutputStream out = new BufferedOutputStream(file);
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
out.flush();
OutputString="Done";
} catch(Exception e) {
OutputString="Error occured while posting the data";
//e.printStackTrace();
}
return OutputString;
}
static class Base64Converter
{
/**
* Constants for conversion.
*
*/
public static final char alphabet[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/'
};
/**
* Creates a new <code>Base64Converter</code> instance.
*
*/
Base64Converter()
{
}
/**
* Encodes the passed string to Base64.
*
* @param s a <code>String</code> value
* @return a <code>String</code> value
*/
public static String encode(String s)
{
return encode(s.getBytes());
}
public static String encode(byte octetString[])
{
char out[] = new char[((octetString.length - 1) / 3 + 1) * 4];
int outIndex = 0;
int i;
for(i = 0; i + 3 <= octetString.length;)
{
int bits24 = (octetString[i++] & 0xff) << 16;
bits24 |= (octetString[i++] & 0xff) << 8;
bits24 |= (octetString[i++] & 0xff) << 0;
int bits6 = (bits24 & 0xfc0000) >> 18;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0x3f000) >> 12;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0xfc0) >> 6;
out[outIndex++] = alphabet[bits6];
bits6 = bits24 & 0x3f;
out[outIndex++] = alphabet[bits6];
}
if(octetString.length - i == 2)
{
int bits24 = (octetString[i] & 0xff) << 16;
bits24 |= (octetString[i + 1] & 0xff) << 8;
int bits6 = (bits24 & 0xfc0000) >> 18;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0x3f000) >> 12;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0xfc0) >> 6;
out[outIndex++] = alphabet[bits6];
out[outIndex++] = '=';
}
else
if(octetString.length - i == 1)
{
int bits24 = (octetString[i] & 0xff) << 16;
int bits6 = (bits24 & 0xfc0000) >> 18;
out[outIndex++] = alphabet[bits6];
bits6 = (bits24 & 0x3f000) >> 12;
out[outIndex++] = alphabet[bits6];
out[outIndex++] = '=';
out[outIndex++] = '=';
}
return new String(out);
}
}
}
GOOD LUCK
THANKS
Subject: RE: download a file
It works OK
Thanks Johon