【发布时间】:2014-09-13 15:22:47
【问题描述】:
我遇到上传问题,服务器位于我的 iphone 代码所在的 dotnet 中
-(NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
像 charam 一样工作,但是当我尝试通过我的 android 应用上传时,当 url 中有一些特殊字符(如 (=))时,它总是从服务器返回错误。
见下方代码
Bitmap icon = BitmapFactory.decodeResource(this.contextParam.getResources(),
ByteArrayOutputStream bao = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeToString(ba, Base64.DEFAULT);
Log.e("Data:", ba1);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "dsfsdfsd"));
nameValuePairs.add(new BasicNameValuePair("filenamewithextension", "upload.png"));
nameValuePairs.add(new BasicNameValuePair("newfilename", "Testing upload"));
nameValuePairs.add(new BasicNameValuePair("entityid", "10"));
nameValuePairs.add(new BasicNameValuePair("filestream", ba1));
Log.i(TAG, httppost.getURI().toString());
Log.d(TAG, nameValuePairs.toString());
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
String base64EncodedCredentials = Base64.encodeToString(CREDENTIALS.getBytes(), Base64.NO_WRAP);
httppost.addHeader("Authorization", "Basic " + base64EncodedCredentials);
httppost.addHeader("Content-Type", "application/largedata");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
return getStringFromInputStream(response.getEntity().getContent());
} catch (ClientProtocolException e) {
Log.e(TAG, e.getLocalizedMessage());
} catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage());
}
return responseString;
使用上述代码,服务器总是返回“请求错误”。 任何机构都可以请教它为什么会发生 旁注:我无法更改服务器实现,我必须在 android 端找到解决方案
【问题讨论】:
-
application/largedata看起来像你编造的东西。application/x-www-form-urlencoded将是其他人用于此类内容的内容。否则服务器产生的错误,请在服务器端进行调试。
标签: java android string base64 special-characters