gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:07:20 28 4
gpt4 key购买 nike

我有这个问题。在将 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);

然后像这样将我的图片添加到 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一样上传

谁能帮帮我..

更新---

日志

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)

最佳答案

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

并使用多部分实体使用此链接。 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"));

关于android - 将 PNG 文件上传到服务器后无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29739119/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com