Tag: apache commons

在GenericObjectPool中创建对象

我正在通过将Cipher放入池中来研究GenericObjectPool ,以便可以重用它。 GenericObjectPool pool; CipherFactory factory = new CipherFactory(); this.pool = new GenericObjectPool(factory); pool.setMaxTotal(10); pool.setBlockWhenExhausted(true); pool.setMaxWaitMillis(30 * 1000); CipherFactory public class CipherFactory extends BasePooledObjectFactory { private boolean running = false; @Override public Cipher create() throws Exception { return Cipher.getInstance(“DESede/CBC/NoPadding”); } @Override public PooledObject wrap(Cipher arg0) { return new DefaultPooledObject(arg0); } @Override public boolean validateObject(PooledObject p) […]

Apache Commons FTPClient.listFiles

我在我的一个应用程序中使用org.apache.commons.net.ftp.FTPClient来使用FTP服务器。 我能够connect , login , pwd和pwd 。 但是,当我尝试list文件时,它不会返回该目录中的文件列表,我知道确实存在文件。 我使用FTPFile[] listFiles() ,它返回一个FTPFile的空数组。 请在下面找到我尝试此操作的代码段: String hostname = properties.getProperty(“FTP_SERVER”); String user = properties.getProperty(“FTP_USER”); String passwd = properties.getProperty(“FTP_PASSWD”); FTPClient client = new FTPClient(); client.connect(hostname); client.login(user, passwd); String reply = client.getStatus(); System.out.println(reply); client.enterRemotePassiveMode(); client.changeWorkingDirectory(“/uploads”); FTPFile[] files = client.listFiles(); System.out.println(files.length); for (FTPFile file : files) { System.out.println(file.getName()); } String[] fileNames = […]

为什么我用Apache Commons FileUpload获得“FileUploadException:Stream意外结束”?

遇到此exception的原因是什么: org.apache.commons.fileupload.FileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly

Apache Commons CLI选项解析器可以忽略未知的命令行选项吗?

我正在编写一个Java应用程序,它接受使用Apache Commons CLI和GnuParser处理的命令行参数。 由于没有兴趣进入的原因,我希望它默默地忽略未知的命令行选项而不是抛出ParseException,但我没有看到这样做的方法。 我看到GnuParser.parse()上有一个stopAtNonOption布尔选项,但我想要的更像ignoreAtNonOption,它会在遇到未知令牌后继续处理选项。 我可以实现我自己的解析器来完成这个但是我很惊讶没有内置这个function所以我想我会在走这条路之前检查一下。 我正在谈论的示例代码: try { CommandLine commandLine = parser.parse(options, args); // stopAtNonOption set to true (below) is also not what I want // CommandLine commandLine = parser.parse(options, args, true); } catch (ParseException e) { LOG.error(“error parsing arguments”, e); throw new RuntimeException(e); }

使用apache-commons-net TelnetClient发送终端命令时如何禁用echo

所以,我有这个使用org.apache.commons.net.telnet.TelnetClient类的类。 它尝试发送命令并读取响应。 public class AutomatedTelnetClient { private TelnetClient telnet = new TelnetClient(); private InputStream in; private PrintStream out; private String prompt = “$”; public AutomatedTelnetClient(String server, String user, String password) { try { EchoOptionHandler echoopt = new EchoOptionHandler(false, false, false, false); telnet.addOptionHandler(echoopt); // Connect to the specified server telnet.connect(server, 23); // Get input and output […]

Apache DefaultHttpClient调用导致“java.lang.RuntimeException:Stub!”

我正在将我的脚趾浸入Android开发中。 我有一个项目将与RESTful资源接口,我正在试图找出如何通过HTTP上的params进行基本GET。 从我读过的所有内容来看,共识似乎都支持HTTPClient优于HttpURLConnection。 我编写了一个包装类,其中包含一个方法,该方法负责实例化密钥对象以使用HTTPClient发出请求: public String get() { String responseString = null; HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(); try { get.setURI(new URI(this.baseURL())); } catch (URISyntaxException e1) { e1.printStackTrace(); } HttpResponse response; try { response = client.execute(get); responseString = readResponse(response.getEntity()); if(response != null) { System.out.println(responseString); } } catch(ClientProtocolException e) { e.printStackTrace(); } […]

html多部分表单中输入文本字段的值

我在一个java服务器端应用程序中使用Apache Commons FileUpload,该应用程序具有带字段的html表单: 将填充目标邮箱的电子邮件地址的目标 带有发件人消息的消息文本 用于上传照片的<input type=file …字段。 我可以接收上传的文件(作为流),但我如何访问1)和2)表单值(由应用程序的用户完成)? 非常感谢,Aurel

我何时应该使用Apache Commons的Validate.isTrue,何时应该使用’assert’关键字?

我何时应该使用Apache Commons的Validate.isTrue,何时应该使用’assert’关键字?

apache commons配置加载属性,直到“,”字符

我想从属性文件加载配置(apache commons配置)。 我的计划是: PropertiesConfiguration pc = new PropertiesConfiguration(“my.properties”); System.out.println(pc.getString(“myValue”)); 在my.properties我有 myValue=value, 用逗号 当我运行程序时,输出是value ,而不是value, with comma 。 看起来像值被加载,直到,字符。 有任何想法吗?

来自apache-commons exec的进程输出

我在这里结束了我的智慧。 我确信这很简单,我很可能在理解java和流时遇到很大漏洞。 我认为有这么多课程,我试图通过API来弄清楚我何时以及如何使用大量的输入/输出流,我有点不知所措。 我刚刚了解了apache commons库的存在(自学java失败),我正在尝试将我的一些Runtime.getRuntime()。exec转换为使用commons – exec。 已经修复了一些每6个月一次这个问题的问题,然后消除了exec的风格问题。 代码执行perl脚本,并在GUI运行时显示GUI中脚本的stdout。 调用代码在swingworker内部。 我迷路了如何使用pumpStreamHandler ……无论如何这里是旧代码: String pl_cmd = “perl script.pl” Process p_pl = Runtime.getRuntime().exec( pl_cmd ); BufferedReader br_pl = new BufferedReader( new InputStreamReader( p_pl.getInputStream() ) ); stdout = br_pl.readLine(); while ( stdout != null ) { output.displayln( stdout ); stdout = br_pl.readLine(); } 我想这就是我在很久以前不完全理解的复制粘贴代码。 上面我假设正在执行该过程,然后抓取输出流(通过“getInputStream”?),将其放入缓冲读取器,然后将循环直到缓冲区为空。 我没有得到的是为什么这里不需要’waitfor’样式命令? 是否可能有一段时间缓冲区将为空,退出循环,并在进程仍在进行时继续? 当我运行它时,情况似乎并非如此。 […]