Skip to content

Commit caf3100

Browse files
committed
实现下载功能
1 parent cb42a4b commit caf3100

File tree

3 files changed

+178
-1
lines changed

3 files changed

+178
-1
lines changed

conf/nbs.db

0 Bytes
Binary file not shown.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package io.ipfs.nbs;
2+
3+
import com.nbs.entity.IPFSFileEntity;
4+
import com.sun.xml.internal.bind.v2.util.ByteArrayOutputStreamEx;
5+
import io.ipfs.api.IPFS;
6+
import io.ipfs.multihash.Multihash;
7+
import io.nbs.client.Launcher;
8+
import io.nbs.client.cnsts.AppGlobalCnst;
9+
import io.nbs.commons.helper.ConfigurationHelper;
10+
import org.apache.commons.lang3.StringUtils;
11+
12+
import java.io.*;
13+
import java.util.concurrent.ArrayBlockingQueue;
14+
import java.util.concurrent.BlockingQueue;
15+
import java.util.concurrent.TimeUnit;
16+
17+
/**
18+
* @Package : io.ipfs.nbs
19+
* @Description : <p></p>
20+
* @Author : lambor.c
21+
* @Date : 2018/8/2-15:10
22+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
23+
* All rights reserved.
24+
*/
25+
public class DownloadThreadBooster {
26+
public static final int MAX_SIZE = 10;
27+
private BlockingQueue<IPFSFileEntity> queue ;
28+
private String DEF_PATH;
29+
private static String savePath;
30+
private boolean running =false;
31+
private DWConsumer dwConsumer;
32+
33+
private IPFS ipfs1;
34+
35+
private DownloadThreadBooster(){
36+
DEF_PATH = AppGlobalCnst.consturactPath(Launcher.appBasePath,"download");
37+
savePath = DEF_PATH;
38+
File desk = new File(savePath);
39+
if(!desk.exists())desk.mkdirs();
40+
dwConsumer = new DWConsumer();
41+
queue = new ArrayBlockingQueue<>(MAX_SIZE);
42+
}
43+
44+
private static class ThreadBoosterHold {
45+
private static DownloadThreadBooster dtb = new DownloadThreadBooster();
46+
}
47+
48+
public static DownloadThreadBooster getInstance(){
49+
return ThreadBoosterHold.dtb;
50+
}
51+
52+
public BlockingQueue<IPFSFileEntity> getQueue() {
53+
return queue;
54+
}
55+
56+
public String getSavePath() {
57+
return savePath;
58+
}
59+
60+
public void setSavePath(String savePath) {
61+
this.savePath = savePath;
62+
}
63+
64+
/**
65+
*
66+
*/
67+
class DWConsumer extends Thread{
68+
@Override
69+
public void run() {
70+
download();
71+
}
72+
73+
/**
74+
* 下载
75+
*/
76+
private void download(){
77+
while (true){
78+
try {
79+
IPFSFileEntity entity = queue.take();
80+
IPFS ipfs = getIpfs();
81+
String hash58 = entity.getHash();
82+
String fileName = StringUtils.isBlank(entity.getSaveName()) ? entity.getHash()+(entity.getSuffix()==null?"":entity.getSuffix()) : entity.getSaveName();
83+
ByteArrayOutputStream baos=null;
84+
FileOutputStream fos = null;
85+
try {
86+
Multihash multihash = Multihash.fromBase58(hash58);
87+
byte[] datas = ipfs.get(multihash);
88+
long ts = System.currentTimeMillis();
89+
String tmpFileName = AppGlobalCnst.consturactPath(savePath,fileName);
90+
File tmpFile = new File(tmpFileName);
91+
if(tmpFile.exists()){
92+
tmpFile.delete();
93+
}
94+
tmpFile.createNewFile();
95+
baos = new ByteArrayOutputStream();
96+
baos.write(datas);
97+
98+
fos = new FileOutputStream(tmpFile);
99+
100+
baos.writeTo(fos);
101+
// DataOutputStream dos = new DataOutputStream(new FileOutputStream(tmpFile));
102+
fos.flush();
103+
File saveFile = new File(AppGlobalCnst.consturactPath(savePath,fileName+".mp4"));
104+
if(saveFile.exists())saveFile.delete();
105+
tmpFile.renameTo(saveFile);
106+
System.out.println("success download...");
107+
} catch (IOException e) {
108+
e.printStackTrace();
109+
}finally {
110+
try {
111+
if(baos!=null)baos.close();
112+
if(fos!=null)fos.close();
113+
} catch (IOException e) {
114+
e.printStackTrace();
115+
}
116+
}
117+
118+
} catch (InterruptedException e) {
119+
e.printStackTrace();
120+
}
121+
try {
122+
TimeUnit.SECONDS.sleep(2);
123+
} catch (InterruptedException e1) {
124+
}
125+
}
126+
}
127+
}
128+
129+
/**
130+
* 添加下载队列
131+
* @param entity
132+
* @return true add success false
133+
*/
134+
public boolean addDownloadTask(IPFSFileEntity entity){
135+
boolean b = queue.offer(entity);
136+
return b;
137+
}
138+
139+
/**
140+
* 启动下载
141+
*/
142+
public void start(){
143+
if(!running){
144+
running = true;
145+
dwConsumer.start();
146+
}
147+
}
148+
149+
/**
150+
*
151+
* @return
152+
*/
153+
private IPFS getIpfs(){
154+
if(ipfs1==null){
155+
String url = ConfigurationHelper.getInstance().getIPFSAddress();
156+
ipfs1 = new IPFS(url);
157+
}
158+
return ipfs1;
159+
}
160+
161+
}

