2017-09-20 185 views
0

對不起,如果這已經發布,但似乎有這麼多的答案,所以我想我會問在這裏得到一些澄清。即時通訊使用下面的截圖webdriver的代碼來寫的截圖到本地驅動器,偉大工程:Selenium - 寫入網絡驅動器。可以寫一個文本文件但不是圖像文件

final Screenshot screenshot = new AShot().shootingStrategy(
new ViewportPastingStrategy(500)).takeScreenshot(driver); 
final BufferedImage image = screenshot.getImage(); 

File outputfile = new File("//Users/me/Desktop/testfolder/saved.png"); 
ImageIO.write(image, "PNG", outputfile); 

該文件完全寫入到本地驅動器。但是,我想寫入我有權訪問的網絡驅動器,並且已經測試了此訪問權限,因爲我可以編寫文本文件。

驅動器看起來像這樣(我的名字和密碼):SMB:// globalnerds; Carl.Lewis:[email protected]/bs-test/

我可以寫那裏有一個文本文件,但是當我嘗試寫入圖像時,它不起作用。

任何人都可以幫我解決這個問題嗎?

謝謝!

+0

你可能不應該發表您真實的用戶名/密碼,如果你這樣做,改變它。 :-)除此之外,您是否從Java程序編寫了一個文本文件?你確定你的Java程序和你的用戶擁有相同的訪問權限嗎? – haraldK

+1

謝謝haraldK。這不是真正的用戶名或密碼。這只是語法。 :)。我從Java程序YES編寫了一個文本文件。如果我用.png重命名該文件,它也會寫入,但將是一個空文件。 –

回答

0

所以我想出瞭如何做到這一點,並使用下面的代碼像魅力一樣工作。我抓取一張圖片並將其寫入帶有憑證的共享目錄。我通過我的憑據和路徑在一條線:

公共無效WriteMe()拋出異常{

InputStream in = null; 
    OutputStream out = null; 
    try { 
     //Get a picture 
     File localFile = new File ("//Users/BIG.BEAR/Desktop/testfolder/saved.png"); 
     String remotePhotoUrl = "smb://globalnerds;BIG.BEAR:[email protected]/testfolder/"; //The shared directory to store pictures 
     SimpleDateFormat fmt = new SimpleDateFormat ("yyyyMMddHHmmssSSS_"); 
     SmbFile remoteFile = new SmbFile (remotePhotoUrl + "/" + fmt.format (new Date()) + localFile.getName()); 
     remoteFile.connect(); //Try to connect 

     in = new BufferedInputStream (new FileInputStream (localFile)); 
     out = new BufferedOutputStream (new SmbFileOutputStream (remoteFile)); 

     byte[] buffer = new byte[4096]; 
     int len = 0; //Read length 
     while ((len = in.read (buffer, 0, buffer.length)) != -1) { 
      out.write (buffer, 0, len); 
     } 
     out.flush(); //The refresh buffer output stream 
    } catch (Exception e) { 
     String msg = "The error occurred: " + e.getLocalizedMessage(); 
     System.out.println (msg); 
    } finally { 
     try { 
      if (out != null) { 
       out.close(); 
      } 
      if (in != null) { 
       in.close(); 
      } 
     } catch (Exception e) { 
     } 
    } 


} 
相關問題