jsp 代码(由于直接使用html元素上传会出问题,此处使用工具转化成图片):
java 服务端代码如下,使用net.sf.json相关的jar包和maven下的Java-WebSocket-1.3.1-SNAPSHOT.jar,低版本jdk可能不支持,此处使用1.8版本jdk环境
package controller;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import jdk.nashorn.internal.parser.JSONParser;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
public class ChatServer extends WebSocketServer {
private String fileName = null;
private String fileState = null;
// 下载的文件最大为缓冲区100M
private static final Integer capacity = 100 * 1024 * 1024;
// 下载文件的二进制缓冲区
private static ByteBuffer totalByteBufferFile = ByteBuffer.allocate(capacity);;
public ChatServer(int port) throws UnknownHostException {
super(new InetSocketAddress(InetAddress.getByName("localhost"), port));
}
public ChatServer(InetSocketAddress address) {
super(address);
}
public ChatServer(String address, int port) throws UnknownHostException {
super(new InetSocketAddress(InetAddress.getByName(address), port));
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
try {
this.sendToAll("CONNECTED");
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(conn + " entered the room!");
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
try {
this.sendToAll("CLOSED");
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(conn + " has left the room!");
}
@Override
public void onMessage(WebSocket conn, String message) {
conn.getRemoteSocketAddress().getAddress().getAddress();
JSONObject jsonObject = JSONObject.fromObject(message);
Map map = (Map) JSONObject.toBean(jsonObject, Map.class);
fileName = (String) map.get("fileName");
fileState = (String) map.get("fileState");
Integer sum = (Integer) map.get("sum");
Integer index = (Integer) map.get("index");
Integer startSize = (Integer) map.get("startSize");
Integer endSize = (Integer) map.get("endSize");
if (fileState.equalsIgnoreCase("sendStart")) {
totalByteBufferFile.clear();
conn.send("sendStart_CallBack");
System.out.println("sendStart_CallBack");
}
if (fileState.equalsIgnoreCase("sendEnd")) {
totalByteBufferFile.flip();
System.out.println("totalByteBufferFile____position:" + totalByteBufferFile.position() + "____limit:"
+ totalByteBufferFile.limit() + "______startSize:" + startSize + "______endSize:" + endSize);
byte[] dst = new byte[totalByteBufferFile.limit()];
totalByteBufferFile.get(dst, 0, totalByteBufferFile.limit());
saveFileFromBytes(dst, "D:\\" + fileName);
totalByteBufferFile.clear();
}
}
// @Override
// public void onMessage(WebSocket conn, ByteBuffer message) {
// byte[] bs = message.array();
// saveFileFromBytes(bs, "src/" + fileName);
// System.out.println("收到2进制流");
//
// }
@Override
public void onMessage(WebSocket conn, ByteBuffer message) {
// TODO Auto-generated method stub
System.out.println("收到2进制流____position:" + message.position() + "____limit:" + message.limit());
totalByteBufferFile.put(message);
conn.send("OK");
}
public static void main(String[] args) throws InterruptedException, IOException {
int port = 8887;
ChatServer s = new ChatServer("localhost", port);
s.start();
System.out.println("ChatServer started on port: " + s.getPort());
}
@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
}
public void sendToAll(String text) throws InterruptedException {
for (WebSocket c : connections()) {
c.send(text);
}
}
public static boolean saveFileFromBytes(byte[] b, String outputFile) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return true;
}
}