① java ftp上傳時斷網,文件損壞
以二進制流上傳,然後實現斷點續傳。
/**
* 上傳文件到FTP伺服器,支持斷點續傳
* @param local 本地文件名稱,絕對路徑
* @param remote 遠程文件路徑,使用/home/directory1/subdirectory/file.ext 按照Linux上的路徑指定方式,支持多級目錄嵌套,支持遞歸創建不存在的目錄結構
* @return 上傳結果
* @throws IOException
*/
public UploadStatus upload(String local,String remote) throws IOException{
FTPClient ftpClient = new FTPClient();
//設置PassiveMode傳輸
ftpClient.enterLocalPassiveMode();
//設置以二進制流的方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
UploadStatus result;
//對遠程目錄的處理
String remoteFileName = remote;
if(remote.contains("/")){
remoteFileName = remote.substring(remote.lastIndexOf("/")+1);
String directory = remote.substring(0,remote.lastIndexOf("/")+1);
if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(directory)){
//如果遠程目錄不存在,則遞歸創建遠程伺服器目錄
int start=0;
int end = 0;
if(directory.startsWith("/")){
start = 1;
}else{
start = 0;
}
end = directory.indexOf("/",start);
while(true){
String subDirectory = remote.substring(start,end);
if(!ftpClient.changeWorkingDirectory(subDirectory)){
if(ftpClient.makeDirectory(subDirectory)){
ftpClient.changeWorkingDirectory(subDirectory);
}else {
System.out.println("創建目錄失敗");
return UploadStatus.Create_Directory_Fail;
}
}
start = end + 1;
end = directory.indexOf("/",start);
//檢查所有目錄是否創建完畢
if(end <= start){
break;
}
}
}
}
//檢查遠程是否存在文件
FTPFile[] files = ftpClient.listFiles(remoteFileName);
if(files.length == 1){
long remoteSize = files[0].getSize();
File f = new File(local);
long localSize = f.length();
if(remoteSize==localSize){
return UploadStatus.File_Exits;
}else if(remoteSize > localSize){
return UploadStatus.Remote_Bigger_Local;
}
//嘗試移動文件內讀取指針,實現斷點續傳
InputStream is = new FileInputStream(f);
if(is.skip(remoteSize)==remoteSize){
ftpClient.setRestartOffset(remoteSize);
if(ftpClient.storeFile(remote, is)){
return UploadStatus.Upload_From_Break_Success;
}
}
//如果斷點續傳沒有成功,則刪除伺服器上文件,重新上傳
if(!ftpClient.deleteFile(remoteFileName)){
return UploadStatus.Delete_Remote_Faild;
}
is = new FileInputStream(f);
if(ftpClient.storeFile(remote, is)){
result = UploadStatus.Upload_New_File_Success;
}else{
result = UploadStatus.Upload_New_File_Failed;
}
is.close();
}else {
InputStream is = new FileInputStream(local);
if(ftpClient.storeFile(remoteFileName, is)){
result = UploadStatus.Upload_New_File_Success;
}else{
result = UploadStatus.Upload_New_File_Failed;
}
is.close();
}
return result;
}
② 請問用java寫FTP,如果想設置當連接失敗後隔一段時間重連,該如何寫
你好,根據你的描述,我覺得可以使用Timer定時器實現。
代碼可以這么寫吧,具體要寫什麼內容,就看你的要求了。
if(連接失敗)
{
Timer timer = new Timer(); timer.schele(new DoFTPConnect(), 0, 30 * 60*1000);
}
希望對你有幫助!
③ 什麼是斷點續傳如果要繼續傳輸一個傳輸中斷的文件,有哪些選擇
FTP客戶端軟體斷點續傳指的是在下載或上傳時,將下載或上傳任務(一個文件或一個壓縮包)人為的劃分為幾個部分,每一個部分採用一個線程進行上傳或下載,如果碰到網路故障,可以從已經上傳或下載的部分開始繼續上傳下載以後未上傳下載的部分
④ java中文件大小超過多大需要斷點續傳
這個不太難吧?
假設A給B傳文件F(1024位元組)。第一次B接收了512位元組,那麼第二次連接A就應該從513位元組開始傳輸。
也就是說,在第二次傳輸時,B要提供「我要從513位元組開始傳送文件F」的信息,然後A使用FileInputStream構建輸入流讀取本地文件,使用skip(512)方法跳過文件F的前512位元組再傳送文件,之後B將數據追加(append)到先前接收的文件末尾即可。
進一步考慮,如果要實現多線程傳送,即分塊傳輸,也同樣的道理。假如B要求分作兩塊同時傳輸,那麼A啟動兩個線程,一個從513位元組讀到768位元組(工256位元組),第二個線程從769位元組到1024位元組即可。
如果你要從網路上下載文件,就是說A方不是你實現的,那麼你要先確認A方支不支持斷電續傳功能(HTTP1.1),然後你查閱下HTTP1.1協議,在HTTP1.1版本里,可以通過設置請求包頭某個欄位的信息(使用URLConnection創建連接並使用setRequestProperty(String key, String value) 方法設置)從而精確讀取文件的某一段數據的。注意,基於HTTP斷點續傳的關鍵是1.1版本,1.0版本是不支持的。
⑤ 救火,如何用JAVA實現FTP多文件上傳
新建一個web工程,用來測試上傳功能。搭建好從前台訪問後台的整個框架,並測試是否能接受參數。從apache官網上下載jar包,這里我們需要的jar包共三個,從官網或者其他途徑都可以下載得到。編寫上傳的工具類。工具類主要是涉及兩個方法,首先是連接伺服器的方法。和我們平常常用的連接資料庫等的方法類似,需要有伺服器名,用戶名和密碼。第二個主要的方法就是上傳的方法,上傳的方法接受一個文件,這里需要注意的,需要去判斷傳入的是一個文件還是一個文件夾,如果是文件夾,需要循環進行文件上傳處理。寫方法調用工具類里的方法,並測試上傳功能。這里需要注意的是連接伺服器的埠號,20表示連接伺服器,21表示有上傳操作。
⑥ 怎麼用Java實現FTP上傳
sun.net.ftp.FtpClient.,該類庫主要提供了用於建立FTP連接的類。利用這些類的方法,編程人員可以遠程登錄到FTP伺服器,列舉該伺服器上的目錄,設置傳輸協議,以及傳送文件。FtpClient類涵蓋了幾乎所有FTP的功能,FtpClient的實例變數保存了有關建立"代理"的各種信息。下面給出了這些實例變數:
public static boolean useFtpProxy
這個變數用於表明FTP傳輸過程中是否使用了一個代理,因此,它實際上是一個標記,此標記若為TRUE,表明使用了一個代理主機。
public static String ftpProxyHost
此變數只有在變數useFtpProxy為TRUE時才有效,用於保存代理主機名。
public static int ftpProxyPort此變數只有在變數useFtpProxy為TRUE時才有效,用於保存代理主機的埠地址。
FtpClient有三種不同形式的構造函數,如下所示:
1、public FtpClient(String hostname,int port)
此構造函數利用給出的主機名和埠號建立一條FTP連接。
2、public FtpClient(String hostname)
此構造函數利用給出的主機名建立一條FTP連接,使用默認埠號。
3、FtpClient()
此構造函數將創建一FtpClient類,但不建立FTP連接。這時,FTP連接可以用openServer方法建立。
一旦建立了類FtpClient,就可以用這個類的方法來打開與FTP伺服器的連接。類ftpClient提供了如下兩個可用於打開與FTP伺服器之間的連接的方法。
public void openServer(String hostname)
這個方法用於建立一條與指定主機上的FTP伺服器的連接,使用默認埠號。
public void openServer(String host,int port)
這個方法用於建立一條與指定主機、指定埠上的FTP伺服器的連接。
打開連接之後,接下來的工作是注冊到FTP伺服器。這時需要利用下面的方法。
public void login(String username,String password)
此方法利用參數username和password登錄到FTP伺服器。使用過Intemet的用戶應該知道,匿名FTP伺服器的登錄用戶名為anonymous,密碼一般用自己的電子郵件地址。
下面是FtpClient類所提供的一些控制命令。
public void cd(String remoteDirectory):該命令用於把遠程系統上的目錄切換到參數remoteDirectory所指定的目錄。
public void cdUp():該命令用於把遠程系統上的目錄切換到上一級目錄。
public String pwd():該命令可顯示遠程系統上的目錄狀態。
public void binary():該命令可把傳輸格式設置為二進制格式。
public void ascii():該命令可把傳輸協議設置為ASCII碼格式。
public void rename(String string,String string1):該命令可對遠程系統上的目錄或者文件進行重命名操作。
除了上述方法外,類FtpClient還提供了可用於傳遞並檢索目錄清單和文件的若干方法。這些方法返回的是可供讀或寫的輸入、輸出流。下面是其中一些主要的方法。
public TelnetInputStream list()
返回與遠程機器上當前目錄相對應的輸入流。
public TelnetInputStream get(String filename)
獲取遠程機器上的文件filename,藉助TelnetInputStream把該文件傳送到本地。
public TelnetOutputStream put(String filename)
以寫方式打開一輸出流,通過這一輸出流把文件filename傳送到遠程計算機
package myUtil;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/**
* ftp上傳,下載
*
* @author why 2009-07-30
*
*/
public class FtpUtil {
private String ip = "";
private String username = "";
private String password = "";
private int port = -1;
private String path = "";
FtpClient ftpClient = null;
OutputStream os = null;
FileInputStream is = null;
public FtpUtil(String serverIP, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
}
public FtpUtil(String serverIP, int port, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;
}
/**
* 連接ftp伺服器
*
* @throws IOException
*/
public boolean connectServer() {
ftpClient = new FtpClient();
try {
if (this.port != -1) {
ftpClient.openServer(this.ip, this.port);
} else {
ftpClient.openServer(this.ip);
}
ftpClient.login(this.username, this.password);
if (this.path.length() != 0) {
ftpClient.cd(this.path);// path是ftp服務下主目錄的子目錄
}
ftpClient.binary();// 用2進制上傳、下載
System.out.println("已登錄到\"" + ftpClient.pwd() + "\"目錄");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 斷開與ftp伺服器連接
*
* @throws IOException
*/
public boolean closeServer() {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (ftpClient != null) {
ftpClient.closeServer();
}
System.out.println("已從伺服器斷開");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 檢查文件夾在當前目錄下是否存在
*
* @param dir
*@return
*/
private boolean isDirExist(String dir) {
String pwd = "";
try {
pwd = ftpClient.pwd();
ftpClient.cd(dir);
ftpClient.cd(pwd);
} catch (Exception e) {
return false;
}
return true;
}
/**
* 在當前目錄下創建文件夾
*
* @param dir
* @return
* @throws Exception
*/
private boolean createDir(String dir) {
try {
ftpClient.ascii();
StringTokenizer s = new StringTokenizer(dir, "/"); // sign
s.countTokens();
String pathName = ftpClient.pwd();
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpClient.sendServer("MKD " + pathName + "\r\n");
} catch (Exception e) {
e = null;
return false;
}
ftpClient.readServerResponse();
}
ftpClient.binary();
return true;
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
}
/**
* ftp上傳 如果伺服器段已存在名為filename的文件夾,該文件夾中與要上傳的文件夾中同名的文件將被替換
*
* @param filename
* 要上傳的文件(或文件夾)名
* @return
* @throws Exception
*/
public boolean upload(String filename) {
String newname = "";
if (filename.indexOf("/") > -1) {
newname = filename.substring(filename.lastIndexOf("/") + 1);
} else {
newname = filename;
}
return upload(filename, newname);
}
/**
* ftp上傳 如果伺服器段已存在名為newName的文件夾,該文件夾中與要上傳的文件夾中同名的文件將被替換
*
* @param fileName
* 要上傳的文件(或文件夾)名
* @param newName
* 伺服器段要生成的文件(或文件夾)名
* @return
*/
public boolean upload(String fileName, String newName) {
try {
String savefilename = new String(fileName.getBytes("GBK"),
"GBK");
File file_in = new File(savefilename);// 打開本地待長傳的文件
if (!file_in.exists()) {
throw new Exception("此文件或文件夾[" + file_in.getName() + "]有誤或不存在!");
}
if (file_in.isDirectory()) {
upload(file_in.getPath(), newName, ftpClient.pwd());
} else {
uploadFile(file_in.getPath(), newName);
}
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception e in Ftp upload(): " + e.toString());
return false;
} finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 真正用於上傳的方法
*
* @param fileName
* @param newName
* @param path
* @throws Exception
*/
private void upload(String fileName, String newName, String path)
throws Exception {
String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");
File file_in = new File(savefilename);// 打開本地待長傳的文件
if (!file_in.exists()) {
throw new Exception("此文件或文件夾[" + file_in.getName() + "]有誤或不存在!");
}
if (file_in.isDirectory()) {
if (!isDirExist(newName)) {
createDir(newName);
}
ftpClient.cd(newName);
File sourceFile[] = file_in.listFiles();
for (int i = 0; i < sourceFile.length; i++) {
if (!sourceFile[i].exists()) {
continue;
}
if (sourceFile[i].isDirectory()) {
this.upload(sourceFile[i].getPath(), sourceFile[i]
.getName(), path + "/" + newName);
} else {
this.uploadFile(sourceFile[i].getPath(), sourceFile[i]
.getName());
}
}
} else {
uploadFile(file_in.getPath(), newName);
}
ftpClient.cd(path);
}
/**
* upload 上傳文件
*
* @param filename
* 要上傳的文件名
* @param newname
* 上傳後的新文件名
* @return -1 文件不存在 >=0 成功上傳,返迴文件的大小
* @throws Exception
*/
public long uploadFile(String filename, String newname) throws Exception {
long result = 0;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
java.io.File file_in = new java.io.File(filename);
if (!file_in.exists())
return -1;
os = ftpClient.put(newname);
result = file_in.length();
is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return result;
}
/**
* 從ftp下載文件到本地
*
* @param filename
* 伺服器上的文件名
* @param newfilename
* 本地生成的文件名
* @return
* @throws Exception
*/
public long downloadFile(String filename, String newfilename) {
long result = 0;
TelnetInputStream is = null;
FileOutputStream os = null;
try {
is = ftpClient.get(filename);
java.io.File outfile = new java.io.File(newfilename);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
result = result + c;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 取得相對於當前連接目錄的某個目錄下所有文件列表
*
* @param path
* @return
*/
public List getFileList(String path) {
List list = new ArrayList();
DataInputStream dis;
try {
dis = new DataInputStream(ftpClient.nameList(this.path + path));
String filename = "";
while ((filename = dis.readLine()) != null) {
list.add(filename);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
FtpUtil ftp = new FtpUtil("192.168.11.11", "111", "1111");
ftp.connectServer();
boolean result = ftp.upload("C:/Documents and Settings/ipanel/桌面/java/Hibernate_HQL.docx", "amuse/audioTest/music/Hibernate_HQL.docx");
System.out.println(result ? "上傳成功!" : "上傳失敗!");
ftp.closeServer();
/**
* FTP遠程命令列表 USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT PASS PASV STOR
* REST CWD STAT RMD XCUP OPTS ACCT TYPE APPE RNFR XCWD HELP XRMD STOU
* AUTH REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ QUIT MODE SYST ABOR
* NLST MKD XPWD MDTM PROT
* 在伺服器上執行命令,如果用sendServer來執行遠程命令(不能執行本地FTP命令)的話,所有FTP命令都要加上\r\n
* ftpclient.sendServer("XMKD /test/bb\r\n"); //執行伺服器上的FTP命令
* ftpclient.readServerResponse一定要在sendServer後調用
* nameList("/test")獲取指目錄下的文件列表 XMKD建立目錄,當目錄存在的情況下再次創建目錄時報錯 XRMD刪除目錄
* DELE刪除文件
*/
}
}
⑦ java ftp怎麼實現java ftp方式的斷點續傳
運用類的辦法,編程人員能夠長途登錄到FTP伺服器,羅列該伺服器上的目錄,設置傳輸協議,以及傳送文件。FtpClient類涵 蓋了簡直一切FTP的功用,FtpClient的實例變數保留了有關樹立"署理"的各種信息。下面給出了這些實例變數:
public static boolean useFtpProxy
這個變數用於標明FTP傳輸過程中是不是運用了一個署理,因此,它實際上是一個符號,此符號若為TRUE,標明運用了一個署理主機。
public static String ftpProxyHost
此變數只要在變數useFtpProxy為TRUE時才有用,用於保留署理主機名。
public static int ftpProxyPort
此變數只要在變數useFtpProxy為TRUE時才有用,用於保留署理主機的埠地址。
FtpClient有三種不同方式的結構函數,如下所示:
1、public FtpClient(String hostname,int port)
此結構函數運用給出的主機名和埠號樹立一條FTP銜接。
2、public FtpClient(String hostname)
此結構函數運用給出的主機名樹立一條FTP銜接,運用默許埠號。
3、FtpClient()
此結構函數將創立一FtpClient類,但不樹立FTP銜接。這時,FTP銜接能夠用openServer辦法樹立。
一旦樹立了類FtpClient,就能夠用這個類的辦法來翻開與FTP伺服器的銜接。類ftpClient供給了如下兩個可用於翻開與FTP伺服器之間的銜接的辦法。
public void openServer(String hostname)
這個辦法用於樹立一條與指定主機上的FTP伺服器的銜接,運用默許埠號。
⑧ 怎麼用libcurl實現ftp斷點續傳
如何用libcurl實現ftp斷點續傳
如題,在libcurl官網上找了一個上傳函數,但測試無法實現
int upload(CURL *curlhandle, const char * remotepath, const char * localpath, long timeout, long tries)
{
FILE *f;
long uploaded_len = 0;
CURLcode r = CURLE_GOT_NOTHING;
int c;
f = fopen(localpath, "rb");
if (f == NULL) {
perror(NULL);
return 0;
}
curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
curl_easy_setopt(curlhandle, CURLOPT_USERPWD, "spider:spider");
if (timeout)
curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */
curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
for (c = 0; (r != CURLE_OK) && (c < tries); c++) {
/* are we resuming */
if (c) { /* yes */
/* determine the length of the file already written */
/*
* With NOBODY and NOHEADER, libcurl will issue a SIZE
* command, but the only way to retrieve the result is
* to parse the returned Content-Length header. Thus,
* getcontentlengthfunc(). We need discardfunc() above
* because HEADER will mp the headers to stdout
* without it.
*/
curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
r = curl_easy_perform(curlhandle);
if (r != CURLE_OK)
continue;
curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
fseek(f, uploaded_len, SEEK_SET);
curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
}
else { /* no */
curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
}
r = curl_easy_perform(curlhandle);
}
fclose(f);
if (r == CURLE_OK)
return 1;
else {
fprintf(stderr, "%s\n", curl_easy_strerror(r));
return 0;
}
}
⑨ 什麼是斷點續傳
就是信號中斷後(掉線或關機等),下次能夠從上次的地方接著傳送(一般指下載或上傳),不支持斷點續傳就意味著下次下載或上傳必須從零開始.$
一般常用的網際快車就支持斷點續傳啊,斷點大小是可以設置的,比如設為200KB,意思就是每下載200KB的文件,就自動保存一次。舉個例子你就明白了:=l
迅雷上次沒傳完,下次打開,就可以直接傳了
⑩ java ftp下載
這個和ftp沒有太大關系,只是一個普通的下載,java連接ftp伺服器傳輸文件是需要提供ip 埠號,用戶名密碼 路徑的,這個只是一個靜態資源,用這個就可以,支持斷點續傳
這邊用到了apachecommons-httpclient-3.1包
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.RandomAccessFile;
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.DefaultHttpClient;
@SuppressWarnings("deprecation")
publicclassDownloadTool{
publicstaticvoidmain(String[]args)throwsClientProtocolException,IOException{
Stringurl="
[陽光電影
].神探駕到.BD.720p.國粵雙語中字.mkv";
StringdownFile="d:\aaa.mkv";//本地存放路徑
LongnetFileLenght=getNetFileSize(url);
LonglocalFileLenght=getLocalFileSize(downFile);
if(localFileLenght>=netFileLenght){
System.out.println("已下載完成");
return;
}
System.out.println("netFileLenght:"+netFileLenght+"localFileLenght:"+localFileLenght);
finalHttpClienthttpClient=newDefaultHttpClient();
httpClient.getParams().setIntParameter("http.socket.timeout",5000);
finalHttpGethttpGet=newHttpGet(url);
httpGet.addHeader("Range","bytes="+localFileLenght+"-");
finalHttpResponseresponse=httpClient.execute(httpGet);
finalintcode=response.getStatusLine().getStatusCode();
finalHttpEntityentity=response.getEntity();
System.out.println(code);
if(entity!=null&&code<400){
Filefile=newFile(downFile);
=newRandomAccessFile(file,"rw");
randomAccessFile.seek(localFileLenght);
InputStreaminputStream=entity.getContent();
intb=0;
finalbytebuffer[]=newbyte[1024];
while((b=inputStream.read(buffer))!=-1){
randomAccessFile.write(buffer,0,b);
}
randomAccessFile.close();
inputStream.close();
httpClient.getConnectionManager().shutdown();
System.out.println("下載完成");
}
}
(StringfileName){
Filefile=newFile(fileName);
returnfile.length();
}
(Stringurl){
Longcount=-1L;
finalHttpClienthttpClient=newDefaultHttpClient();
httpClient.getParams().setIntParameter("http.socket.timeout",5000);
finalHttpGethttpGet=newHttpGet(url);
HttpResponseresponse=null;
try{
response=httpClient.execute(httpGet);
finalintcode=response.getStatusLine().getStatusCode();
finalHttpEntityentity=response.getEntity();
if(entity!=null&&code==200){
count=entity.getContentLength();
}
}catch(ClientProtocolExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
httpClient.getConnectionManager().shutdown();
}
returncount;
}
}