最近在做的項目中客戶有監控軟件的需求。
需求:每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,
新聞熱點
疑難解答