基於html5 websocket API簡單實現斷點上傳檔案
本例項基於html5的 websocket API和netty框架,如果您對兩個技術不太熟悉,可以點選下面連線瞭解
準備:
fiefox瀏覽器或chrome瀏覽器
在classpath中匯入netty類庫,json類庫
好拉,一切準備就緒...
伺服器端:
WebSocketServerInitializer.java
- /*
- * Copyright 2012 The Netty Project
- *
-
* The Netty Project licenses this file to you under the Apache License,
- * version 2.0 (the "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- */
- package com.wudasong.breakPoinUploadServer;
- import io.netty.bootstrap.ServerBootstrap;
-
import io.netty.channel.Channel;
- import io.netty.channel.socket.nio.NioEventLoop;
- import io.netty.channel.socket.nio.NioServerSocketChannel;
- /**
- * A HTTP server which serves Web Socket requests at:
- *
- * http://localhost:8080/websocket
- *
- * Open your browser at http://localhost:8080/, then the demo page will be loaded and a Web Socket connection will be
- * made automatically.
- *
- * This server illustrates support for the different web socket specification versions and will work with:
- *
- * <ul>
- * <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00)
- * <li>Chrome 6-13 (draft-ietf-hybi-thewebsocketprotocol-00)
- * <li>Chrome 14+ (draft-ietf-hybi-thewebsocketprotocol-10)
- * <li>Chrome 16+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
- * <li>Firefox 7+ (draft-ietf-hybi-thewebsocketprotocol-10)
- * <li>Firefox 11+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
- * </ul>
- */
- publicclass WebSocketServer {
- privatefinalint port;
- public WebSocketServer(int port) {
- this.port = port;
- }
- publicvoid run() throws Exception {
- ServerBootstrap b = new ServerBootstrap();
- try {
- b.eventLoop(new NioEventLoop(), new NioEventLoop())
- .channel(new NioServerSocketChannel())
- .localAddress(port)
- .childHandler(new WebSocketServerInitializer());
- Channel ch = b.bind().sync().channel();
- System.out.println("Web socket server started at port " + port + '.');
- System.out.println("Open your browser and navigate to http://localhost:" + port + '/');
- ch.closeFuture().sync();
- } finally {
- b.shutdown();
- }
- }
- publicstaticvoid main(String[] args) throws Exception {
- int port;
- if (args.length > 0) {
- port = Integer.parseInt(args[0]);
- } else {
- port = 8082;
- }
- new WebSocketServer(port).run();
- }
- }
WebSocketServerHandler.java
- package com.wudasong.breakPoinUploadServer;
- importstatic io.netty.handler.codec.http.HttpHeaders.*;
- importstatic io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
- importstatic io.netty.handler.codec.http.HttpMethod.*;
- importstatic io.netty.handler.codec.http.HttpResponseStatus.*;
- importstatic io.netty.handler.codec.http.HttpVersion.*;
- import java.io.File;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
- import org.json.JSONException;
- import org.json.JSONObject;
- import io.netty.buffer.Unpooled;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelFutureListener;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.ChannelInboundMessageHandlerAdapter;
- import io.netty.handler.codec.http.DefaultHttpResponse;
- import io.netty.handler.codec.http.HttpHeaders;
- import io.netty.handler.codec.http.HttpRequest;
- import io.netty.handler.codec.http.HttpResponse;
- import io.netty.handler.codec.http.HttpResponseStatus;
- import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
-
相關推薦
基於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