duanjiaolao1187 2015-06-17 14:02
浏览 74

使用PHP将图像上载到服务器时出错

I'm trying to upload an image to a server by using php. But I'm getting following error:

Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 171:my server name/upload.php?sel_bar_code=ab12&text=hello&image=[encoded image to base64 comes hear]

I'm not getting this error on android. Below is my code that uploades images to the server.

if (isImage) {
            imgPreview.setVisibility(View.VISIBLE);
            vidPreview.setVisibility(View.GONE);
            // bimatp factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            // down sizing image as it throws OutOfMemory Exception for larger
            // images
            options.inSampleSize = 8;

            bitmap = BitmapFactory.decodeFile(filePath, options);
            imgPreview.setImageBitmap(bitmap);

            ByteArrayOutputStream ByteStream=new  ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG,100, ByteStream);
            byte [] b=ByteStream.toByteArray();
            image_value=Base64.encodeToString(b, Base64.DEFAULT);

Background process code:

protected String doInBackground(Void... params) {
            return uploadFile();
        }

        @SuppressWarnings("deprecation")
        private String uploadFile() {
            String responseString = null;

            HttpClient httpclient = new DefaultHttpClient();
            List<NameValuePair> qparams = new ArrayList<NameValuePair>();
         // These params should ordered in key
            qparams.add(new BasicNameValuePair("sel_bar_code", product_id));

            HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL+"sel_bar_code="+product_id+"&text="+text_value +"&image="+image_value);

            try {
                AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
                        new ProgressListener() {

                            @Override
                            public void transferred(long num) {
                                publishProgress((int) ((num / (float) totalSize) * 100));
                            }
                        });

                File sourceFile = new File(filePath);

                // Adding file data to http body
                entity.addPart("image", new FileBody(sourceFile));

                // Extra parameters if you want to pass to server
                entity.addPart("website",
                        new StringBody("www.google.com"));
                entity.addPart("email", new StringBody("info@gmail.com"));

                totalSize = entity.getContentLength();
                httppost.setEntity(entity);

                // Making server call
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity r_entity = response.getEntity();

                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    // Server response
                    responseString = EntityUtils.toString(r_entity);
                } else {
                    responseString = "Error occurred! Http Status Code: "
                            + statusCode;
                }

            } catch (ClientProtocolException e) {
                responseString = e.toString();
            } catch (IOException e) {
                responseString = e.toString();
            }

            return responseString;

        }
  • 写回答

1条回答 默认 最新

  • dongtangze6393 2015-06-17 14:29
    关注

    Yesterday I develop the same process...

    • The difference to my app need to send photos one by one. For the other hand, I send image bytes as String, at final I had problems with Base64 compression because the flag 'Base64.DEFAULT' adds intros in the string in my case. I solve this using flag 'Base64.NO_WRAP'. For omit this problem, you need to copy the file into public directory before send to verify that file is not corrupt.

    • It's posible too that the server have a low size file that can send, the server part is which limit the size of files that receives.

    If I can helped you tell me and good programming!

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程