Android中實(shí)現(xiàn)OkHttp上傳文件到服務(wù)器并帶進(jìn)度
在上一講中 OkHttp下載文件并帶進(jìn)度條 中,我們知道怎樣去下載文件了。那上傳文件呢
一、編寫服務(wù)器端
在上一講服務(wù)器下新建UploadFileServlet,代碼如下:然后重啟服務(wù)器!
@WebServlet("/UploadFileServlet") @MultipartConfig public class UploadFileServlet extends HttpServlet { private static final long serialVersionUID = 1L; public UploadFileServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doPost=="); request.setCharacterEncoding("utf-8"); //獲取file命名的part,注意要與Android端一樣 Part part = request.getPart("file"); // 獲取請(qǐng)求頭,請(qǐng)求頭的格式:form-data; name="file"; filename="snmp4j--api.zip" String header = part.getHeader("content-disposition"); System.out.println(header); String fileName = getFileName(header); // 存儲(chǔ)路徑 String savePath = "D:/huang/upload"; // 把文件寫到指定路徑 part.write(savePath + File.separator + fileName); response.setCharacterEncoding("UTF-8"); PrintWriter writer = response.getWriter(); writer.print("上傳成功"); } public String getFileName(String header) { /** * header 為 form-data; name="file"; filename="dial.png" * String[] tempArr1 = * header.split(";");代碼執(zhí)行完之后,在不同的瀏覽器下,tempArr1數(shù)組里面的內(nèi)容稍有區(qū)別 * 火狐或者google瀏覽器下:tempArr1={form-data,name="file",filename= * "snmp4j--api.zip"} * IE瀏覽器下:tempArr1={form-data,name="file",filename="E:\snmp4j--api.zip"} */ String[] tempArr1 = header.split(";"); /** * 火狐或者google瀏覽器下:tempArr2={filename,"snmp4j--api.zip"} * IE瀏覽器下:tempArr2={filename,"E:\snmp4j--api.zip"} */ String[] tempArr2 = tempArr1[2].split("="); // 獲取文件名,兼容各種瀏覽器的寫法 String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", ""); return fileName; } }
二、Android端
1.布局,上一講activity_main代碼中添加 :
<Button android:id="@+id/ok_post_file" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上傳文件" /> <TextView android:id="@+id/post_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="0" /> <ProgressBar android:id="@+id/post_progress" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" />
2.OkHttpUtil新增上傳文件方法:
public static void postFile(String url, final ProgressListener listener, Callback callback, File...files){ MultipartBody.Builder builder = new MultipartBody.Builder(); builder.setType(MultipartBody.FORM); Log.i("huang","files[0].getName()=="+files[0].getName()); //第一個(gè)參數(shù)要與Servlet中的一致 builder.addFormDataPart("file",files[0].getName(), RequestBody.create(MediaType.parse("application/octet-stream"),files[0])); MultipartBody multipartBody = builder.build(); Request request = new Request.Builder().url(url).post(new ProgressRequestBody(multipartBody,listener)).build(); okHttpClient.newCall(request).enqueue(callback); }
3.ProgressRequestBody是自定義RequestBody類,用來監(jiān)聽進(jìn)度:
public class ProgressRequestBody extends RequestBody { public static final int UPDATE = 0x01; private RequestBody requestBody; private ProgressListener mListener; private BufferedSink bufferedSink; private MyHandler myHandler; public ProgressRequestBody(RequestBody body, ProgressListener listener) { requestBody = body; mListener = listener; if (myHandler==null){ myHandler = new MyHandler(); } } class MyHandler extends Handler { //放在主線程中顯示 public MyHandler() { super(Looper.getMainLooper()); } @Override public void handleMessage(Message msg) { switch (msg.what){ case UPDATE: ProgressModel progressModel = (ProgressModel) msg.obj; if (mListener!=null)mListener.onProgress(progressModel.getCurrentBytes(),progressModel.getContentLength(),progressModel.isDone()); break; } } } @Override public MediaType contentType() { return requestBody.contentType(); } @Override public long contentLength() throws IOException { return requestBody.contentLength(); } @Override public void writeTo(BufferedSink sink) throws IOException { if (bufferedSink==null){ bufferedSink = Okio.buffer(sink(sink)); } //寫入 requestBody.writeTo(bufferedSink); //刷新 bufferedSink.flush(); } private Sink sink(BufferedSink sink) { return new ForwardingSink(sink) { long bytesWritten = 0L; long contentLength = 0L; @Override public void write(Buffer source, long byteCount) throws IOException { super.write(source, byteCount); if (contentLength==0){ contentLength = contentLength(); } bytesWritten += byteCount; //回調(diào) Message msg = Message.obtain(); msg.what = UPDATE; msg.obj = new ProgressModel(bytesWritten,contentLength,bytesWritten==contentLength); myHandler.sendMessage(msg); } }; } }
4.在MainActivity添加上傳按鈕點(diǎn)擊事件,代碼如下:
File file = new File(basePath + "/1.mp4"); String postUrl = "http://192.168.0.104:8080/OkHttpServer/UploadFileServlet"; OkHttpUtil.postFile(postUrl, new ProgressListener() { @Override public void onProgress(long currentBytes, long contentLength, boolean done) { Log.i(TAG, "currentBytes==" + currentBytes + "==contentLength==" + contentLength + "==done==" + done); int progress = (int) (currentBytes * 100 / contentLength); post_progress.setProgress(progress); post_text.setText(progress + "%"); } }, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response != null) { String result = response.body().string(); Log.i(TAG, "result===" + result); } } }, file);
相關(guān)效果圖:
上傳完成后,在電腦D:\huang\upload下可以看到:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義view實(shí)現(xiàn)圓環(huán)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)圓環(huán)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02FragmentTabHost FrameLayout實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了FragmentTabHost和FrameLayout實(shí)現(xiàn)底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android源碼學(xué)習(xí)之觀察者模式應(yīng)用及優(yōu)點(diǎn)介紹
定義對(duì)象間一種一對(duì)多的依賴關(guān)系,使得當(dāng)一個(gè)對(duì)象改變狀態(tài),則所有依賴于它的對(duì)象都會(huì)得到通知并被自動(dòng)更新等等,需要了解的朋友可以參考下2013-01-01Android Lottie實(shí)現(xiàn)中秋月餅變明月動(dòng)畫特效實(shí)例
Lottie是Airbnb開源的一個(gè)支持 Android、iOS 以及 ReactNative,利用json文件的方式快速實(shí)現(xiàn)動(dòng)畫效果的庫,下面這篇文章主要給大家介紹了關(guān)于Android Lottie實(shí)現(xiàn)中秋月餅變明月動(dòng)畫特效的相關(guān)資料,需要的朋友可以參考下2021-09-09關(guān)于如何使用Flutter開發(fā)執(zhí)行操作系統(tǒng)shell命令的工具詳解
本文主要介紹如何在Flutter應(yīng)用中開發(fā)一個(gè)Android終端命令行工具,包括終端命令行頁面的布局設(shè)計(jì)、與Shell通信的基本原理、輸入輸出處理的基本技巧等,以及如何在具體應(yīng)用中利用終端命令行工具來執(zhí)行系統(tǒng)命令和與用戶進(jìn)行交互2023-06-06Android學(xué)習(xí)教程之日歷庫使用(15)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)教程之日歷庫使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android開發(fā)使用UncaughtExceptionHandler捕獲全局異常
本文主要介紹在Android開發(fā)中使用UncaughtExceptionHandler捕獲全局異常,需要的朋友可以參考下。2016-06-06AndroidStudio報(bào)錯(cuò)Emulator:PANIC:Cannot find AVD system path. P
這篇文章主要介紹了AndroidStudio報(bào)錯(cuò)Emulator:PANIC:Cannot find AVD system path. Please define ANDROID_SDK_ROOT完整的解決方案2021-08-08