import java.io.*;
import java.net.*;
public class myDownload{
public float getSize(){
return fileSize;
}
public myDownload(){
mainDl();
}
public static void fileUrl(String fAddress, String
localFileName, String destinationDir) {
try {
Url= new URL(fAddress);
outStream = new BufferedOutputStream(new
FileOutputStream(destinationDir+"\\"+localFileName));
uCon = Url.openConnection();
fileSize=uCon.getContentLength();
//System.out.println("size is"+ tt/1048576);
is = uCon.getInputStream();
buf = new byte[size];
while ((ByteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, ByteRead);
ByteWritten += ByteRead;
}
System.out.println("Downloaded Successfully.");
System.out.println("File name:\""+localFileName+ "\"\nNo ofbytes :" + ByteWritten);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
outStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
synchronized public static void fileDownload()
{
System.out.println("filedownload");
fAddress=dmGUI.getLink();
destinationDir=dmGUI.getLoc();
int slashIndex =fAddress.lastIndexOf('/');
int periodIndex =fAddress.lastIndexOf('.');
String fileName=fAddress.substring(slashIndex + 1);
if (periodIndex >=1 && slashIndex >= 0 && slashIndex < fAddress.length()-1){
fileUrl(fAddress,fileName,destinationDir);
}
else{
System.err.println("path or file name.");
}
}
public static void mainDl(){
new Thread(
new Runnable(){
public void run(){
fileDownload();
}
}).start();
}
final static int size=1024;
static byte buf[];
static OutputStream outStream = null;
static URLConnection uCon = null;
static InputStream is = null;
static int ByteRead,ByteWritten=0;
static URL Url;
static float fileSize=0.0f;
static String fAddress;
static String destinationDir;
}