forked from fanruan/design
Bryant
5 years ago
4 changed files with 62 additions and 408 deletions
@ -1,118 +0,0 @@
|
||||
package com.fr.design.update.actions; |
||||
|
||||
import com.fr.design.update.domain.UpdateConstants; |
||||
import com.fr.locale.InterProviderFactory; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.design.update.domain.DownloadItem; |
||||
import com.fr.stable.ArrayUtils; |
||||
import com.fr.stable.StableUtils; |
||||
|
||||
import javax.swing.JOptionPane; |
||||
import javax.swing.SwingWorker; |
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.InputStream; |
||||
import java.net.URL; |
||||
import java.net.URLConnection; |
||||
import java.util.concurrent.ExecutionException; |
||||
|
||||
/** |
||||
* Created by XINZAI on 2018/8/21. |
||||
*/ |
||||
public abstract class FileDownloader extends SwingWorker<Boolean, DownloadItem> { |
||||
private static final int REPEAT_DOWNLOAD_TIMES = 3; |
||||
private DownloadItem[] files; |
||||
private String saveDir; |
||||
//已经完成的大小
|
||||
private long completeSize; |
||||
|
||||
public FileDownloader(DownloadItem[] files, String saveDir) { |
||||
this.files = files; |
||||
this.saveDir = saveDir; |
||||
} |
||||
|
||||
@Override |
||||
protected Boolean doInBackground() throws Exception { |
||||
if (ArrayUtils.isNotEmpty(files)) { |
||||
setCompleteSize(0L); |
||||
for (DownloadItem item : files) { |
||||
for (int i = 0; i < REPEAT_DOWNLOAD_TIMES; i++) { |
||||
item.setTotalLength(0); |
||||
item.setDownloadLength(0); |
||||
download(item); |
||||
if (item.getTotalLength() == item.getDownloadLength()) { |
||||
break; |
||||
} |
||||
} |
||||
if (item.getTotalLength() != item.getDownloadLength()) { |
||||
JOptionPane.showMessageDialog(null, |
||||
com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Updater_Download_Failed"), |
||||
InterProviderFactory.getProvider().getLocText("Fine-Design_Updater_Alert"), JOptionPane.ERROR_MESSAGE); |
||||
return false; |
||||
} else { |
||||
item.setDownloadLength(0); |
||||
completeSize += item.getTotalLength(); |
||||
} |
||||
} |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
protected void done() { |
||||
boolean success = false; |
||||
try { |
||||
success = get(); |
||||
} catch (InterruptedException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
Thread.currentThread().interrupt(); |
||||
} catch (ExecutionException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
if (success) { |
||||
onDownloadSuccess(); |
||||
} else { |
||||
onDownloadFailed(); |
||||
} |
||||
} |
||||
|
||||
private void download(DownloadItem item) throws Exception { |
||||
URL url = new URL(item.getUrl()); |
||||
URLConnection connection = url.openConnection(); |
||||
int total = connection.getContentLength(); |
||||
item.setTotalLength(total); |
||||
File tempFile = new File(StableUtils.pathJoin(saveDir, item.getName())); |
||||
StableUtils.makesureFileExist(tempFile); |
||||
try ( InputStream reader = connection.getInputStream(); |
||||
FileOutputStream writer = new FileOutputStream(tempFile)) { |
||||
byte[] buffer = new byte[UpdateConstants.BYTE]; |
||||
int bytesRead = 0; |
||||
int totalBytesRead = 0; |
||||
while ((bytesRead = reader.read(buffer)) != -1) { |
||||
writer.write(buffer, 0, bytesRead); |
||||
buffer = new byte[UpdateConstants.BYTE]; |
||||
totalBytesRead += bytesRead; |
||||
item.setDownloadLength(totalBytesRead); |
||||
publish(item); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 下载成功 |
||||
*/ |
||||
public abstract void onDownloadSuccess(); |
||||
|
||||
/** |
||||
* 下载失败 |
||||
*/ |
||||
public abstract void onDownloadFailed(); |
||||
|
||||
public long getCompleteSize() { |
||||
return completeSize; |
||||
} |
||||
|
||||
public void setCompleteSize(long completeSize) { |
||||
this.completeSize = completeSize; |
||||
} |
||||
} |
@ -0,0 +1,55 @@
|
||||
package com.fr.design.update.actions; |
||||
|
||||
import com.fr.decision.update.Info.UpdateCallBack; |
||||
import com.fr.decision.update.UpdateExecutor; |
||||
import com.fr.log.FineLoggerFactory; |
||||
|
||||
import javax.swing.*; |
||||
import java.util.concurrent.ExecutionException; |
||||
|
||||
/** |
||||
* @author Bryant |
||||
* @version 10.0 |
||||
* Created by Bryant on 2019-09-12 |
||||
*/ |
||||
public abstract class FileProcess extends SwingWorker<Boolean, Void> { |
||||
|
||||
private UpdateCallBack callBack; |
||||
|
||||
public FileProcess(UpdateCallBack callBack) { |
||||
this.callBack = callBack; |
||||
} |
||||
|
||||
@Override |
||||
protected Boolean doInBackground() throws Exception { |
||||
return UpdateExecutor.getInstance().execute(callBack); |
||||
} |
||||
|
||||
@Override |
||||
protected void done() { |
||||
boolean success = false; |
||||
try { |
||||
success = get(); |
||||
} catch (InterruptedException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
Thread.currentThread().interrupt(); |
||||
} catch (ExecutionException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
if (success) { |
||||
onDownloadSuccess(); |
||||
} else { |
||||
onDownloadFailed(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 下载成功 |
||||
*/ |
||||
public abstract void onDownloadSuccess(); |
||||
|
||||
/** |
||||
* 下载失败 |
||||
*/ |
||||
public abstract void onDownloadFailed(); |
||||
} |
@ -1,102 +0,0 @@
|
||||
package com.fr.design.update.domain; |
||||
|
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.json.JSONObject; |
||||
|
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* Created by XINZAI on 2018/8/21. |
||||
*/ |
||||
public class DownloadItem { |
||||
|
||||
//显示为百分比
|
||||
private static final int PERCENTAGE_RATIO = 100; |
||||
//显示kB
|
||||
private static final int BYTETOKB_RATIO = 1000; |
||||
|
||||
private String name; |
||||
private String url; |
||||
private long size; |
||||
|
||||
private int totalLength; |
||||
private int downloadLength; |
||||
|
||||
public DownloadItem(JSONObject json) { |
||||
this(json.optString("name"), json.optString("url"), json.optLong("size")); |
||||
} |
||||
|
||||
public DownloadItem(String name, String url, long size) { |
||||
this.name = name; |
||||
this.url = url; |
||||
this.size = size; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public String getUrl() { |
||||
return url + "?v=" + new Date().getTime(); |
||||
} |
||||
|
||||
|
||||
public long getSize() { |
||||
return size; |
||||
} |
||||
|
||||
public void setSize(long size) { |
||||
this.size = size; |
||||
} |
||||
|
||||
public int getTotalLength() { |
||||
return totalLength; |
||||
} |
||||
|
||||
public int getDownloadLength() { |
||||
return downloadLength; |
||||
} |
||||
|
||||
public void setTotalLength(int totalLength) { |
||||
this.totalLength = totalLength; |
||||
} |
||||
|
||||
public void setDownloadLength(int downloadLength) { |
||||
this.downloadLength = downloadLength; |
||||
} |
||||
|
||||
public int getProgressValue() { |
||||
return (int) ((downloadLength / (double) totalLength) * PERCENTAGE_RATIO); |
||||
} |
||||
|
||||
public String getProgressString() { |
||||
return downloadLength / BYTETOKB_RATIO + "KB/" + totalLength / BYTETOKB_RATIO + "KB"; |
||||
} |
||||
|
||||
/** |
||||
* 转化为字符串 |
||||
* |
||||
* @return 字符串 |
||||
*/ |
||||
@Override |
||||
public String toString() { |
||||
return "name:" + name + ";download:" + getProgressString(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
return obj instanceof DownloadItem |
||||
&& ComparatorUtils.equals(((DownloadItem) obj).name, name) |
||||
&& ComparatorUtils.equals(((DownloadItem) obj).url, url); |
||||
} |
||||
|
||||
/** |
||||
* 返回一个hash码 |
||||
* |
||||
* @return hash码 |
||||
*/ |
||||
@Override |
||||
public int hashCode() { |
||||
return name.hashCode(); |
||||
} |
||||
} |
Loading…
Reference in new issue