自己写就比较麻烦了,还是用apache的multipart的库,方便点儿

解决方案 »

  1.   

    用了,但是初手不会写了HttpPost httppost = new HttpPost(urlServer);
           
            MultipartEntity mpEntity = new MultipartEntity(); //文件传输
            ContentBody cbFile = new FileBody(new File(listd.get(0).toString()));
            
            mpEntity.addPart("pic[]", cbFile); // <input type="file" name="userfile" />  对应的
            httppost.setEntity(mpEntity);
            System.out.println("executing request " + httppost.getRequestLine());
             
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();
         
            System.out.println(response.getStatusLine());//通信Ok
    请问多文件怎么改啊,我现在手写.get(0)
    怎么写这个循环,请指教
      

  2.   

    mpEntity.addPart("pic[]", cbFile);
    有几个文件就加几个呗
      

  3.   

    HttpPost httppost = new HttpPost(urlServer);
            for(int i=0;i<listd.size();i++)
            {
            File file = new File(listd.get(i).toString());
            
            MultipartEntity mpEntity = new MultipartEntity(); //文件传输
            ContentBody cbFile = new FileBody(file);
            mpEntity.addPart("pic[]", cbFile); // <input type="file" name="userfile" />  对应的
            Log.i("post", cbFile.toString());
            }
            httppost.setEntity(mpEntity);
            System.out.println("executing request " + httppost.getRequestLine());
             
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();
         
            System.out.println(response.getStatusLine());//通信Ok
            String json="";
            String path="";
            if (resEntity != null) {
             System.out.println(EntityUtils.toString(resEntity,"utf-8"));
              json=EntityUtils.toString(resEntity,"utf-8");
              JSONObject p=null;
              try{
                  p=new JSONObject(json);
                  path=(String) p.get("path");
              }catch(Exception e){
                  e.printStackTrace();
              }
            }
            if (resEntity != null) {
              resEntity.consumeContent();
            }
         
            httpclient.getConnectionManager().shutdown();
            return path;
          }
      

  4.   

    你这代码写的……能编译都是个奇迹啊for(int i=0;i<listd.size();i++)
            {
            File file = new File(listd.get(i).toString());
             
            MultipartEntity mpEntity = new MultipartEntity(); //文件传输
            ContentBody cbFile = new FileBody(file);
            mpEntity.addPart("pic[]", cbFile); // <input type="file" name="userfile" />  对应的
            Log.i("post", cbFile.toString());
            }
            httppost.setEntity(mpEntity);MultipartEntity 只需要一个,你放到 for 循环里面干啥。
    而且,如果mpEntity是在for里面new出来的,你在for外面httppost.setEntity(mpEntity);根本编译不过。估计是你在之前的哪个位置又new了一个
      

  5.   


    大哥,我这说的是多图,不是文字+图
    我的也是需要上传多图但是还需要上传文字,就是文字和多图同步上传啊!你的解决了告诉我一下!原理都是一样的啊!
    呵呵,我也一样,我在也是多图加文字,昨天多图解决了,文字没解决,今天换了方法,图也没解决了嗯嗯!我也是啊!就是我的后台是用PHP啊!我只把单图片上传解决了啊!但是现在多图和文字同步上传到服务器还没有解决啊!这个问题都困扰我好久了啊!
      

  6.   

    感谢@youngc527大神的帮助,已经全部解决。楼上兄弟,我已经全部ok
      

  7.   

    哥们!发给我一下看看!你怎么实现的?我邮箱[email protected]