使用XUtils上传文件(初始版,待整理)
来源:互联网 发布:快手未知网络连接错误 编辑:程序博客网 时间:2024/05/18 02:20
import android.os.Environment;import com.lidroid.xutils.HttpUtils;import com.lidroid.xutils.exception.HttpException;import com.lidroid.xutils.http.RequestParams;import com.lidroid.xutils.http.ResponseInfo;import com.lidroid.xutils.http.callback.RequestCallBack;import com.lidroid.xutils.http.client.HttpRequest;import net.yundongpai.iyd.network.RestApi;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;/** * Created by xiaoyee on 9/11/15. In project yundong_android */public class UpDownUtil { public static void upload(// String filePath, Map<String, String> paramMaps, String url ) { String uploadUrl = RestApi.getAbsoluteUrl("/yundong/yd_info/addTopicInfo.json"); File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); String pathpic = path.getAbsolutePath() + "/Camera/IMG_20140927_113448.jpg"; String pathVideo = path.getAbsolutePath() + "/Camera/VID_20150911_141955.mp4"; //sdcard/DCIM/Camera/IMG_20140927_113448.jpg RequestParams params = new RequestParams(); params.addHeader("appversion", "2.2.1(alpha)"); params.addHeader("build", "26"); params.addHeader("uid", "10205"); params.addHeader("User-agent", "Yundong[Android;Model number:Nexus 5;Android version:5.1.1]"); params.addQueryStringParameter("topic_id", "97"); params.addQueryStringParameter("type", "2"); params.addQueryStringParameter("uid", "10205"); //topic_id 97 type 1 File filePic = new File(pathpic); File fileVideo = new File(pathVideo); try { InputStream inputStreamPic = new FileInputStream(filePic); InputStream inputStreamVideo = new FileInputStream(fileVideo);// InputStreamBody body = new InputStreamBody(inputStream, file.length(), file.getName(), "image/jpeg");// params.addBodyParameter("file", inputStream, file.length(), file.getName(), "image/jpeg"); params.addBodyParameter("file_pic", inputStreamPic, filePic.length(), filePic.getName(), "image/jpeg"); params.addBodyParameter("file_video", inputStreamVideo, fileVideo.length(), fileVideo.getName(), "video/mp4"); } catch (FileNotFoundException e) { e.printStackTrace(); } HttpUtils http = new HttpUtils(); http.send( HttpRequest.HttpMethod.POST, uploadUrl, params, new RequestCallBack<String>() { @Override public void onStart() { LogCus.d("onStart"); } @Override public void onLoading(long total, long current, boolean isUploading) { if (isUploading) { LogCus.d("onLoading upload: " + current + "/" + total);// testTextView.setText("upload: " + current + "/" + total); } else { LogCus.d("onLoading reply: " + current + "/" + total);// testTextView.setText("reply: " + current + "/" + total); } } @Override public void onSuccess(ResponseInfo<String> responseInfo) { LogCus.d("reply: " + responseInfo.result);// testTextView.setText("reply: " + responseInfo.result); } @Override public void onFailure(HttpException error, String msg) { LogCus.d("onStart" + error.getExceptionCode() + ":" + msg);// testTextView.setText(error.getExceptionCode() + ":" + msg); } } ); }}
0 0
- 使用XUtils上传文件(初始版,待整理)
- xUtils的基本使用(get,post,上传文件)
- 使用xUtils下载和上传文件
- 使用xUtils下载和上传文件
- xUtils上传文件使用表单的方式上传文件。
- struts2使用ajax上传文件(待测试)
- xutils上传文件到服务器
- 个人整理的Xutils使用
- jvisualvm监控使用(待更新整理)
- Ant使用[待整理]
- android 使用xutils 上传 数组
- 使用xutils 上传 数组记录
- 使用XUtils上传文件(利用桥接模式将上传模块化)
- Java文件上传(待完善)
- Xutils上传图片(2)
- Xutils HttpUtils上传文件的实现
- Xutils HttpUtils上传文件的实现
- 树链剖分(待整理)
- 深入浅出CChart 每日一课——快乐高四第十六课 老马识途,ChartCtrl控件在WTL中的使用
- 用mocha写分页的测试用例
- NullReferenceException: Object reference not set to an instance of an object
- VC多线程编程
- 获得输入焦点
- 使用XUtils上传文件(初始版,待整理)
- RX-93-v GUNDAM VER.KA
- iOS每日一记之————AlertView的BUG解决 限制textField的字数长度 ScrollerView的上划键盘下去
- Form widgets for many2many fields in Odooj添加多个图片
- JAVA之初体验
- 理解nodejs的stream和pipe机制
- lintcode-最长连续子序列
- 格式化控制台输出颜色
- mysql共享表空间和独立表空间