Tag: apache commons

用Java确定另一个国家的互联网时间

我正在开发一个将被世界各地的人们使用的Java应用程序。 一项function要求它显示澳大利亚墨尔本当前时间。 我找到了这个答案,并按如下方式调整了代码,但它返回了我当前的时间(如预期的那样)。 它使用Apache Commons Net库 : try { String TIME_SERVER = “time-a.nist.gov”; NTPUDPClient timeClient = new NTPUDPClient(); InetAddress inetAddress = InetAddress.getByName(TIME_SERVER); TimeInfo timeInfo = timeClient.getTime(inetAddress); long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); return new Date(returnTime); } catch (Exception e) { System.out.println(e.getMessage()); return null; } 如何修改此代码以返回墨尔本的时间,而不是我的时间? 我也愿意接受其他解决方案来解决这个问题。 谢谢! 编辑: 根据Jon的建议,我使用了JodaTime库并构建了以下代码来解决问题。 通过将Australia / Melbourne更改为此处找到的任何时区,它可以用于其他时区。 try { //Get the time […]

使用Apache Commons电子邮件发送邮件时出现问题

我正在尝试使用commons电子邮件发送简单的文本电子邮件 Email email = new SimpleEmail(); email.setHostName(“smtp.gmail.com”); email.setSmtpPort(587); email.setAuthenticator(new DefaultAuthenticator(“user@gmail.com”, “123456”)); email.setTLS(true); email.setFrom(from); email.setSubject(subject); email.setMsg(mesage); email.addTo(“to@gmail.com”); email.send(); 但是当我尝试发送电子邮件时,我得到了以下exception: 我错过了什么吗?

StringEscapeUtils查明字符串是否被转义

我一直在使用StringEscapeUtils.escapeHTML来转义URL。 是否有类似的东西可以找出字符串是否已被转义?

如何在java中提供FTP地址?

我编写了从FTP服务器下载文件的代码。 由于我本地有我的FTP服务器,我想访问“ftp:// localhost / alfresco”。 这是露天的FTP。 我有以下代码 public class FtpTransfer { public static final void main(String[] args) { FTPClient ftp = new FTPClient(); FileOutputStream br = null; try { ftp.connect(“ftp://localhost/alfresco”); ftp.login(“admin”, “admin”); String file = “KPUB//Admin//TMM//Pickup//TMM_TO_ARTESIA_06152010220246.xml”; br = new FileOutputStream(“file”); ftp.retrieveFile(“/”+file, br); System.out.println(“Downloaded…”); } catch(IOException exception) { System.out.println(“Error : “+exception); } } } 发生以下exception。 Error […]

使用不同的格式/模式解析字符串到目前为止

我需要解析一个字符串到一个日期,但没有事先知道字符串将在哪个模式。这类似于如何在不知道格式的情况下将字符串转换为日期的问题? 。 为了解决这个问题,我采用了几种模式来测试结果。 但是,我得到的有点奇怪。 例1: import java.util.Date; import org.apache.commons.lang3.time.DateUtils; public Date extractDate(String dateStr) { String [] datePatterns = {“yyyy-MM-dd”, “dd-MM-yyyy”}; Date date = null; try { date = DateUtils.parseDate(dateStr, datePatterns); } catch (Exception except) { except.printStackTrace(); } return date; } public static void main(String[] args) throws Exception { System.out.println (“2013-09-30:” + extractDate(“2013-09-30”) ); System.out.println (“30-09-2013:” […]

属性文件未使用Apache Commons Configuration反映修改的更改

我正在尝试探索Apache commons配置以动态加载属性文件并在文件中进行修改并保存。 我为此写了一个演示代码。 代码片段 package ABC; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; public class Prop { public static void main(String[] args) { try { URL propertiesURL = Prop.class.getResource(“/d1.properties”); if (propertiesURL == null) { System.out.println(“null”); } String absolutePath=propertiesURL.getPath(); PropertiesConfiguration pc = new PropertiesConfiguration(absolutePath); pc.setReloadingStrategy(new FileChangedReloadingStrategy()); String s=(String)pc.getProperty(“key_account_sales”); System.out.println(“s is ” + s); pc.setAutoSave(true); pc.setProperty(“key_account_sales”, “Dummy”); pc.save(); […]

apache commons压缩的tar问题

我正在努力尝试使用压缩库来tar某些文件。 我的代码如下,取自commons.compress wiki示例: private static File createTarFile(String[] filePaths, String saveAs) throws Exception{ File tarFile = new File(saveAs); OutputStream out = new FileOutputStream(tarFile); TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream(“tar”, out); for(String filePath : filePaths){ File file = new File(filePath); TarArchiveEntry entry = new TarArchiveEntry(file); entry.setSize(file.length()); aos.putArchiveEntry(entry); IOUtils.copy(new FileInputStream(file), aos); aos.closeArchiveEntry(); } aos.finish(); out.close(); return tarFile; } […]

org.apache.commons.dbcp.DelegatingPreparedStatement已关闭

org.apache.commons.dbcp.DelegatingPreparedStatement已关闭 我能知道这种情况会发生在哪种情况下。 我关闭了所有结果集和准备好的语句。 我怎么解决这个问题。 代码: public int UpdateMovementLines(List mlinelist,String projId,String documentno,String user){ int count = 1; int line = 0; String uom = null; String projLocatorId = null; String projWarehouseId = null; String warehouseLocatorId = null; String issuanceId = null; String movementLineId =null; String pinstanceId = null; String sqlQry = null; String whLocatorId = null; […]

反序列化会话数据时出现InvalidClassException错误

这是调用代码: Object attribute = session.getAttribute(name, scope); 这会引发以下错误: Could not deserialize session data. java.io.InvalidClassException: org.apache.commons.lang.time.FastDateFormat; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = -7186497712641044641 环境是Weblogic。 Web应用程序是一个spring portlet。 我正在努力了解如何解决这个问题。

用于压缩(例如LZW)字符串的Java库

Apache Commons Compress仅适用于存档文件(如果我错了,请纠正我)。 我需要类似的东西 MyDB.put(LibIAmLookingFor.compress(“My long string to store”)); String getBack = LibIAmLookingFor.decompress(MyDB.get())); 而LZW只是一个例子,可能是类似的。 谢谢。