亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 PHP 中解碼 Java 流

如何在 PHP 中解碼 Java 流

PHP
蝴蝶刀刀 2022-06-11 09:20:44
我請求了沃爾瑪報告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代碼傳輸字節流之類的原因。這是如何轉換字節流的問題。你能幫我嗎?順便說一句,我已將部分流剪切如下:
查看完整描述

2 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

你解決問題了嗎?下面的簡單代碼對我有用


    $fp = fopen('/your path where you store the zip file/'.$filename, 'w+'); 

    if ($fp == FALSE){ 

      print "File not opened<br>"; 

      exit; 

    }

    fwrite($fp, $response);

    fclose($fp);

$response是 API 響應的正文,它將是 $filename從標頭中獲取的不可讀格式的 zip 文件名。


查看完整回答
反對 回復 2022-06-11
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

似乎 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 {

                        $header = $resultInfo->getHeader('Content-Disposition');

                        if (!empty($header)) {

                            if (strpos($header, 'filename') !== false) {

                                $filename = substr($header, strpos($header, 'filename'));

                                $str = explode('=', $filename);

                                $body = $resultInfo->getBody();

                                $fp = fopen(storage_path("csv/{$str[1]}"), 'w');

                                ##uncomment below line if response not valid may help you.                            

                                # header('Content-Type: application/json');

                                $dbody = json_decode($body,true);

                                fwrite($fp, $dbody);

                                fclose($fp);

                            }

                        }

                    } catch (\Exception $e) {

                        echo $e->getMessage();

                    }

希望能有所幫助


查看完整回答
反對 回復 2022-06-11
  • 2 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號