将PNG文件上传到服务器后无法正常工作

时间:2015-04-20 03:59:29

标签: android http post

我有这个问题。在将PNG文件上传到服务器之前,我将它转换为像这样的字节数组

Bitmap bitmap = BitmapFactory.decodeFile(signature_path);          
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);

然后将我的Image添加到POST请求中,如此

JSONArray nameValueArray = new JSONArray();
signature_path_value.put("name", "uploadfile");
signature_path_value.put("value", image_str);
nameValueArray.put(signature_path_value);

data.put(NAME_VALUE_LIST, nameValueArray);

String restData = org.json.simple.JSONValue.toJSONString(data);

HttpClient httpClient = new DefaultHttpClient();
HttpPost req = new HttpPost(rest_url);
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(METHOD, SET_ENTRY));
nameValuePairs.add(new BasicNameValuePair(INPUT_TYPE, JSON));
nameValuePairs.add(new BasicNameValuePair(RESPONSE_TYPE, JSON));
nameValuePairs.add(new BasicNameValuePair(REST_DATA, restData));
req.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Send POST request
httpClient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
HttpResponse res = httpClient.execute(req);

但在我的网站图片中看起来像这样

iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABHNCSVQICAgIfAhkiAAAEIFJREFUaIG9mXuQ3FWVxz/n/h79mJ5JmDxIgsGAETFRggq+WDAp8MXiKoszy4qsItZS6OJjV2V9QcddnxRrIZTrWoII5QLTxaKr4sK6MCUoGIUYMAkSEpIAYZIJmcyju3+Pe+/ZP37dM4kJmmRXb9WtX03P7f6d7znf7znn3iv8H8YjzUvOHt2zZ92

为什么会这样?为什么它没有上传img.png

任何人都可以帮助我..

更新---

logcat的

04-20 12:28:22.535: E/AndroidRuntime(18823): FATAL EXCEPTION: main
04-20 12:28:22.535: E/AndroidRuntime(18823): Process: com.stonewheeltest, PID: 18823
04-20 12:28:22.535: E/AndroidRuntime(18823): java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
04-20 12:28:22.535: E/AndroidRuntime(18823):    at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:85)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at newCrm.Sugercrm_sync.uploadFile(Sugercrm_sync.java:207)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at newCrm.Sugercrm_sync.access$0(Sugercrm_sync.java:192)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at newCrm.Sugercrm_sync$AddNewRecord.onPostExecute(Sugercrm_sync.java:185)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at newCrm.Sugercrm_sync$AddNewRecord.onPostExecute(Sugercrm_sync.java:1)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.AsyncTask.finish(AsyncTask.java:632)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.os.Looper.loop(Looper.java:146)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at android.app.ActivityThread.main(ActivityThread.java:5653)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at java.lang.reflect.Method.invoke(Method.java:515)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
04-20 12:28:22.535: E/AndroidRuntime(18823):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

请参阅此链接以上传图片而不使用多部分 http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106

并使用multipart实体使用此链接。 http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/

你可以使用

reqEntity.addPart("uploadfile", new FileBody(<File object of your image path>, "image/jpeg,image/png"));