java调用下载窗口_java jna调用迅雷接口下载

该博客介绍了如何利用Java的JNA库调用迅雷的DLL接口进行文件下载。通过初始化下载引擎,设置文件路径和URL,然后调用下载方法并监控下载进度,直到下载完成。注意此方法不支持多线程,且依赖32位JDK、Windows环境以及XLDownload.dll和zlib1.dll库。
摘要由CSDN通过智能技术生成

---> 不支持多线程

---> 依赖32位jdk,windows 和jna.jar

---> 依赖 XLDownload.dll, zlib1.dll  放置项目跟目录。不是src下,可以为绝对路径

package com.leunpha;

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;

import java.util.*;

import java.util.concurrent.*;

import java.util.concurrent.atomic.AtomicInteger;

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(String link){

XLDownload xldownload = (XLDownload) Native.loadLibrary(link,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){

Map info = new HashMap();

info.put("e:\\temp\\18ec54e736d12f2ee6d124c84dc2d56284356846.jpg","http://h.hiphotos.bdimg.com/wisegame/pic/item/18ec54e736d12f2ee6d124c84dc2d56284356846.jpg");

info.put("e:\\temp\\9bdcd100baa1cd11d028f888bb12c8fcc2ce2df8.jpg","http://c.hiphotos.bdimg.com/wisegame/pic/item/9bdcd100baa1cd11d028f888bb12c8fcc2ce2df8.jpg");

info.put("e:\\temp\\2410b912c8fcc3cef9f7d0ab9045d688d43f2076.jpg","http://c.hiphotos.bdimg.com/wisegame/pic/item/2410b912c8fcc3cef9f7d0ab9045d688d43f2076.jpg");

info.put("e:\\temp\\579b033b5bb5c9eadd791d7bd739b6003bf3b3a4.jpg","http://a.hiphotos.bdimg.com/wisegame/pic/item/579b033b5bb5c9eadd791d7bd739b6003bf3b3a4.jpg");

info.put("e:\\temp\\aaea15ce36d3d5397db7115b3887e950342ab0a4.jpg","http://d.hiphotos.bdimg.com/wisegame/pic/item/aaea15ce36d3d5397db7115b3887e950342ab0a4.jpg");

info.put("e:\\temp\\abfcc3cec3fdfc03ae4ffa20d63f8794a4c22676.jpg","http://e.hiphotos.bdimg.com/wisegame/pic/item/abfcc3cec3fdfc03ae4ffa20d63f8794a4c22676.jpg");

info.put("e:\\temp\\f145d688d43f8794e4c851b6d01b0ef41bd53a76.jpg","http://a.hiphotos.bdimg.com/wisegame/pic/item/f145d688d43f8794e4c851b6d01b0ef41bd53a76.jpg");

// final AtomicInteger ai = new AtomicInteger(1);

// ExecutorService service = Executors.newFixedThreadPool(1);

// int i=1;

// List futures = new ArrayList();

for(final Map.Entry entry: info.entrySet()){

//i++;

XLDownLoader instance=new XLDownLoader(entry.getKey(),entry.getValue(),0);

instance.startTask(String.format("XLDownload",1));

/*service.execute(new Runnable() {

@Override

public void run() {

com.leunpha.XLDownLoader instance=new com.leunpha.XLDownLoader(entry.getKey(),entry.getValue(),ai.get());

instance.startTask("E:\\xl\\"+ai.getAndIncrement()+"\\XLDownload.dll");

}

});

futures.add(service.submit(new Callable() {

@Override

public Object call() throws Exception {

return null;

}

}));*/

}

/* service.shutdown();

for(Future future:futures){

try {

future.get();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ExecutionException e) {

e.printStackTrace();

}

}*/

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值