第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 PHP 中解碼 Java 流

如何在 PHP 中解碼 Java 流

PHP
蝴蝶刀刀 2022-06-11 09:20:44
我請(qǐng)求了沃爾瑪報(bào)告API,結(jié)果將返回zip文件流。參考API文檔,它給出了一個(gè)用Java代碼實(shí)現(xiàn)的例子,如下所示: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());  }}它會(huì)下載zip文件但文件數(shù)據(jù)已損壞。這可能是使用Java代碼傳輸字節(jié)流之類的原因。這是如何轉(zhuǎn)換字節(jié)流的問題。你能幫我嗎?順便說一句,我已將部分流剪切如下:
查看完整描述

2 回答

?
尚方寶劍之說

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

你解決問題了嗎?下面的簡(jiǎn)單代碼對(duì)我有用


    $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 響應(yīng)的正文,它將是 $filename從標(biāo)頭中獲取的不可讀格式的 zip 文件名。


查看完整回答
反對(duì) 回復(fù) 2022-06-11
?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

似乎 Json Encoded 并且您必須在 php 腳本中使用 json 對(duì)其進(jìn)行解碼,因此: json_decode(string,array) string = 您收集的編碼響應(yīng)。array = True 如果你想要結(jié)果數(shù)據(jù)的數(shù)組。


2- 使用 header('Content-Type: application/json') (此標(biāo)頭在發(fā)送或接收響應(yīng)之前最有用)


3 -錯(cuò)誤處理: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();

                    }

希望能有所幫助


查看完整回答
反對(duì) 回復(fù) 2022-06-11
  • 2 回答
  • 0 關(guān)注
  • 133 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)