1. 程式人生 > >基於html5 websocket API簡單實現斷點上傳檔案

基於html5 websocket API簡單實現斷點上傳檔案

本例項基於html5的 websocket API和netty框架,如果您對兩個技術不太熟悉,可以點選下面連線瞭解

準備:

fiefox瀏覽器或chrome瀏覽器

在classpath中匯入netty類庫,json類庫

好拉,一切準備就緒...

伺服器端:

WebSocketServerInitializer.java

  1. /* 
  2.  * Copyright 2012 The Netty Project 
  3.  * 
  4.  * The Netty Project licenses this file to you under the Apache License,
     
  5.  * version 2.0 (the "License"); you may not use this file except in compliance 
  6.  * with the License. You may obtain a copy of the License at: 
  7.  * 
  8.  *   http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     
  12.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  13.  * License for the specific language governing permissions and limitations 
  14.  * under the License. 
  15.  */
  16. package com.wudasong.breakPoinUploadServer;  
  17. import io.netty.bootstrap.ServerBootstrap;  
  18. import io.netty.channel.Channel;  
  19. import io.netty.channel.socket.nio.NioEventLoop;  
  20. import io.netty.channel.socket.nio.NioServerSocketChannel;  
  21. /** 
  22.  * A HTTP server which serves Web Socket requests at: 
  23.  * 
  24.  * http://localhost:8080/websocket 
  25.  * 
  26.  * Open your browser at http://localhost:8080/, then the demo page will be loaded and a Web Socket connection will be 
  27.  * made automatically. 
  28.  * 
  29.  * This server illustrates support for the different web socket specification versions and will work with: 
  30.  * 
  31.  * <ul> 
  32.  * <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00) 
  33.  * <li>Chrome 6-13 (draft-ietf-hybi-thewebsocketprotocol-00) 
  34.  * <li>Chrome 14+ (draft-ietf-hybi-thewebsocketprotocol-10) 
  35.  * <li>Chrome 16+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17) 
  36.  * <li>Firefox 7+ (draft-ietf-hybi-thewebsocketprotocol-10) 
  37.  * <li>Firefox 11+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17) 
  38.  * </ul> 
  39.  */
  40. publicclass WebSocketServer {  
  41.     privatefinalint port;  
  42.     public WebSocketServer(int port) {  
  43.         this.port = port;  
  44.     }  
  45.     publicvoid run() throws Exception {  
  46.         ServerBootstrap b = new ServerBootstrap();  
  47.         try {  
  48.             b.eventLoop(new NioEventLoop(), new NioEventLoop())  
  49.              .channel(new NioServerSocketChannel())  
  50.              .localAddress(port)  
  51.              .childHandler(new WebSocketServerInitializer());  
  52.             Channel ch = b.bind().sync().channel();  
  53.             System.out.println("Web socket server started at port " + port + '.');  
  54.             System.out.println("Open your browser and navigate to http://localhost:" + port + '/');  
  55.             ch.closeFuture().sync();  
  56.         } finally {  
  57.             b.shutdown();  
  58.         }  
  59.     }  
  60.     publicstaticvoid main(String[] args) throws Exception {  
  61.         int port;  
  62.         if (args.length > 0) {  
  63.             port = Integer.parseInt(args[0]);  
  64.         } else {  
  65.             port = 8082;  
  66.         }  
  67.         new WebSocketServer(port).run();  
  68.     }  
  69. }  

