导航:首页 > 版本升级 > 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上传目录文件相关的资料

热点内容
学编程需要哪些方面的能力 浏览:896
在word2003表格中插入一行 浏览:606
怎么把拍照取字的文件转成pdf 浏览:838
小米穿戴app哪里更换表盘 浏览:911
满足该条件更改文件内容的代码 浏览:503
xp系统怎么卸载win10系统文件 浏览:709
华为手机双系统app怎么转 浏览:317
u盘插上pc自动跳出文件夹 浏览:232
机密文件写在哪里 浏览:480
qq主题免费使用女孩 浏览:342
园林景观网站模板 浏览:717
五线谱入门基础视频教程下载 浏览:598
个人网站怎么盈利 浏览:618
怎么禁止程序启动程序运行 浏览:261
苹果平板的测距仪app有什么作用 浏览:229
乌云数据库酒店开房 浏览:674
查看linux用户账号密码 浏览:76
win7系统叠加网络 浏览:161
少儿编程猫的礼包兑换码是什么 浏览:274
tim文件下到哪里 浏览:147

友情链接