import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.WString;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.win32.StdCallLibrary;
/**
*
* @author Song Kaisong from Northeastern University
* @version 1.0
* I rewrite the C program demo from URL=http://thunderplatform.xunlei.com/ by java,we can
* easily use and extend the program function,including off-line download, continuous download from
* breakpoint and so on. We usually want to get resource such as text, image , sound track and video
* for data gaining, however the tools from JDK such as URL,URLConnection is not adaptable to gain sound
* and video data. So i try to utilize the bt fuction of XunLei by calling DLL and vastly improve download
* speed.
*/
public class XLDownLoader {
String FileName;
String Url;
long task;
/**
*
* @param FileName saved file path
* @param Url pre-downloaded url
* @param task taskid
*/
public XLDownLoader( String FileName,String Url,long task){
this.FileName=FileName;
this.Url=Url;
this.task=task;
}
public interface XLDownload extends StdCallLibrary {
public boolean XLInitDownloadEngine();
public int XLURLDownloadToFile(WString pszFileName, WString pszUrl, WString pszRefUrl, NativeLongByReference lTaskId);
public int XLQueryTaskInfo(NativeLong lTaskId, NativeLongByReference plStatus, LongByReference pullFileSize, LongByReference pullRecvSize);
public int XLPauseTask(long lTaskId, NativeLongByReference lNewTaskId);
public int XLContinueTask(long lTaskId);
public int XLContinueTaskFromTdFile(WString pszTdFileFullPath, NativeLongByReference lTaskId);
public void XLStopTask(long lTaskId);
public boolean XLUninitDownloadEngine();
}
/**
*
* @return 268435467 pre-downloaded file exist in specified file path
* -1 Fail to initialize(Fail to find DLL)
* 12 Succeed in downloading file of specified url
*/
public int startTask(){
XLDownload xldownload = (XLDownload) Native.loadLibrary("XLDownload",
XLDownload.class);
if(false==xldownload.XLInitDownloadEngine()){
System.out.println("Initialize download engine failed");
return -1;
}
WString pszFileName=new WString(FileName);
WString pszUrl=new WString(Url);
WString pszRefUrl=new WString("");
NativeLong TaskId=new NativeLong(task);
NativeLongByReference lTaskId=new NativeLongByReference(TaskId);
int dwRet=xldownload.XLURLDownloadToFile(pszFileName,pszUrl ,pszRefUrl, lTaskId);
do{
LongByReference ullFileSize =new LongByReference(0);
LongByReference ullRecvSize =new LongByReference(0);
NativeLongByReference lStatus=new NativeLongByReference(new NativeLong(-1L));
dwRet=xldownload.XLQueryTaskInfo(TaskId,lStatus,ullFileSize,ullRecvSize);
if(0==dwRet){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if ( 0 != ullFileSize.getValue() )
{
double douProgress = (double)ullRecvSize.getValue()/(double)ullFileSize.getValue();
douProgress *= 100.0;
System.out.println(FileName+"Download progress:"+douProgress+"%");
}
else
{
System.out.println("File size is zero.\n");
}
if (11==lStatus.getValue().intValue())
{
System.out.println("Download successfully.\n");
break;
}
if ( 12==lStatus.getValue().intValue() )
{
System.out.println("Download failed.\n");
break;
}
}
}while(0==dwRet);
xldownload.XLStopTask(this.task);
xldownload.XLUninitDownloadEngine();
return 12;
}
public static void main(String[] args){
String FileName="d:\\1-29.mp3";
String Url="http://audio.soundtrack.net/0/0/0/1/1-29.mp3";
long id=0L;
XLDownLoader instance=new XLDownLoader(FileName,Url,id);
instance.startTask();
}
}
- 1
- 2
- 3
- 4
前往页