src/main/java/io/nbs/client/ui/panels/manage/body/TipResultHashPanel.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.nbs.client.ui.panels.manage.body;
22

3+
import com.nbs.entity.IPFSFileEntity;
34
import io.ipfs.api.IPFS;
45
import io.ipfs.api.MerkleNode;
56
import io.ipfs.api.beans.blk.BlockStat;
67
import io.ipfs.multihash.Multihash;
8+
import io.ipfs.nbs.DownloadThreadBooster;
79
import io.nbs.client.Launcher;
810
import io.nbs.client.cnsts.ColorCnst;
911
import io.nbs.client.helper.BrowserOperationHelper;
@@ -165,9 +167,21 @@ public void searchingFromIntelnet(){
165167
}
166168

167169

168-
private void openSaveOption(BlockStat stat ){
170+
private void openSaveOption(){
171+
BlockStat stat = new BlockStat();
172+
stat.setHash(hash58);
169173
if(stat!=null&&stat.getHash()!=null){
170174
//add Queue
175+
DownloadThreadBooster booster = DownloadThreadBooster.getInstance();
176+
//booster.setSavePath();
177+
booster.start();
178+
IPFSFileEntity entity = new IPFSFileEntity();
179+
entity.setHash(stat.getHash());
180+
entity.setFullName(stat.getHash());
181+
boolean res = booster.addDownloadTask(entity);
182+
if(!res){
183+
JOptionPane.showMessageDialog(context,"当前已超过"+DownloadThreadBooster.MAX_SIZE+"个下载任务,不能继续添加");
184+
}
171185
}
172186
}
173187

@@ -206,6 +220,7 @@ public void actionPerformed(ActionEvent e) {
206220
@Override
207221
public void actionPerformed(ActionEvent e) {
208222
logger.info("download lanbery");
223+
openSaveOption();
209224
}
210225
});
211226
}
@@ -308,4 +323,5 @@ public void hideMMMonitor(){
308323
public void setPreousHash(String preousHash) {
309324
this.preousHash = preousHash;
310325
}
326+
311327
}

0 commit comments

Comments
 (0)