Android使用xutils图片上传和服务器接收
2015-07-06 14:50
726 查看
使用xUtils https://github.com/wyouflf/xUtils上传图片:
Client:
需要Xutils.jar
调用上传:
String uploadHost="http://129.44.0.137:8080/AndroidUploadServer/upload.do";
RequestParams params=new RequestParams();
params.addBodyParameter("msg",imgtxt.getText().toString());
params.addBodyParameter(filePath.replace("/",
""), new File(filePath));
uploadMethod(params,uploadHost);
上传方法:
public
void uploadMethod(final RequestParams params,final String uploadHost) {
http.send(HttpRequest.HttpMethod.POST, uploadHost, params,new RequestCallBack<String>() {
@Override
public void onStart() {
// msgTextview.setText("conn...");
}
@Override
public void onLoading(long total,
long current,boolean isUploading) {
if (isUploading) {
// msgTextview.setText("upload: " + current + "/"+ total);
} else {
// msgTextview.setText("reply: " + current + "/"+ total);
}
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
// msgTextview.setText("reply: " + responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
// msgTextview.setText(error.getExceptionCode() + ":" + msg);
}
});
}
Server:
需要smartupload.jar猛戳下载
测试代码下载
[java]
view plaincopyprint?
import com.jspsmart.upload.SmartUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UploadServlet
extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html,charset=UTF-8");
SmartUpload smartUpload = new SmartUpload();
try {
smartUpload.initialize(this.getServletConfig(), request, response);
smartUpload.upload();
com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);
if (!smartFile.isMissing()) {
String saveFileName =
"/data/" + smartFile.getFileName();
smartFile.saveAs(saveFileName, smartUpload.SAVE_PHYSICAL);
response.getWriter().print("ok:" + saveFileName +
", msg:" + smartUpload.getRequest().getParameter("msg"));
} else {
response.getWriter().print("missing...");
}
} catch (Exception e) {
response.getWriter().print(e);
}
}
}
-------------------------------------------------------------------以下代码是早期写的测试代码,仅做参考-----------------------------------------------------------------------------------------
界面很简单,点击 【选择图片】,从图库里选择图片,显示到下面的imageview里,点击上传,就会上传到指定的服务器
Client:
需要Xutils.jar
调用上传:
String uploadHost="http://129.44.0.137:8080/AndroidUploadServer/upload.do";
RequestParams params=new RequestParams();
params.addBodyParameter("msg",imgtxt.getText().toString());
params.addBodyParameter(filePath.replace("/",
""), new File(filePath));
uploadMethod(params,uploadHost);
String uploadHost="http://129.44.0.137:8080/AndroidUploadServer/upload.do"; RequestParams params=new RequestParams(); params.addBodyParameter("msg",imgtxt.getText().toString()); params.addBodyParameter(filePath.replace("/", ""), new File(filePath)); uploadMethod(params,uploadHost);
上传方法:
public
void uploadMethod(final RequestParams params,final String uploadHost) {
http.send(HttpRequest.HttpMethod.POST, uploadHost, params,new RequestCallBack<String>() {
@Override
public void onStart() {
// msgTextview.setText("conn...");
}
@Override
public void onLoading(long total,
long current,boolean isUploading) {
if (isUploading) {
// msgTextview.setText("upload: " + current + "/"+ total);
} else {
// msgTextview.setText("reply: " + current + "/"+ total);
}
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
// msgTextview.setText("reply: " + responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
// msgTextview.setText(error.getExceptionCode() + ":" + msg);
}
});
}
public void uploadMethod(final RequestParams params,final String uploadHost) { http.send(HttpRequest.HttpMethod.POST, uploadHost, params,new RequestCallBack<String>() { @Override public void onStart() { // msgTextview.setText("conn..."); } @Override public void onLoading(long total, long current,boolean isUploading) { if (isUploading) { // msgTextview.setText("upload: " + current + "/"+ total); } else { // msgTextview.setText("reply: " + current + "/"+ total); } } @Override public void onSuccess(ResponseInfo<String> responseInfo) { // msgTextview.setText("reply: " + responseInfo.result); } @Override public void onFailure(HttpException error, String msg) { // msgTextview.setText(error.getExceptionCode() + ":" + msg); } }); }
Server:
需要smartupload.jar猛戳下载
测试代码下载
[java]
view plaincopyprint?
import com.jspsmart.upload.SmartUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UploadServlet
extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html,charset=UTF-8");
SmartUpload smartUpload = new SmartUpload();
try {
smartUpload.initialize(this.getServletConfig(), request, response);
smartUpload.upload();
com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);
if (!smartFile.isMissing()) {
String saveFileName =
"/data/" + smartFile.getFileName();
smartFile.saveAs(saveFileName, smartUpload.SAVE_PHYSICAL);
response.getWriter().print("ok:" + saveFileName +
", msg:" + smartUpload.getRequest().getParameter("msg"));
} else {
response.getWriter().print("missing...");
}
} catch (Exception e) {
response.getWriter().print(e);
}
}
}
import com.jspsmart.upload.SmartUpload; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class UploadServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html,charset=UTF-8"); SmartUpload smartUpload = new SmartUpload(); try { smartUpload.initialize(this.getServletConfig(), request, response); smartUpload.upload(); com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0); if (!smartFile.isMissing()) { String saveFileName = "/data/" + smartFile.getFileName(); smartFile.saveAs(saveFileName, smartUpload.SAVE_PHYSICAL); response.getWriter().print("ok:" + saveFileName + ", msg:" + smartUpload.getRequest().getParameter("msg")); } else { response.getWriter().print("missing..."); } } catch (Exception e) { response.getWriter().print(e); } } }
-------------------------------------------------------------------以下代码是早期写的测试代码,仅做参考-----------------------------------------------------------------------------------------
界面很简单,点击 【选择图片】,从图库里选择图片,显示到下面的imageview里,点击上传,就会上传到指定的服务器
相关文章推荐
- 解决android照片溢出问题
- 从明天开始,开始写我的一点android经验。欢迎大家拍砖。
- Android中GridView滚动到底部加载数据
- Android -----listView的重要属性
- android NDK开发未包含javah生成的头文件导致程序运行失败问题
- android ImageView 宽度设定,高度自适应
- android window 属性
- MTP in Android
- android crash 日志保存
- android 5.0 support
- Android源码之DeskClock (一)
- android 删除SD卡或手机的缓存图像和文件夹
- Android studio 快捷键
- Android仿QQ中的“我的空间”做出ScrollView滑动修改标题栏颜色操作
- Android Studio使用备忘
- 分享45个android实例源码
- android 开发框架
- 使用Kotlin开发Android应用(II):创建新工程
- Android 自定义控件初级 多层按钮旋转控件
- Android动态加载(插件化)基本原理总结