2017-05-26 24 views
0

我正在嘗試複製並重命名本地文件機器(Win 7)在JMeter 3.0(Java v1.8)中使用Bean Shell Sampler。這個想法是創建一個具有唯一名稱的新文件,並將該名稱保存爲一個變量,可用於代替FTP PUT請求中的文件名。JMeter Bean Shell Sampler錯誤「...當複製文件時,在類'java.nio.file.Paths」中找不到靜態方法get(java.lang.String)

這裏是我使用複製和重命名代碼:

import java.text.*; 
import java.nio.file.StandardCopyOption.*; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 

String filename = new SimpleDateFormat("dd-MM-yyyy_hh:mm:ss").format(new Date())+".xlsx"; 
log.info(filename); 

Path source = Paths.get("C:/dropfile/qatp/QATP_GuestRecords.xlsx"); 
Path target = Paths.get("C:/dropfile/qatp/"+filename); 

Files.copy(source, target, REPLACE_EXISTING); 

錯誤我收到在日誌中:

ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.text.; import java.nio.file.StandardCopyOption.; import java.io.IO . . . '' : Typed variable declaration : Error in method invocation: Static method get( java.lang.String) not found in class'java.nio.file.Paths'

我一直在尋找的答案,這個問題並遇到了一個solution where the suggestion was: 「我的猜測是,它的問題是它沒有填充可變參數。試試:

Path target = Paths.get(filename, new String[0]);" 

我試圖通過修改我的代碼如下所示此解決方案:

import java.text.*; 
import java.nio.file.StandardCopyOption.*; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 

String filename = new SimpleDateFormat("dd-MM-yyyy_hh:mm:ss").format(new Date())+".xlsx"; 
log.info(filename); 

Path source = Paths.get("C:/dropfile/qatp/QATP_GuestRecords.xlsx", new String[0]); 
Path target = Paths.get("C:/dropfile/qatp/"+filename, new String[0]); 

Files.copy(source, target, REPLACE_EXISTING); 

而且收到此錯誤:

ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.text.; import java.nio.file.StandardCopyOption.; import java.io.IO . . . '' : Typed variable declaration : Method Invocation Paths.get

有誰知道爲什麼我打這個錯誤,如何解決呢?

回答

2

即使在普通的舊Java中,這也是一種誤導性的使用方法,它使用一個URI或一個字符串數組(可變參數)作爲Paths.getSee javadoc

在Java中,您嘗試的工作原理是靜態類型允許編譯器確定您傳遞的是單個字符串的數組。顯然,BeanShell不會讓人困惑。在我看來,其他答案中提出的技巧並不是一個好主意:再次在Java中,通過加入兩個字符串(第二個字符串爲空,所以結果是第一個字符串,這就是您想要的),它會起作用,但它會引起混淆BeanShell都是一樣的,因爲有另一個靜態的get方法需要2個參數。

如果你已經有路徑作爲一個字符串,試試這個:

Path source = new File("C:/dropfile/qatp/QATP_GuestRecords.xlsx").toPath(); 

或者,你可以使用Paths.get這樣的:

Path source = Paths.get("C:", "dropfile", "qatp", "QATP_GuestRecords.xlsx"); 

或像這樣(可變參數是syntaxic糖以幫助通過一個陣列):

Path source = Paths.get(new String [] { "C:/dropfile/qatp/QATP_GuestRecords.xlsx" }); 

它是完全有效的通過fragme作爲參數的路徑的nts,或者整個路徑字符串作爲單個參數,但似乎會使BeanShell脫機,因此,最好避免在BeanShell中使用Paths.get,除非像上例中那樣顯式傳遞數組。

0

BeanShell的!= Java的,它不支持所有Java功能(想想作爲關於Java 1.5,並適當地修改您的密碼。

因此,我建議切換到JSR223 SamplerGroovy語言的,Groovy更符合Java並且性能更好。

另外要注意,你可以使用FileUtils.copyFile() method這將爲雙方的BeanShell和/或Groovy

import org.apache.commons.io.FileUtils; 
import java.text.SimpleDateFormat; 

String filename = new SimpleDateFormat("dd-MM-yyyy_hh:mm:ss").format(new Date()) + ".xlsx"; 
FileUtils.copyFile(new File("/tmp/QATP_GuestRecords.xlsx"), new File("/tmp/" + filename)); 

Groovy is the New Black文章工作的詳細信息,使用JMeter測試腳本的Groovy語言。

相關問題