如何在 PHP 中解码 Java 流

我请求了沃尔玛报告API,结果将返回zip文件流。参考API文档,它给出了一个用Java代码实现的例子,如下所示:


if (response.getStatus() == Response.Status.OK.getStatusCode() && response.hasEntity()) {

  InputStream inputStream = (InputStream)response.getEntity();

  try {

    String header = response.getHeaderString("Content-Disposition");

    if(header != null && !("").equals(header)) {

      if(header.contains("filename")){

        //header value will be something like:

        //attachment; filename=10000000354_2016-01-15T23:09:54.438+0000.zip

        int length = header.length();

        String fileName = header.substring(header.indexOf("filename="),length);

        System.out.println("filenameText " + fileName);

        String [] str = fileName.split("=");

        System.out.println("fileName: " + str[1]);

        //replace "/Users/anauti1/Documents/" below with your values

        File reportFile = new File("/Users/anauti1/Documents/" + str[1].toString());

        OutputStream outStream = new FileOutputStream(reportFile);

        byte[] buffer = new byte[8 * 1024];

        int bytesRead;

        while ((bytesRead = inputStream.read(buffer)) != -1) {

          outStream.write(buffer, 0, bytesRead);

        }

        IOUtils.closeQuietly(inputStream);

        IOUtils.closeQuietly(outStream);

      }

    }

  }

  catch (Exception ex){

    System.out.print("Exception: " + ex.getMessage());

  }

}

它会下载zip文件但文件数据已损坏。这可能是使用Java代码传输字节流之类的原因。这是如何转换字节流的问题。你能帮我吗?顺便说一句,我已将部分流剪切如下:


蝴蝶刀刀
浏览 112回答 2
2回答

尚方宝剑之说

你解决问题了吗?下面的简单代码对我有用&nbsp; &nbsp; $fp = fopen('/your path where you store the zip file/'.$filename, 'w+');&nbsp;&nbsp; &nbsp; if ($fp == FALSE){&nbsp;&nbsp; &nbsp; &nbsp; print "File not opened<br>";&nbsp;&nbsp; &nbsp; &nbsp; exit;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; fwrite($fp, $response);&nbsp; &nbsp; fclose($fp);$response是 API 响应的正文,它将是 $filename从标头中获取的不可读格式的 zip 文件名。

喵喔喔

似乎 Json Encoded 并且您必须在 php 脚本中使用 json 对其进行解码,因此: json_decode(string,array) string = 您收集的编码响应。array = True 如果你想要结果数据的数组。2- 使用 header('Content-Type: application/json') (此标头在发送或接收响应之前最有用)3 -错误处理:json_last_error() json_last_error_msg()try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $header = $resultInfo->getHeader('Content-Disposition');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!empty($header)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strpos($header, 'filename') !== false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filename = substr($header, strpos($header, 'filename'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $str = explode('=', $filename);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $body = $resultInfo->getBody();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fp = fopen(storage_path("csv/{$str[1]}"), 'w');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##uncomment below line if response not valid may help you.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # header('Content-Type: application/json');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dbody = json_decode($body,true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fwrite($fp, $dbody);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fclose($fp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (\Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $e->getMessage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }希望能有所帮助
打开App,查看更多内容
随时随地看视频慕课网APP