• Android 批量上传sd卡图片


    最近手头上需要批量上传一些保存到SD卡图片由于简单,过于忘记,写在博客中吧!同时也希望能帮到大家!

    一 、 以下是一个Service类

    package cn.com.service;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.methods.multipart.FilePart;
    import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
    import org.apache.commons.httpclient.methods.multipart.Part;
    import org.apache.commons.httpclient.methods.multipart.StringPart;

    import android.util.Log;

    /**
     * 批量上传服务类
     * @author ChonZY
     *
     */
    public class DataService {
     /**
      * 批量上传图片
      * @param path  服务器地址
      * @param name 用户名
      * @param filePath sd卡图片路径
      * @return
      * @throws Exception
      */
     public static String sendDataByHttpClientPost(String path ,String name,List<File> filePath)throws Exception{
      
      List<Part> partList = new ArrayList<Part>();
      partList.add(new StringPart("user", name));
      partList.add(new StringPart("picnumber", "4"));
      
      for (int i = 0; i < filePath.size(); i++) {
       String picPath = filePath.get(i).getPath();
       Log.i("Other", "服务类 图片路径Service _ PicPath ="+picPath);
       partList.add(new FilePart("file", new File(filePath.get(i).getPath())));
      }
      
      // 实例化上传数据的数组
    //  Part [] parts = {new StringPart("user", name),new FilePart("file", new File(filePath))};
      
      Part[] parts = partList.toArray(new Part[0]);
      
      Log.i("Other","new Part[0] _size:"+ parts.length);
      
      PostMethod filePost = new PostMethod(path);
      
      filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
      
      org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
      client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
      
      int status = client.executeMethod(filePost);
      Log.i("Other", "返回结果:"+status);
      if(status == 200){
       
       System.out.println( filePost.getResponseCharSet());
       String result = new String(filePost.getResponseBodyAsString());
       
       
       System.out.println("--"+result);
       return result;
      }else{
       throw new IllegalStateException("服务器状态异常");
      }
      
      
     }
     /**
      * 单次上传图片or文件
      * @param path
      * @param name
      * @param filePath
      * @return
      * @throws Exception
      */
     public static String sendDataByHttpClientPost(String path ,String name,String filePath)throws Exception{
      
      /*List<Part> partList = new ArrayList<Part>();
      partList.add(new StringPart("user", name));
      
      for (int i = 0; i < 4; i++) {
       partList.add(new FilePart(name, FilePart()));
      }*/
      
      // 实例化上传数据的数组
      Part [] parts = {new StringPart("user", name),new FilePart("file", new File(filePath))};
      
      PostMethod filePost = new PostMethod(path);
      
      filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
      
      org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
      client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
      
      int status = client.executeMethod(filePost);
      Log.i("Other", "返回结果:"+status);
      if(status == 200){
       
       System.out.println( filePost.getResponseCharSet());
       String result = new String(filePost.getResponseBodyAsString());
       
       
       System.out.println("--"+result);
       return result;
      }else{
       throw new IllegalStateException("服务器状态异常");
      }
      
      
     }

    }

    ----------------------------------------------------------------------------------------------------------

    二、 以下是Activity中调用Service类的方法,进行批量上传图片

     /**
      * 批量上传图片
      */
     private void filePostForSDPic() {
      new Thread(){
       
       public void run() {
        try {
         saveDir = Environment.getExternalStorageDirectory()+ "/chonPic";
           
         String filePath = saveDir;
         String path = "http://这里的路径就不贴上去了,保证成功并万无一失";
         String result = DataService.sendDataByHttpClientPost(path, "admin", fileList);
         
         Toast.makeText(MainActivity.this,"pic_haha:"+ result, 0).show();
        
        
        } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       };
       
      }.start();
     }
     

    ---------------------------------------------------------------------------------------

    调用批量上传方法需要引入阿帕奇3 个jar包

    1.  commons-codec-1.3.jar

    2. commons-httpclient-3.1.jar

    3.  commons-logging-1.1.jar

    大家可以去网上下载,也可以去我的博客的资源下载!

  • 相关阅读:
    synchronize模块
    ansible 的user模块
    copy src remote_src false表示本地,true在远程
    import_tasks: tasks/sometasks.yml
    ansible 变量传递到include
    ansible unarchive模块
    防火墙在setup进入不了
    telegram汉化和代理
    Ubuntu 18.04 一键安装深度截图工具 Deepin Screenshot
    8086汇编语言程序设计——子程序与模块化
  • 原文地址:https://www.cnblogs.com/james1207/p/3275701.html
Copyright © 2020-2023  润新知