我需要将一个文本参数传递给使用Apache Commons Exec启动的命令的stdin(对于好奇,命令是gpg,参数是密钥库的密码; gpg没有明确提供密码的参数,仅从stdin接受它.
另外,我需要它来支持Linux和Windows.
在shell脚本中,我会这样做
cat mypassphrase|gpg --passphrase-fd
Run Code Online (Sandbox Code Playgroud)
要么
type mypassphrase|gpg --passphrase-fd
Run Code Online (Sandbox Code Playgroud)
但是类型在Windows上不起作用,因为它不是可执行文件,而是命令内置的命令(cmd.exe).
该代码不工作(由于上述原因)低于.为此产生一个完整的外壳太难看了,我一直在寻找更优雅的解决方案.不幸的是,BouncyCastle库和PGP之间存在一些不兼容的问题,因此我无法在(非常短的)时间内使用完全编程的解决方案.
提前致谢.
CommandLine cmdLine = new CommandLine("type");
cmdLine.addArgument(passphrase);
cmdLine.addArgument("|");
cmdLine.addArgument("gpg");
cmdLine.addArgument("--passphrase-fd");
cmdLine.addArgument("0");
cmdLine.addArgument("--no-default-keyring");
cmdLine.addArgument("--keyring");
cmdLine.addArgument("${publicRingPath}");
cmdLine.addArgument("--secret-keyring");
cmdLine.addArgument("${secretRingPath}");
cmdLine.addArgument("--sign");
cmdLine.addArgument("--encrypt");
cmdLine.addArgument("-r");
cmdLine.addArgument("recipientName");
cmdLine.setSubstitutionMap(map);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);
Run Code Online (Sandbox Code Playgroud) 我何时应该使用Apache Commons的Validate.isTrue,何时应该使用'assert'关键字?
我使用Apache Commons FTPClient上传大文件,但传输速度只是使用WinSCP通过FTP传输速度的一小部分.我怎样才能加快转移?
public boolean upload(String host, String user, String password, String directory,
String sourcePath, String filename) throws IOException{
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(host);
client.login(user, password);
client.setControlKeepAliveTimeout(500);
logger.info("Uploading " + sourcePath);
fis = new FileInputStream(sourcePath);
//
// Store file to server
//
client.changeWorkingDirectory(directory);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.storeFile(filename, fis);
client.logout();
return true;
} catch (IOException e) {
logger.error( "Error uploading " + filename, e );
throw e;
} finally {
try {
if (fis != null) …Run Code Online (Sandbox Code Playgroud) 我正在使用commons压缩库来创建目录的tar.gz.我有一个目录结构如下.
parent/
child/
file1.raw
fileN.raw
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码进行压缩.它运行良好,没有例外.但是,当我尝试解压缩tar.gz时,我得到一个名为"childDirToCompress"的文件.它的大小正确,因此文件在tarring过程中显然已经相互附加.所需的输出将是一个目录.我无法弄清楚我做错了什么.任何有智慧的公共审判者都可以让我走上正确的道路吗?
CreateTarGZ() throws CompressorException, FileNotFoundException, ArchiveException, IOException {
File f = new File("parent");
File f2 = new File("parent/childDirToCompress");
File outFile = new File(f2.getAbsolutePath() + ".tar.gz");
if(!outFile.exists()){
outFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(outFile);
TarArchiveOutputStream taos = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream(fos)));
taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
addFilesToCompression(taos, f2, ".");
taos.close();
}
private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir) throws IOException{
taos.putArchiveEntry(new TarArchiveEntry(file, dir));
if (file.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(bis, taos);
taos.closeArchiveEntry();
bis.close();
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Apache Commons解决优化问题.我在这里为Commons Math 2 找到了一个"Hello World"示例.但是,我想使用Commons Math 3.2,我找不到任何关于如何使用这部分代码的示例:
PointValuePair solution = null;
SimplexSolver solver = new SimplexSolver();
solution = solver.optimize(optData);
Run Code Online (Sandbox Code Playgroud)
具体来说,我不知道什么是optData以及我把约束放在哪里.如果有人给我一个关于如何使用org.apache.commons.math3.optim库的"Hello World"示例,我将不胜感激.
最好的祝愿!
我有一个List<Object>,我想返回它找到的第一个与谓词匹配的值.
我发现我可以使用CollectionUtils.find(collection,predicate)(Apache commons).假设它Object包含一个名为:的整数变量value,我如何在谓词中指定该值可以1,2,3,4,5并丢弃那些不匹配的变量.有可能做'包含'.
也没有使用java 8所以无法做流.
SimpleHttpConnectionManager使用不正确.确保始终调用HttpMethod.releaseConnection(),并且一次只有一个线程和/或方法使用此连接管理器.
有谁知道为什么会出现此错误并导致我要下载的文件或失败并重试或下载未完成
谢谢 !
commons-lang 3.0仍然是测试版,但它可以在一些maven存储库中找到(我不能)
以下是创建文本文档并将其上传到我的FTP服务器的代码.由于某种原因,它似乎不起作用.我习惯了提供的图书馆
http://lavalatwork.blogspot.tw/2010/09/using-apache-commons-ftp-library-in.html
用于与FTP服务器通信.
try
{
final String testString = new String("Hello");
FileOutputStream fOut = openFileOutput("samplefile.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(testString);
osw.flush();
osw.close();
}
catch(IOException ex)
{
}
FTPClient mFTP = new FTPClient();
try {
// Connect to FTP Server
mFTP.connect("192.168.10.101");
//mFTP.login("user", "password");
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalPassiveMode();
// Prepare file to be uploaded to FTP Server
File file = new File(getFileStreamPath("samplefile.txt")+ "");
FileInputStream ifile = new FileInputStream(file);
// Upload file to FTP Server
mFTP.storeFile("filetotranfer",ifile);
mFTP.disconnect();
} catch (SocketException e) {
// …Run Code Online (Sandbox Code Playgroud) 我正在使用Jedis客户端连接到我的Redis服务器.以下是我用于连接Jedis的设置(使用apache公共池):
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setMaxIdle(400);
// Tests whether connections are dead during idle periods
poolConfig.setTestWhileIdle(true);
poolConfig.setMaxTotal(400);
// configuring it for some good max value so that timeout don't occur
poolConfig.setMaxWaitMillis(120000);
Run Code Online (Sandbox Code Playgroud)
到目前为止,通过这些设置,我在可靠性方面没有遇到任何问题(我可以随时获得Jedis连接),但我看到Jedis性能存在一定的延迟.
任何人都可以建议我进一步优化以实现高性能吗?
apache-commons ×10
java ×9
ftp ×2
android ×1
assert ×1
collections ×1
compression ×1
http ×1
jedis ×1
math ×1
maven-2 ×1
optimization ×1
redis ×1
simplex ×1
tar ×1
validation ×1