View Code
public String postFile(String pathUrl, byte[] data) {        StringBuilder sbuilder = new StringBuilder();        try {            URL url = new URL(pathUrl);            HttpURLConnection httpConn = (HttpURLConnection) url                    .openConnection();            //设置连接属性            httpConn.setDoOutput(true);// 使用 URL 连接进行输出            httpConn.setDoInput(true);// 使用 URL 连接进行输入            httpConn.setUseCaches(false);// 忽略缓存            httpConn.setRequestMethod("POST");// 设置URL请求方法            // 设置请求属性            httpConn.setRequestProperty("Content-length", "" + data.length);            httpConn.setRequestProperty("Content-Type",                    "application/octet-stream");            httpConn.setRequestProperty("Connection", "Keep-Alive");            httpConn.setRequestProperty("Charset", "UTF-8");            // 建立输出流,并写入数据            OutputStream outputStream = httpConn.getOutputStream();            outputStream.write(data);            outputStream.close();                        // 获得响应状态            RESPONSE_CODE = httpConn.getResponseCode();            if (HttpURLConnection.HTTP_OK == RESPONSE_CODE) {                String readLine;                BufferedReader responseReader;                // 处理响应流,必须与服务器响应流输出的编码一致                responseReader = new BufferedReader(new InputStreamReader(                        httpConn.getInputStream(), "UTF-8"));                while ((readLine = responseReader.readLine()) != null) {                    sbuilder.append(readLine);                }                responseReader.close();            }        } catch (Exception ex) {            sbuilder.append(ex.getMessage());            ex.printStackTrace();        }        return sbuilder.toString();    }

可以直接拷贝下来用。只要将File to byte[]:

View Code
    public static byte[] getBytesFromFile(File f){        if(f==null)return null;        try {            FileInputStream stream = new FileInputStream(f);            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);            byte[] b = new byte[1024];            for(int n;(n=stream.read(b))!=-1;){                out.write(b,0,n);            }            stream.close();            out.close();            return out.toByteArray();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return null;    }

更多相关文章

  1. Android(安卓)时间格式转换
  2. 【实战】android网页源代码查看器
  3. android app 与电脑wifi通信
  4. android检查网络连接状态
  5. Android(安卓)S5PV210 fimc驱动分析 - fimc_regs.c
  6. android 无线连接eclipse
  7. Android(安卓)AudioFlinger
  8. Android实时绘制效果(二)
  9. Android中listview中的button

随机推荐

  1. android:textAppearance设置文字外观
  2. Android清除本地数据缓存代码案例
  3. Android(安卓)实现联网——在线程中联网
  4. android 获取sim卡运营商信息
  5. Android(安卓)widget组件(一):Button、 Edit
  6. Android在layout xml中使用include
  7. android保持在休眠时,后台程序继续运行(让
  8. Android的SMS短消息格式和主要字段
  9. 动画学习笔记-Android(安卓)Animation
  10. Android权限问题整理