導航:首頁 > 版本升級 > android上傳目錄文件

android上傳目錄文件

發布時間:2024-07-14 13:44:54

① android的自帶的httpClient 怎麼上傳文件

這里只貼出代碼,可根據實際情況自行修改。

[java]
package com.lxb.uploadwithprogress.http;

import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import com.lxb.uploadwithprogress.http.CustomMultipartEntity.ProgressListener;

public class HttpMultipartPost extends AsyncTask
{

private Context context;
private String filePath;
private ProgressDialog pd;
private long totalSize;

public HttpMultipartPost(Context context, String filePath) {
this.context = context;
this.filePath = filePath;
}

@Override
protected void onPreExecute() {
pd = new ProgressDialog(context);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage(Uploading Picture...);
pd.setCancelable(false);
pd.show();
}

@Override
protected String doInBackground(String... params) {
String serverResponse = null;

HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost

try {
CustomMultipartEntity multipartContent = new CustomMultipartEntity(
new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});

// We use FileBody to transfer an image
multipartContent.addPart(data, new FileBody(new File(
filePath)));
totalSize = multipartContent.getContentLength();

// Send it
httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, httpContext);
serverResponse = EntityUtils.toString(response.getEntity());

} catch (Exception e) {
e.printStackTrace();
}

return serverResponse;
}

@Override
protected void onProgressUpdate(Integer... progress) {
pd.setProgress((int) (progress[0]));
}

@Override
protected void onPostExecute(String result) {
System.out.println(result: + result);
pd.dismiss();
}

@Override
protected void onCancelled() {
System.out.println(cancle);
}

}
[java]
package com.lxb.uploadwithprogress.http;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;

public class CustomMultipartEntity extends MultipartEntity {

private final ProgressListener listener;

public CustomMultipartEntity(final ProgressListener listener) {
super();
this.listener = listener;
}

public CustomMultipartEntity(final HttpMultipartMode mode,
final ProgressListener listener) {
super(mode);
this.listener = listener;
}

public CustomMultipartEntity(HttpMultipartMode mode, final String boundary,
final Charset charset, final ProgressListener listener) {
super(mode, boundary, charset);
this.listener = listener;
}

@Override
public void writeTo(OutputStream outstream) throws IOException {
super.writeTo(new CountingOutputStream(outstream, this.listener));
}

public static interface ProgressListener {
void transferred(long num);
}

public static class CountingOutputStream extends FilterOutputStream {

private final ProgressListener listener;
private long transferred;

public CountingOutputStream(final OutputStream out,
final ProgressListener listener) {
super(out);
this.listener = listener;
this.transferred = 0;
}

public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
this.transferred += len;
this.listener.transferred(this.transferred);
}

public void write(int b) throws IOException {
out.write(b);
this.transferred++;
this.listener.transferred(this.transferred);
}
}

}
上面為兩個主要的類,下面放一個調用的Activity

② android上大文件分片上傳 具體怎麼弄

提供一點demo
斷點續傳(改良版)
package com.phone1000.demo09;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessDemo {
public static void main(String[] args) {
// 1.找到文件
File file = new File("E:\\網路雲盤\\網路雲同步盤\\Android開發視頻教程\\[Android開發視頻教程]02_01_spinner的使用.mp4");
File file2 = new File("E:\\我的照片\\[Android開發視頻教程]02_01_spinner的使用.mp4");
//2.創建流
RandomAccessFile is = null;
FileOutputStream os = null;

try {
is = new RandomAccessFile(file,"r");
os = new FileOutputStream(file2,true);
//3.定義一個容器
byte[] b = new byte[1024];
//4.定義一個長度
int len = 0 ;
long oldLength = file.length();
long newLength = 0;
//5.循環讀數
while((len = is.read(b)) != -1){
if(newLength >= oldLength)
{
System.out.println("傳輸完成!");
break;
}
else{
newLength = newLength + len;
is.seek(newLength);
os.write(b);
}
}//釋放資源
os.close();
is.close();

} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}
}

③ Android的.apk文件怎麼上傳到手機

具體方法如下:

1、首先打開電腦上的qq,在首界面找到我的設備。

同時打開手機qq,並連回上無線。

閱讀全文

與android上傳目錄文件相關的資料

熱點內容
中文網站翻譯成俄語多少錢 瀏覽:107
文件名加標題 瀏覽:644
共享數據怎麼被凍結了 瀏覽:150
如何登記考勤數據 瀏覽:640
蘋果6s前置照片效果 瀏覽:171
企業微信素材庫文件下載鏈接 瀏覽:582
pdf文件怎麼取消標黃 瀏覽:781
打電話時不能使用網路是什麼原因 瀏覽:919
ps怎麼將文件合並 瀏覽:747
java的日期格式化 瀏覽:981
電腦應用程序怎麼關 瀏覽:986
微信上鏈接在哪個文件夾 瀏覽:691
歐姆龍安裝打開找不到密鑰文件 瀏覽:302
蘋果基本表情 瀏覽:128
我的世界教育版編程在哪裡 瀏覽:842
pong文件夾找不到 瀏覽:759
69版本黑切 瀏覽:997
杭州道富java 瀏覽:635
知道qq號查微博賬號和密碼 瀏覽:294
紅手指自帶哪些app 瀏覽:103

友情鏈接