Android:使用HttpURLConnection将文件作为字节数组发送

时间:2017-10-09 21:36:11

标签: android byte httpurlconnection

我想通过POST HTTP请求发送文件但是找不到如何处理它...

我会用字符串参数执行此操作:

String html_params = "myparam=myparam";
byte[] outputInBytes = html_params.getBytes("UTF-8");

URL url = new URL("http://www.myurl.com/senddata.asp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "text/html");

OutputStream os = conn.getOutputStream();
os.write(outputInBytes);
os.close();

response = conn.getResponseCode();

但是我不知道如何将文件转换并发送为字节数组而不是字符串......

感谢您的帮助

0 个答案:

没有答案