【发布时间】:2015-06-17 05:47:42
【问题描述】:
我正在使用PhantomJS 对网站进行无头测试。由于exe 将捆绑在jar 文件中,因此我决定读取它并将其写入临时文件,以便我可以通过绝对路径正常访问它。
以下是将 InputStream 转换为 String 的代码,引用新的临时文件:
public String getFilePath(InputStream inputStream, String fileName)
throws IOException
{
String fileContents = readFileToString(inputStream);
File file = createTemporaryFile(fileName);
String filePath = file.getAbsolutePath();
writeStringToFile(fileContents, filePath);
return file.getAbsolutePath();
}
private void writeStringToFile(String text, String filePath)
throws FileNotFoundException
{
PrintWriter fileWriter = new PrintWriter(filePath);
fileWriter.print(text);
fileWriter.close();
}
private File createTemporaryFile(String fileName)
{
String tempoaryFileDirectory = System.getProperty("java.io.tmpdir");
File temporaryFile = new File(tempoaryFileDirectory + File.separator
+ fileName);
return temporaryFile;
}
private String readFileToString(InputStream inputStream)
throws UnsupportedEncodingException, IOException
{
StringBuilder inputStringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream, "UTF-8"));
String line;
while ((line = bufferedReader.readLine()) != null)
{
inputStringBuilder.append(line);
inputStringBuilder.append(System.lineSeparator());
}
String fileContents = inputStringBuilder.toString();
return fileContents;
}
这可行,但是当我尝试启动 PhantomJS 时,它会给我一个 ExecuteException:
SERVERE: org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "C:\Users\%USERPROFILE%\AppData\Local\Temp\phantomjs.exe" (in directory "."): CreateProcess error=216, the version of %1 is not compatible with this Windows version. Check the system information of your computer and talk to the distributor of this software)
如果我不尝试从jar 中读取PhantomJS,因此使用相对路径可以正常工作。问题是我如何从 jar 文件中读取和执行PhantomJS,或者至少获得读取和写入新(临时)文件的解决方法。
【问题讨论】:
-
jar文件中为什么会出现PhantomJS exe?
-
@ArtjomB。让用户无需关心 webdriver 可执行文件
-
您正在将 phantomjs.exe 读入字符串?
标签: java phantomjs windows-8.1