Android使用xutils图片上传和服务器接收

使用xUtils   https://github.com/wyouflf/xUtils上传图片:


Client:

需要Xutils.jar

调用上传:

 
  1. String uploadHost="http://129.44.0.137:8080/AndroidUploadServer/upload.do"
  2. RequestParams params=new RequestParams(); 
  3. params.addBodyParameter("msg",imgtxt.getText().toString());  
  4. params.addBodyParameter(filePath.replace("/", ""), new File(filePath));  
  5. 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);


上传方法:

 
  1.     public  void uploadMethod(final RequestParams params,final String uploadHost) { 
  2.         http.send(HttpRequest.HttpMethod.POST, uploadHost, params,new RequestCallBack<String>() { 
  3.                     @Override 
  4.                     public void onStart() { 
  5. //                      msgTextview.setText("conn..."); 
  6.                     } 
  7.                     @Override 
  8.                     public void onLoading(long total, long current,boolean isUploading) { 
  9.                         if (isUploading) { 
  10. //                          msgTextview.setText("upload: " + current + "/"+ total); 
  11.                         } else
  12. //                          msgTextview.setText("reply: " + current + "/"+ total); 
  13.                         } 
  14.                     } 
  15.                     @Override 
  16.                     public void onSuccess(ResponseInfo<String> responseInfo) { 
  17. //                      msgTextview.setText("reply: " + responseInfo.result); 
  18.                     } 
  19.                     @Override 
  20.                     public void onFailure(HttpException error, String msg) { 
  21. //                      msgTextview.setText(error.getExceptionCode() + ":" + msg);  
  22.                     } 
  23.                 }); 
  24.     } 
	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猛戳下载     测试代码下载

  1. import com.jspsmart.upload.SmartUpload; 
  2.  
  3. import javax.servlet.ServletException; 
  4. import javax.servlet.http.HttpServletRequest; 
  5. import javax.servlet.http.HttpServletResponse; 
  6. import java.io.IOException; 
  7.  
  8. public class UploadServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { 
  9.  
  10.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  11.  
  12.         response.setContentType("text/html,charset=UTF-8"); 
  13.  
  14.         SmartUpload smartUpload = new SmartUpload(); 
  15.  
  16.         try
  17.             smartUpload.initialize(this.getServletConfig(), request, response); 
  18.             smartUpload.upload(); 
  19.             com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0); 
  20.             if (!smartFile.isMissing()) { 
  21.                 String saveFileName = "/data/" + smartFile.getFileName(); 
  22.                 smartFile.saveAs(saveFileName, smartUpload.SAVE_PHYSICAL); 
  23.                 response.getWriter().print("ok:" + saveFileName + ", msg:" + smartUpload.getRequest().getParameter("msg")); 
  24.             } else
  25.                 response.getWriter().print("missing..."); 
  26.             } 
  27.         } catch (Exception e) { 
  28.             response.getWriter().print(e); 
  29.         } 
  30.  
  31.     } 
  32.  
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里,点击上传,就会上传到指定的服务器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值