WebSocketServerHandler.java
  1. package com.wudasong.breakPoinUploadServer;  
  2. importstatic io.netty.handler.codec.http.HttpHeaders.*;  
  3. importstatic io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;  
  4. importstatic io.netty.handler.codec.http.HttpMethod.*;  
  5. importstatic io.netty.handler.codec.http.HttpResponseStatus.*;  
  6. importstatic io.netty.handler.codec.http.HttpVersion.*;  
  7. import java.io.File;  
  8. import java.io.IOException;  
  9. import java.io.RandomAccessFile;  
  10. import java.nio.ByteBuffer;  
  11. import java.nio.channels.FileChannel;  
  12. import org.json.JSONException;  
  13. import org.json.JSONObject;  
  14. import io.netty.buffer.Unpooled;  
  15. import io.netty.channel.ChannelFuture;  
  16. import io.netty.channel.ChannelFutureListener;  
  17. import io.netty.channel.ChannelHandlerContext;  
  18. import io.netty.channel.ChannelInboundMessageHandlerAdapter;  
  19. import io.netty.handler.codec.http.DefaultHttpResponse;  
  20. import io.netty.handler.codec.http.HttpHeaders;  
  21. import io.netty.handler.codec.http.HttpRequest;  
  22. import io.netty.handler.codec.http.HttpResponse;  
  23. import io.netty.handler.codec.http.HttpResponseStatus;  
  24. import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;  
  25. 相關推薦

    基於html5 websocket API簡單實現斷點檔案

    本例項基於html5的 websocket API和netty框架,如果您對兩個技術不太熟悉,可以點選下面連線瞭解 準備: fiefox瀏覽器或chrome瀏覽器 在classpath中匯入netty類庫,json類庫 好拉

    Dubbox 鏈路追蹤(基於Brave+Zipkin的簡單實現

    很多時候,我們都能體會到分散式架構的話好處,其實一個系統不大,做分散式的成本是很高的,系統變得鬆耦合,這樣做的好處不言而喻,說說壞處吧,A系統遠端呼叫B系統,B系統又依賴C,D系統,當線上某個介面報錯,或者超時的時候,亦或者是業務問題的時候,定位一個問題是麻煩的,因為日記不

    Struts框架下載檔案輔助類,簡單實現Struts圖片以及下載

           首先在看這篇文章的前提下,你得會用Struts框架,有一定的基礎瞭解,說白了瞭解怎麼搭建就行了,然後基本就能順利執行本篇文章的Demo,當然這個類不僅僅侷限於圖片上傳下載的,因為是自己用流寫的方法所以可以支援其他檔案上傳下載。

    簡單實現斷點+MVP+Retrofit+RxJava

    依賴: compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.s

    JavaScript/Jsp 實現對資料庫的增刪改查和簡單的下載檔案

    完成目標:在頁面顯示資料庫的資訊,並且完成對資料庫的增刪改查,並且增加分頁功能。在頁面實現圖片或文字的下載。 1.增刪改查操作 User實體類程式碼: package com.jredu.web.entity; public class User { private

    ajax實現非同步檔案

     html+js程式碼 <form action="" enctype="multipart/form-data" id="upForm"> {{csrf_field()}} <input type="text" name="name" value="

    用java實現ftp檔案

    實際專案需求:從資料庫查詢資訊,在本地生成檔案,再上傳到ftp 所用Jar包:NetComponents.jar 核心程式碼: import java.io.File; import java.io.FileInputStream; import java.io.IOExcepti

    java實現FTP(檔案)、下載(檔案、資料夾、資料夾遞迴)、刪除(檔案、資料夾遞迴)

    提示:必須先保證有在FTP上上傳、下載、刪除的許可權! 本文結構 ---- 先給出測試樣例(圖片版),再給出工具類程式碼(文字版)! 上傳測試 注意:.uploadFile(String remoteDir, String remoteFileName, F

    Java實現FTP檔案到Linux伺服器的那些坑

    一、Linux安裝FTP服務         首先Linux伺服器需要安裝FTP服務,步驟如下: 1、執行如下ftp服務安裝命令: yum install vsftpd 可能會出現如下錯誤: "Couldn't open file /m

    一個簡單的WinHttp檔案的類

    這幾天用WinHttp做一個檔案上傳的東東,中間遇到大坑,雖然時候發現其實是小問題,但是既然耽擱了這麼久的時間,還是決定寫下來。 之前也在網上看到過很多網友的文章,說在使用WinHttp上傳檔案的時候總是出現各種問題,其實,只要按照格式來,就不會有這麼難。這裡首先要曉得在使

    ajax利用html5新特性帶進度條檔案

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <

    SpringMVC使用MultipartFile 實現非同步檔案

    目的是實現非同步上傳 1.新增pom依賴 新增pom依賴,因為用的ajax,資料需要轉成json的格式進行傳輸,所以還有加入一個JSON jar包: <dependenc

    SpringMVC_20_使用HttpMessageConverter T 實現檔案(不推薦)和下載檔案操作

    使用HttpMessageConverter< T>將請求資訊轉化並繫結到處理方法的入參中或將響應結果轉化為對應型別的響應資訊,Spring提供了兩種途徑: 使用@RequestBody/@ResponseBody對處理方法進行標註 使用Ht

    multipart/form-data圖片實現方法(檔案到Nimg)

            在實現multipart/form-data的圖片上傳時,需要用\r\n來分隔每一行,在JAVA中實現multipart/form-data的圖片上傳時則可以使用System.getProperty("line.separator")來進行每一行的分割。但是如果要將程式碼用於Android中

    利用第三方庫實現sftp檔案

    封裝一個類CSftpupload_sftp.h#define UPLOAD_SFTP_H#include <QString>#include <string>#include "libssh2_sftp.h"class CSftp{public:   

    不帶外掛 ,自己寫js,實現批量檔案及進度顯示

    今天接受專案中要完成檔案批量上傳檔案而且還要顯示上傳進度,一開始覺得這個應該不是很麻煩,當我在做的時候遇到了很多問題,很頭疼啊。 不過看了別人寫的程式碼,自己也測試過,發現網上好多都存在一些問題,並不是自己想要的。然後自己查閱各種資料,經過自己總結,最終完成了這個功能。

    python 實現自動檔案到百度網盤(附程式原始碼及實現過程)

    5、編寫AutoIt指令碼,實現檔案上傳 1.開啟scite script editor 程式碼如下: # coding=utf-8 import time from selenium import webdriver driver = webdriver.Firefox() #開啟火狐瀏覽器 dr

    阿里雲物件儲存OSS--實現隨時隨地檔案到阿里雲

    需求背景:消費者多批次回饋我司生產的車載智慧後視鏡出現宕機、連不上伺服器等問題,因產品已經出到全國各地不方便去取異常log,也不可能要求消費者把log傳給我們分析。 需求目標:公司內部實現遠端後臺上傳問題機型的log。 必備條件:後視鏡有SIM卡且能夠聯網(

    jquery.form實現ajax檔案同時設定headers

    function ajaxSubmitForm() {     var option = {    url : cache.batchImport,    type : 'POST',    dataType : '

    C#利用HttpListener實現接受檔案

    最近一個winform專案想直接用http協議與java端進行雙向通訊,發現HttpListener可以實現http通訊,如果提交form的enctype=application/x-www-form-urlencoded則可以通過HttpUtility.Par