?? qqshowdownloadthread.java
字號:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* QQ秀圖片的下載線程,在下載完畢后,將會調用QQShowManager的回調方法
*
* @author 馬若劼
*/
public class QQShowDownloadThread extends Thread {
// Log對象
private static Log log = LogFactory.getLog(QQShowDownloadThread.class);
// QQShowManager實例
private QQShowManager sm;
// 好友QQ號
private int qqNum;
// 輸入流
private BufferedInputStream bis;
/**
* 構造函數
* @param qqNum 好友的QQ號
*/
public QQShowDownloadThread(int qqNum) {
this.qqNum = qqNum;
setDaemon(true);
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
sm = QQShowManager.getInstance();
// 得到URL,如果有錯誤,則返回
String urlString = sm.getQQShowUrlString(qqNum);
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
sm.threadCallback(qqNum);
return;
}
File tempFile = null;
FileOutputStream fos = null;
try {
log.debug("開始下載QQ秀,地址" + urlString);
// 臨時文件
tempFile = new File(QQShowManager.QQ_SHOW_CACHE_DIR + qqNum + "-temp.gif");
if(!tempFile.exists())
tempFile.createNewFile();
// 打開臨時文件輸出流
fos = new FileOutputStream(tempFile);
// 開始下載,同時數據寫入到臨時文件中
byte[] buf = new byte[1024];
bis = new BufferedInputStream(url.openStream());
log.debug("成功打開輸入流");
for(int i = bis.read(buf, 0, 1024); i != -1; i = bis.read(buf, 0, 1024))
fos.write(buf, 0, i);
// 寫入完成,把臨時文件改名,但是要先關閉流,不然改不了的
fos.close();
fos = null;
File file = new File(QQShowManager.QQ_SHOW_CACHE_DIR + qqNum + ".gif");
log.debug("改名" + (tempFile.renameTo(file)?"成功":"失敗"));
log.debug("下載成功");
} catch (IOException e) {
log.error("下載QQ秀圖片文件時發生錯誤或者用戶退出程序導致線程強行結束");
// 如果出現錯誤,則刪除臨時文件
if(fos != null) {
try {
fos.close();
fos = null;
} catch (IOException e1) {
log.error(e.getMessage());
}
}
if(tempFile != null && tempFile.exists())
tempFile.delete();
} finally {
try {
if(fos != null) fos.close();
if(bis != null) bis.close();
} catch (IOException e1) {
log.error(e1.getMessage());
}
sm.threadCallback(qqNum);
log.debug("" + qqNum + " QQ秀下載線程退出");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -