Retrofit2ファイルのアップロードとダウンロード



Retrofit2 File Upload



Retrofit2ファイルのアップロードとダウンロード

まず第一に、あなたはまだAPIインターフェースでメソッドを作成する必要があります

/** * download file * If you download large files, you must add @Streaming annotations. * * @param fileUrl file path * @return request call */ @GET Call download(@Url String fileUrl)

次に、上記のようにレトロフィットオブジェクトを作成します(レトロフィットオブジェクトを作成するたびに多くの重複コードを使用するため、パブリックメソッドに抽出できます)

リクエストインターフェイスをインスタンス化します。

//I found a picture on Baidu and split his address. The url before the last slash is set to baseUrl, and the slash is set here. //Address split: .baseUrl('https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=e3dc64d05a3d26972ed30f5b6dc0d5c6/') //The overall address is https://gss3.bdstatic.com/Po3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=e3dc64d05a3d26972ed30f5b6dc0d5c6/ (split here) 241f95cad1c8a7868a2713146c09c93d70cf509e.jpg Call download = retrofit.create(Api.class).download('241f95cad1c8a7868a2713146c09c93d70cf509e.jpg') download.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response != null && response.isSuccessful()) { // writeResponseBodyToDisk is the download and save local tool class I wrote, you can refer to boolean toDisk = writeResponseBodyToDisk(response.body()) if (toDisk) { System.out.println('Download success please check') } else { System.out.println('Download failed, please try again later') } } else { System.out.println('Server returns error') } } @Override public void onFailure(Call call, Throwable t) { //Connection failed, most of them are caused by network unavailability System.out.println('Network not available') } })

writeResponseBodyToDisk

/** * Download to local * * @param body content * @return success or failure */ private boolean writeResponseBodyToDisk(ResponseBody body) { try { / / Determine whether the folder exists File files = new File(SD_HOME_DIR)// with a directory folder if (!files.exists()) { //Create it if it doesn't exist files.mkdirs() } / / Create a file File futureStudioIconFile = new File(SD_HOME_DIR + 'download.jpg') / / Initialize the input stream InputStream inputStream = null / / Initialize the output stream OutputStream outputStream = null try { / / Set the byte of each read and write byte[] fileReader = new byte[4096] long fileSize = body.contentLength() long fileSizeDownloaded = 0 / / Request returned byte stream inputStream = body.byteStream() / / Create an output stream outputStream = new FileOutputStream(futureStudioIconFile) / / Read operation while (true) { int read = inputStream.read(fileReader) if (read == -1) { break } / / Write operation outputStream.write(fileReader, 0, read) fileSizeDownloaded += read } //Refresh outputStream.flush() return true } catch (IOException e) { return false } finally { if (inputStream != null) { / / Close the input stream inputStream.close() } if (outputStream != null) { / / Close the output stream outputStream.close() } } } catch (IOException e) { return false } }

ダウンロードした画像表示

ご不明な点がございましたら、メッセージを残してください!

全文が完成しました!!!