您现在的位置: 爱51代码网 >> 范文 >> 文章正文
Android http post 上传图片不成功
public static String newPost(String actionUrl, Map<String, String> params,String imagepath) {         String code = "0";         Log.i("http", actionUrl);         HttpParams httpParameters = new BasicHttpParams();         HttpConnectionParams.setConnectionTimeout(httpParameters, 1000*30);         HttpConnectionParams.setSoTimeout(httpParameters, 1000*30);         HttpConnectionParams.setTcpNoDelay(httpParameters, true);           HttpClient httpclient = new DefaultHttpClient(httpParameters);                   HttpPost httppost = new HttpPost(actionUrl);         MultipartEntity mpEntity = new MultipartEntity();         try {             File imageFile = new File(imagepath);             if(!imageFile.exists()) {                 Log.i("http", "999");                 code = "999";             }             for (Map.Entry<String, String> entry : params.entrySet()) {                    String key = entry.getKey().toString();                    String value = entry.getValue().toString();                    Log.i("Http","KEY:" + key + ",Value:" + value );                    mpEntity.addPart(key, new StringBody(value));                   }             FileBody file = new FileBody(imageFile);             mpEntity.addPart("name", file);             httppost.setEntity(mpEntity);             HttpResponse response = httpclient.execute(httppost);             if (response.getStatusLine().getStatusCode() == 200) {                 code = EntityUtils.toString(response.getEntity());                   System.out.println("result:" + code);                   Log.i("http", code);                               }         } catch (Exception e) {             return code;         }         return code;     } 这个是我昨天刚写的,可用。 

需要用到2个jar包,httpmime-4.0.jar,apache-mime4j-0.6.jar
还有一种是

Java code?public static String post(String actionUrl, Map<String, String> params,             Map<String, File> files) throws IOException {         StringBuilder sb2 = new StringBuilder();         String BOUNDARY = java.util.UUID.randomUUID().toString();         String PREFIX = "--", LINEND = "\r\n";         String MULTIPART_FROM_DATA = "multipart/form-data";         String CHARSET = "UTF-8";           URL uri = new URL(actionUrl);         HttpURLConnection conn = (HttpURLConnection) uri.openConnection();         conn.setReadTimeout(5 * 1000);         conn.setDoInput(true);         conn.setDoOutput(true);         conn.setUseCaches(false);         conn.setRequestMethod("POST");         conn.setRequestProperty("connection", "keep-alive");         conn.setRequestProperty("Charsert", "UTF-8");         conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA                 + ";boundary=" + BOUNDARY);           StringBuilder sb = new StringBuilder();         for (Map.Entry<String, String> entry : params.entrySet()) {             sb.append(PREFIX);             sb.append(BOUNDARY);             sb.append(LINEND);             sb.append("Content-Disposition: form-data; name=\""                    + entry.getKey() + "\"" + LINEND);             sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);             sb.append("Content-Transfer-Encoding: 8bit" + LINEND);             sb.append(LINEND);             sb.append(entry.getValue());             sb.append(LINEND);         }           DataOutputStream outStream = new DataOutputStream(                 conn.getOutputStream());         outStream.write(sb.toString().getBytes());         if (files != null) {             // int i = 0;             for (Map.Entry<String, File> file : files.entrySet()) {                 StringBuilder sb1 = new StringBuilder();                 sb1.append(PREFIX);                 sb1.append(BOUNDARY);                 sb1.append(LINEND);                 // sb1.append("Content-Disposition: form-data; name=\"file"+(i++)+"\"; filename=\""+file.getKey()+"\""+LINEND);                 sb1.append("Content-Disposition: form-data; name=\"userupfile\"; filename=\""                        + file.getKey() + "\"" + LINEND);                 sb1.append("Content-Type: application/octet-stream; charset="                        + CHARSET + LINEND);                 sb1.append(LINEND);                 outStream.write(sb1.toString().getBytes());                   InputStream is = new FileInputStream(file.getValue());                 byte[] buffer = new byte[1024];                 int len = 0;                 while ((len = is.read(buffer)) != -1) {                     outStream.write(buffer, 0, len);                 }                   is.close();                 outStream.write(LINEND.getBytes());             }         }           byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();         outStream.write(end_data);         outStream.flush();           int res = conn.getResponseCode();         InputStream in = null;         if (res == 200) {             in = conn.getInputStream();             int ch;               while ((ch = in.read()) != -1) {                 sb2.append((char) ch);             }             Log.i("CAMERA", sb2.toString());         }           return in == null ? null : sb2.toString();     }

上一页  [1] [2] 

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    android手机无法与eclipse或电脑
    C/C++洗牌算法源代码
    servlet技术实现用户名唯一的验证
    E-business suite system servic
    ZOJ 3700 Ever Dream 文章中单词
    TortoiseGit和msysGit安装及使用
    asp中有一段javascipt的网页鼠标
    sharepoint 2010 获取用户信息Us
    设计包含max函数的队列
    随机从数组中取出指定的不重复的
    ZOJ 3700 Ever Dream 文章中单词
    TortoiseGit和msysGit安装及使用
    sharepoint 2010 获取用户信息Us
    mysql主从同步延迟方案解决的学习
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    恢复使用繁体字可行性研究报告
    安卓本地软件修改密码的实现
    jni thread 退出异常 , nati
    error: Error: No resource 
    Android 3.2上的一个大BUG
    Android 视频流远程监控程序
    java 十六进制字符串转换问题
    QT如何实现左右滑动的按钮
    case expressions must be c
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |