我需要上传图像到Web服务器,它需要ImageContent是在字节[]在文档它说base64Binary的,但我想base64编码字符串,没有用安卓:通过HTTP GET或POST请求发送图像的byte []
这是我的课:
private class background extends AsyncTask<byte[],Void,String> {
String url = "http://www.sample.com/_mobfiles/CLS_Account.asmx/UploadImage";
String charset = "UTF-8";
String param1 = "jpg";
@Override
protected String doInBackground(byte[]... params) {
try {
String query = String.format("ImageContent=%s&imageExtenstion=%s", params[0], URLEncoder.encode(param2, charset));
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
String contentType = connection.getHeaderField("Content-Type");
String charset = null;
for (String param : contentType.replace(" ", "").split(";")) {
if (param.startsWith("charset=")) {
charset = param.split("=", 2)[1];
break;
}
}
if (charset != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response, charset))) {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
}
}
else {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = response.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] arr = buffer.toByteArray();
String decoded = new String(arr, "UTF-8");
System.out.println(decoded);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
返回(java.io.FileNotFoundException)
和Base64编码的回报(java.net.ProtocolException:意外的状态行:对象移动)
这里是文件:
HTTP GET
以下是一个示例HTTP GET请求和响应。显示的占位符与实际值替换。
GET /_mobfiles/CLS_Account.asmx/UploadImage?ImageContent=base64Binary&imageExtenstion=string HTTP/1.1
Host: www.sample.com
和响应是象
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
HTTP POST
以下是一个示例HTTP POST请求和响应。显示的占位符与实际值替换。
POST /_mobfiles/CLS_Account.asmx/UploadImage HTTP/1.1
Host: www.sample.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
ImageContent=base64Binary&imageExtenstion=string
和响应就像
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
我应该在哪里写的是什么? –
尝试在浏览器上尝试
,其中yyy指图像类型,xxxx指向base64编码图像在此尝试https://jsfiddle.net/o4mut2uv/并共享。使用小图像快速。 –
user1615664
它显示图像ok –