最近在做的項目中客戶有監控軟件的需求。
需求:每5秒顯示被監控電腦的桌面情況。
實現思路:
1.截圖端:Timer每5秒截圖、調用服務端接口上傳。
2.服務端:保存截圖到服務端本地並把截圖信息保存到數據庫,包括圖片在服務端的保存路徑。
3.監控端:①調用服務端下載List<ScreenShot>接口,下載需要顯示的截圖列表。②Timer每5秒調用服務端下載最新ScreenShot對象,加入監控端list<ScreenShot>中。③要顯示某張截圖時根據本地List<ScreenShot>中信息調用服務端下載截圖接口把截圖下載到本地並顯示在pictureBox中,下載成功則把ScreenShot對象中isDownload屬性標為true,一遍下次直接調用,避免重復下。
一、截圖端
1.實現截屏功能。
//截屏
private void timerPrtSc_Tick(object sender, EventArgs e)
{
//定義一個矩形
Rectangle rect = new Rectangle();
//該矩形大小為當前屏幕大小
rect = System.Windows.Forms.Screen.GetBounds(this);
//定義Size類型,取得矩形的長和寬
Size mySize = new Size(rect.Width, rect.Height);
//定義一個位圖,長寬等於屏幕長寬
Bitmap bitmap = new Bitmap(rect.Width, rect.Height);
//定義Graphics為了在bitmap上繪制圖像
Graphics g = Graphics.FromImage(bitmap);
//把mySize大小的屏幕繪制到Graphisc上
g.CopyFromScreen(0, 0, 0, 0, mySize);
//創建文件路徑
string ImageName = DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".png";
string dir = @"./upload/";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string filePath = dir + ImageName;
//保存文件到本地
bitmap.Save(filePath);
//上傳文件到服務器
HttpPostData(R.URL, R.TimeOut, R.MultipartFile, filePath);
//釋放資源
bitmap.Dispose();
g.Dispose();
GC.Collect();
}
2.上傳文件到服務器(模擬表單提交把要傳送的文件轉換成流post到服務器中)
/// <summary>
/// 模擬表單Post數據(傳送文件以及參數)到Java服務端
/// </summary>
/// <param name="url">服務端url地址</param>
/// <param name="timeOut">響應時間</param>
/// <param name="fileKeyName">對應接口中 @RequestParam("file"),fileKeyName="file"</param>
/// <param name="filePath">要上傳文件在本地的全路徑</param>
/// <returns></returns>
///
private string HttpPostData(string url, int timeOut, string fileKeyName, string filePath)
{
//stringDict為要傳遞的參數的集合
NameValueCollection stringDict = new NameValueCollection();
stringDict.Add("ip", ip);
stringDict.Add("user", program.UserId);
stringDict.Add("programId", program.ProgramId);
string responseContent=string.Empty;
//創建其支持存儲區為內存的流
var memStream = new MemoryStream();
var webRequest = (HttpWebRequest)WebRequest.Create(url);
// 邊界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
// 邊界符
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
// 最後的結束符
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
// 設置屬性
webRequest.Method = "POST";
webRequest.Timeout = timeOut;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
// 寫入文件(以下字符串中name值對應接口中@RequestParam("file"),fileName為上傳文件在本地的全路徑)
const string filePartHeader =
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";
var header = string.Format(filePartHeader, fileKeyName, filePath);
var headerbytes = Encoding.UTF8.GetBytes(header);
memStream.Write(beginBoundary, 0, beginBoundary.Length);
memStream.Write(headerbytes, 0, headerbytes.Length);
var buffer = new byte[1024];
int bytesRead; // =0
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
// 寫入字符串的Key
var stringKeyHeader = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\"" +
"\r\n\r\n{1}\r\n";
foreach (byte[] formitembytes in from string key in stringDict.Keys
select string.Format(stringKeyHeader, key, stringDict[key])
into formitem
select Encoding.UTF8.GetBytes(formitem))
{
memStream.Write(formitembytes, 0, formitembytes.Length);
}
// 寫入最後的結束邊界符
memStream.Write(endBoundary, 0, endBoundary.Length);
webRequest.ContentLength = memStream.Length;
var requestStream = webRequest.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),
Encoding.GetEncoding("utf-8")))
{
responseContent = httpStreamReader.ReadToEnd();
}
fileStream.Close();
httpWebResponse.Close();
webRequest.Abort();
return responseContent;
}
二、Java服務端
1.服務端上傳接口
/**
* 上傳截屏
* @param ip
* @param filename
* @return
*/
@RequestMapping(value="upload",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> upload(
@RequestParam("ip") String ip,
@RequestParam("user") String user,
@RequestParam("programId") String programId,
@RequestParam("file") MultipartFile file
){
Map<String, String> result = new HashMap<String, String>();
logger.info("file name " + file.getOriginalFilename());
ScreenShot ss=screenShotService.upload(ip.trim(), user.trim(), programId.trim(), file);
result.put("ip", ss.getId());
result.put("user", ss.getUser());
result.put("channel", ss.getProgramId());
result.put("localpath", ss.getLocalPath());
return result;
}
/**
* 上傳截屏
*
* 邏輯:
* 1,把文件轉移到存儲目錄
* 2,生成screenshot對象保存起來
* @param ip
* @param filename
*/
public ScreenShot upload(String userIP,String userName,String programid, MultipartFile file)
{
try {
String ip=userIP;
String user=userName;
String programId=programid;
String localPath = receiveFile(file);
Long timeStamp=System.currentTimeMillis();
if (StringUtils.isNotBlank(localPath)){
//創建對象
ScreenShot ss = new ScreenShot(file.getOriginalFilename(),ip,user,programId,localPath,timeStamp);
//保存到數據庫
ss = screenShotRepository.save(ss);
return ss;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private String receiveFile(MultipartFile file) throws IOException {
String path;
//destPath為存儲文件的目錄地址,目錄下文件夾和文件取名由ileUtil.buildNewFileName()完成
String destPath = FileUtil.buildNewFileName(configService.getUploadRootPath() + Const._SPLASH, file.getOriginalFilename());
logger.info("upload file Path is " + file.getOriginalFilename()
+ " dest file name is " + destPath);
//新建一個名為dest的空文件
File dest = new File(destPath);
//把file放到dest的path
file.transferTo(dest);
path = dest.getPath();
return path;
}
2.服務端下載List<ScreenShot>接口
/**
* 返回節目開始至當前時間點的List<ScreenShot>
* @param timeStamp
* @param ip
* @return
*/
@RequestMapping (value = "searchList")
@ResponseBody
public Map<String,Object> searchList(
@RequestParam( value = "ip", defaultValue="" ) String ip,
@RequestParam( value = "startId", defaultValue="" ) String startId,
//@PathVariable("ip") String ip, @PathVariable路徑變量
//@PathVariable("startId") String startId @RequestParam請求參數
HttpServletRequest request)
{
List<ScreenShot> ss = this.screenShotService.findListByIpandID(ip,startId);
Map<String,Object> map = new HashMap<String,Object>();
map.put("rows", ss);
return map;
}
3.服務端下載ScreenShot對象接口
/**
* 返回當前時間點的ScreenShot
* @param ip
* @return
*/
@RequestMapping (value = "searchCurrent")
@ResponseBody
public ScreenShot searchCurrent(
@RequestParam( value = "ip", defaultValue="" ) String ip,
@RequestParam( value = "currentId", defaultValue="" ) String currentId)
{
ScreenShot ss = this.screenShotService.findOneById(ip,currentId);
return ss;
}
4.服務端根據id下載文件接口
/**
* 下載id對應的截屏
*/
@RequestMapping (value = "download/{id}")
@ResponseBody
public void download(
@PathVariable("id") String id,
HttpServletRequest request,
HttpServletResponse response)
{
try {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding( Const._UTF8 );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
ScreenShot ss = this.screenShotService.findById(id);
String localPath =ss.getLocalPath();
response.setContentType("application/octet-stream");
String downloadName = ss.getFileName();
String extention = "png";
response.setHeader("extension",extention);
response.setHeader("downloadName",downloadName );
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE")) { // IE內核浏覽器
downloadName = new String(downloadName.getBytes(), "ISO8859-1");
downloadName = URLEncoder.encode(downloadName, Const._UTF8 );
downloadName = new String(downloadName.getBytes( Const._UTF8 ), "ISO8859-1");
downloadName = downloadName.replace("+", "%20");// 處理IE文件名中有空格會變成+"的問題;
} else {// 非IE
downloadName = URLDecoder.decode(downloadName, Const._UTF8);
downloadName = "=?UTF-8?B?"
+ (new String(Base64.encodeBase64(downloadName.getBytes( Const._UTF8 ))))
+ "?=";
}
response.setHeader("Content-disposition", "attachment; filename=" + downloadName);
bis = new BufferedInputStream(new FileInputStream( localPath ));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
三、監控端
1.調用服務端下載接口下載List<ScreenShot>
//保存服務器下載的ScreenShot列表
private List<ScreenShot> listScreenShot = new List<ScreenShot>();
/// <summary>
/// 下載節目開始至當前時間點的List<ScreenShot>
/// </summary>
/// <param name="ip">njmonitor客戶端PC機的ip</param>
/// <param name="startId">當前節目開始的id號</param>
/// <returns></returns>
public List<ScreenShot> DownloadList(string ip, string startId)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
List<ScreenShot> ss = new List<ScreenShot>();
string url = "monit/searchList?ip={ip}&startId={startId}";
parameters.Add("ip", ip);
parameters.Add("startId", startId);
RestResponse response = HttpClient.RequestResponse("radionav", url, "get", parameters);
OpResult result = null;
if (!response.StatusCode.Equals(System.Net.HttpStatusCode.OK))
{
result = new OpResult();
result.Ok = false;
result.Reason = response.ErrorMessage;
}
else
{
string content = response.Content;
try
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(content);
object[] obj = (object[])json["rows"];
//遍歷數組
for (int i = 0; i < obj.Length; i++)
{
//object對象內又為dictionary,轉為DICTIONARY對象
Dictionary<string, object> jsonScreenShot = (Dictionary<string, object>)obj[i];
//傳入screenshot類
ScreenShot screenShot = new ScreenShot(jsonScreenShot);
ss.Add(screenShot);
}
}
catch (Exception e)
{
result = new OpResult(false, null);
}
}
return ss;
}
2.調用服務端下載接口下載截屏
public static string GetUrlDownContent(string url, string path)
{
string localPath = string.Empty;
try
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
string downloadName = myrp.Headers["downloadName"];
localPath = path + downloadName;
System.IO.Stream so = new System.IO.FileStream(localPath, System.IO.FileMode.Create);
System.IO.Stream st = myrp.GetResponseStream();
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
myrp.Close();
Myrq.Abort();
return localPath;
}
catch (System.Exception e)
{
return null;
}
}