简体   繁体   English

如何将使用GZIP压缩的字符串从Java App发送到PHP Web服务

[英]How to send string compressed with GZIP from Java App to PHP web service

I have this issue with GZIP compression: 我有GZIP压缩这个问题:

I need to send by POST method a huge JSON string, which is too big to be accept like URL (Ex: http://localhost/app/send/ JSON STRING ENCODED BY BASE64 ), than it result in HTTP error 403 我需要通过POST方法发送一个巨大的JSON字符串,这个字符串太大而不能接受像URL(例如: http:// localhost / app / send / JSON STRING ENCODED by BASE64 ),而不是导致HTTP错误403

so, I need to compress my json and I found a way to do it with GZIP compression, which I can decompress with gzdecode() in PHP. 所以,我需要压缩我的json,我找到了一种方法来使用GZIP压缩,我可以用PHP中的gzdecode()解压缩。

but it doesn't work... 但它不起作用......

my functions compress() and decompress() works fine inside my Java App, but when I send it to webservice, something goes wrong and gzdecode() doesn't work. 我的函数compress()和decompress()在我的Java App中运行良好,但是当我将它发送到webservice时,出现问题并且gzdecode()不起作用。 I have no idea what I missing, I need some help 我不知道我错过了什么,我需要一些帮助

functions used in java app (client) java app(client)中使用的函数

    public String Post(){
     String retorno = "";
     String u = compress(getInput());
     u = URLEncoder.encode(URLEncoder.encode(u, "UTF-8"));

     URL uri = new URL(url + u);

     HttpURLConnection conn = (HttpURLConnection) uri.openConnection();

     conn.setDoOutput(false);
     conn.setRequestMethod(getMethod());

     conn.setRequestProperty("Content-encoding", "gzip");
     conn.setRequestProperty("Content-type", "application/octet-stream");

     BufferedReader buffer = new BufferedReader(
                    new InputStreamReader((conn.getInputStream())));

     String r = "";
     while ((r = buffer.readLine()) != null) {
                retorno = r + "\n";
     }
     return retorno;
}

GZIP compress function (client) GZIP压缩功能(客户端)

public static String compress(String str) throws IOException {

        byte[] blockcopy = ByteBuffer
                .allocate(4)
                .order(java.nio.ByteOrder.LITTLE_ENDIAN)
                .putInt(str.length())
                .array();
        ByteArrayOutputStream os = new ByteArrayOutputStream(str.length());
        GZIPOutputStream gos = new GZIPOutputStream(os);
        gos.write(str.getBytes());
        gos.close();
        os.close();
        byte[] compressed = new byte[4 + os.toByteArray().length];
        System.arraycopy(blockcopy, 0, compressed, 0, 4);
        System.arraycopy(os.toByteArray(), 0, compressed, 4,
                os.toByteArray().length);

        return Base64.encode(compressed);

    }

method php used to receive a URL (server, using Slim/PHP Framework) 方法php用于接收URL(服务器,使用Slim / PHP Framework)

init::$app->post('/enviar/:obj/', function( $obj ) {
     $dec = base64_decode(urldecode( $obj ));//decode url and decode base64 tostring
     $dec = gzdecode($dec);//here is my problem, gzdecode() doesn't work
}

post method 发布方法

public Sender() throws JSONException {   
    //
    url = "http://192.168.0.25/api/index.php/enviar/";
    method = "POST";
    output = true;
    //
}

As noticed in some of the comments. 正如一些评论中所注意到的那样。

  1. Bigger data should be send as a POST request instead of GET. 较大的数据应作为POST请求而不是GET发送。 URL params should be used only for single variables. URL参数只能用于单个变量。 As you noticed the URL length is limited to few kB and it's not very good idea to send larger data this way (even though GZIP compressed). 正如您所注意到的,URL长度限制为几KB,以这种方式发送更大的数据并不是一个好主意(即使GZIP压缩)。

  2. Your GZIP compression code seems to be wrong. 您的GZIP压缩代码似乎是错误的。 Please try this: 请试试这个:

  public static String compress(String str) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream(str.length());
    GZIPOutputStream gos = new GZIPOutputStream(os);
    gos.write(str.getBytes());
    os.close();
    gos.close();
    return Base64.encodeToString(os.toByteArray(),Base64.DEFAULT);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Java发送压缩(gzip)JSON作为对Ajax请求的响应? - How can I send compressed (gzip) JSON as response to an Ajax Request, from Java? 如何使用 Java 代码 GZip 解压缩压缩的 String 数据 - How to GZip decompress a compressed String data with Java code 如何从java Web应用程序发送电子邮件 - How to send email from a java web app 如何将图像从App Inventor发送到使用FormDataParam的Java Web服务 - How to send image from App Inventor to a java web service that uses FormDataParam 如果在BlackBerry中使用相同的数据压缩数据,如何在Java中使用GZIP解压缩数据 - How to Decompress the data using GZIP in java if the data is compressed with the same in BlackBerry 如何使用 android volley 将字符串值从应用程序传递到 php web 服务 - How to pass String value from a app to php web service using android volley PHP 中压缩的字符串与 java 中压缩的字符串不匹配 - String compressed in PHP doesn't match with string compressed in java 如何从Java Web项目向静态服务发送参数 - How to send parameters to a restful service from a java web project java - 如何从 RESTful Web 服务发送图像? - java - how to send images from a RESTful web service? 如何从Java客户端向C#Web服务发送数据 - How to send data to c# web service from java client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM