!13 处理客户端并发注册问题

Merge pull request !13 from Flever_fj/Develop_Branch
This commit is contained in:
seagull 2020-03-02 13:43:41 +08:00 committed by Gitee
commit fb066e052f
4 changed files with 149 additions and 18 deletions

View File

@ -1,20 +1,16 @@
package luckyclient.netty;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
@ -23,6 +19,7 @@ import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import luckyclient.utils.config.SysConfig;
public class ClientHandler extends ChannelHandlerAdapter {
//从application.properties文件中获取用到的参数;
@ -51,6 +48,15 @@ public class ClientHandler extends ChannelHandlerAdapter {
private static final String SERVER_PORT= SysConfig.getConfiguration().getProperty("server.web.port");
private int byteRead;
private volatile int start = 0;
private volatile int lastLength = 0;
public RandomAccessFile randomAccessFile;
private static ChannelHandlerContext ctx;
public ClientHandler() throws IOException {
@ -67,17 +73,17 @@ public class ClientHandler extends ChannelHandlerAdapter {
}catch (Exception e)
{
log.error("收到服务端非Json消息"+jsonStr);
System.out.println("收到服务端非Json消息"+jsonStr);
return;
}
log.info("收到服务端消息:"+json.toString());
System.out.println("收到服务端消息:"+json.toString());
//解析消息
if("run".equals(json.get("method"))){
try {
//返回请求
JSONObject re=new JSONObject();
re.put("method","return");
Result result=new Result(1,"同步等待消息返回",json.get("uuid").toString());
Result result=new Result(1,"同步等待消息返回",json.get("uuid").toString(),null);
//如果是调度请求则发起一个HTTP请求
//获取是get方法还是post方法
String getOrPost=json.get("getOrPost").toString();
@ -92,7 +98,7 @@ public class ClientHandler extends ChannelHandlerAdapter {
try {
tmpResult=HttpRequest.httpClientGet(urlParam,jsonparams,socketTimeout);
} catch (Exception e) {
log.error("转发服务端GET请求出错");
System.out.println("转发服务端GET请求出错");
}
}
else
@ -102,7 +108,7 @@ public class ClientHandler extends ChannelHandlerAdapter {
try {
tmpResult=HttpRequest.httpClientPost(urlParam,jsonparams,socketTimeout);
} catch (Exception e) {
log.error("转发服务端POST请求出错");
System.out.println("转发服务端POST请求出错");
}
}
result.setMessage(tmpResult);
@ -121,22 +127,81 @@ public class ClientHandler extends ChannelHandlerAdapter {
HttpRequest.downLoadFromUrl("http://"+SERVER_IP+":"+SERVER_PORT+"/common/download?fileName="+fileName,fileName,path);
//返回请求
JSONObject re=new JSONObject();
Result result=new Result(1,"上传成功",json.get("uuid").toString());
Result result=new Result(1,"上传成功",json.get("uuid").toString(),null);
re.put("method","return");
re.put("data",result);
sendMessage(re.toString());
log.info("下载驱动包成功,路径为:"+path+";文件名为:"+fileName);
System.out.println("下载驱动包成功,路径为:"+path+";文件名为:"+fileName);
} catch (Exception e) {
e.printStackTrace();
//返回请求
JSONObject re=new JSONObject();
Result result=new Result(0,"上传失败",json.get("uuid").toString());
Result result=new Result(0,"上传失败",json.get("uuid").toString(),null);
re.put("method","return");
re.put("data",result);
sendMessage(re.toString());
log.info("下载驱动包失败,路径为:"+path+";文件名为:"+fileName);
System.out.println("下载驱动包失败,路径为:"+path+";文件名为:"+fileName);
}
}
else if("upload".equals(json.get("method")))
{
//上传截图到服务器
Map<String,Object> jsonparams = (Map<String,Object>)json.get("data");
String fileName=jsonparams.get("imgName").toString();
String ctxPath = System.getProperty("user.dir")+File.separator+"log"+File.separator+"ScreenShot"+File.separator+fileName;
File file=new File(ctxPath);
int start=Integer.valueOf(json.get("start").toString());
FileUploadFile fileUploadFile=new FileUploadFile();
fileUploadFile.setFile(file);
if (start != -1) {
try {
if(start==0)
lastLength = 1024 * 10;
randomAccessFile = new RandomAccessFile(fileUploadFile.getFile(), "r");
randomAccessFile.seek(start); //将文件定位到start
System.out.println("长度:" + (randomAccessFile.length() - start));
int a = (int) (randomAccessFile.length() - start);
int b = (int) (randomAccessFile.length() / 1024 * 2);
if (a < lastLength) {
lastLength = a;
}
System.out.println("文件长度:" + (randomAccessFile.length()) + ",start:" + start + ",a:" + a + ",b:" + b + ",lastLength:" + lastLength);
byte[] bytes = new byte[lastLength];
System.out.println("bytes的长度是="+bytes.length);
if ((byteRead = randomAccessFile.read(bytes)) != -1 && (randomAccessFile.length() - start) > 0) {
System.out.println("byteRead = " + byteRead);
fileUploadFile.setEndPos(byteRead);
fileUploadFile.setBytes(bytes);
//返回请求
JSONObject re=new JSONObject();
Result result=new Result(1,"上传成功",json.get("uuid").toString(),fileUploadFile);
re.put("method","upload");
re.put("data",result);
re.put("uuid",json.get("uuid").toString());
sendMessage(re.toString());
try {
ctx.writeAndFlush(fileUploadFile);
} catch (Exception e) {
e.printStackTrace();
}
} else {
randomAccessFile.close();
System.out.println("文件已经读完channelRead()--------" + byteRead);
//返回请求
JSONObject re=new JSONObject();
Result result=new Result(1,"上传成功",json.get("uuid").toString(),null);
re.put("method","return");
re.put("data",result);
sendMessage(re.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@ -149,7 +214,6 @@ public class ClientHandler extends ChannelHandlerAdapter {
json.put("method","clientUp");
ClientHandler.ctx=ctx;
sendMessage(json.toString());
System.out.println("通道已连接,客户端登录消息已发送");
}
@Override

View File

@ -0,0 +1,58 @@
package luckyclient.netty;
import java.io.File;
import java.io.Serializable;
public class FileUploadFile implements Serializable {
private static final long serialVersionUID = 1L;
private File file;// 文件
private String file_md5;// 文件名
private int starPos;// 开始位置
private byte[] bytes;// 文件字节数组
private int endPos;// 结尾位置
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFile_md5() {
return file_md5;
}
public void setFile_md5(String file_md5) {
this.file_md5 = file_md5;
}
public int getStarPos() {
return starPos;
}
public void setStarPos(int starPos) {
this.starPos = starPos;
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
public int getEndPos() {
return endPos;
}
public void setEndPos(int endPos) {
this.endPos = endPos;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}

View File

@ -10,11 +10,20 @@ public class Result implements Serializable {
private int code;
private Object message;
private String uniId;
public Result(int code, Object message, String uniId) {
private FileUploadFile fileUploadFile;
public Result(int code, Object message, String uniId,FileUploadFile fileUploadFile) {
this.code = code;
this.message = message;
this.uniId = uniId;
this.fileUploadFile=fileUploadFile;
}
public FileUploadFile getFileUploadFile() {
return fileUploadFile;
}
public void setFileUploadFile(FileUploadFile fileUploadFile) {
this.fileUploadFile = fileUploadFile;
}
public int getCode() {

View File

@ -17,7 +17,7 @@ public class SysConfig {
static{
try {
InputStream in = new BufferedInputStream(SysConfig.class.getResourceAsStream(SYS_CONFIG_FILE));
SYS_CONFIG.load(new InputStreamReader(in, "UTF-8"));
SYS_CONFIG.load(new InputStreamReader(in, "GBK"));
} catch (IOException e) {
e.printStackTrace();
}