处理客户端无法获取错误截图问题
This commit is contained in:
parent
dfd97096a6
commit
2a6e773b49
|
@ -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;
|
||||
|
@ -51,6 +47,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 {
|
||||
|
@ -77,7 +82,7 @@ public class ClientHandler extends ChannelHandlerAdapter {
|
|||
//返回请求
|
||||
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();
|
||||
|
@ -121,7 +126,7 @@ 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());
|
||||
|
@ -130,13 +135,72 @@ public class ClientHandler extends ChannelHandlerAdapter {
|
|||
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());
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue