我期望得到以下但实际上没有.即使它在我尝试使用String而不是Item Object时也能正常工作.我想知道为什么原因以及如何编码以获得预期的结果.谢谢.
EXPECTED
------------------------------
removed object are:
2
same object are:
1
3
add object are:
4
------------------------------
Run Code Online (Sandbox Code Playgroud)
ACTUAL
------------------------------
removed object are:
1
2
3
same object are:
add object are:
1
3
4
------------------------------
Run Code Online (Sandbox Code Playgroud)
package com.javastudy;
import java.util.ArrayList;
import java.util.List;
public class CollectionCompareToObjects {
public static void main(String[] args) {
List<Item> before = new ArrayList<Item>();
List<Item> after = new ArrayList<Item>();
before.add(new Item(1L));
before.add(new Item(2L)); // delete
before.add(new Item(3L));
after.add(new Item(1L));
after.add(new Item(3L));
after.add(new Item(4L)); // added
List<Item> removed …Run Code Online (Sandbox Code Playgroud) 我有一个在 JBoss 6.1 中运行的 JSF 应用程序,它使用内部的 Tomcat Servlet 容器。
我已经实现了使用 apache commons 文件上传的上传。我想防止上传过大的文件,并
fileSizeMax在类中将该属性设置为 10MB FileUploadBase。它有效,文件上传会FileSizeLimitExceededException为所有大于 10MB的文件抛出一个。此异常会在不到一秒的时间内引发。但主要问题是,整个文件将通过网络传输。我通过检查网络流量发现了这一点。之后重定向到错误页面完成。
如何在超过最大大小时中断文件传输而不传输整个文件?我假设由于 web 表单属性,文件将在多个包中传输enctype
="multipart/form-data"。
java jsf file-upload apache-commons apache-commons-fileupload
我正在提交带有文本和文件类型输入字段的表单,并使用此代码获取文本数据
但问题是
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process normal fields here.
//Taking all text and doing task
System.out.println("Field name: " + item.getFieldName());
System.out.println("Field value: " + item.getString());
} else {
// Process <input type="file"> here.
//And Leaving this at this time
}
}
Run Code Online (Sandbox Code Playgroud)
如果我解析请求并逐一迭代它,然后在formField中我用来获取所有文本参数,然后我再次在文件类型条件中使用此代码上传文件,因此它不会再次解析
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process normal fields here.
//Leaving this section this time …Run Code Online (Sandbox Code Playgroud) 我已经通过javadoc但我找不到任何东西.它内部缓冲数据吗?如果是,由于没有关闭/刷新操作,它如何确保所有数据都已写入.如果它没有缓冲,那么每次调用都会进入磁盘吗?
我在这里查看了源代码,http://grepcode.com/file/repo1.maven.org/maven2/commons-io/commons-io/2.4/org/apache/commons/io/FileUtils.java#FileUtils.openOutputStream %28java.io.File%2Cboolean%29它似乎没有做任何缓冲.它每次都打开一个流.
我正在开发一个将被世界各地的人们使用的Java应用程序.一项功能要求它显示澳大利亚墨尔本当前时间.
我找到了这个答案,并按如下方式调整了代码,但它返回了我当前的时间(如预期的那样).它使用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;
}
Run Code Online (Sandbox Code Playgroud)
如何修改此代码以返回墨尔本的时间,而不是我的时间?我也愿意接受其他解决方案来解决这个问题.
谢谢!
编辑:
根据Jon的建议,我使用了JodaTime库并构建了以下代码来解决问题.通过将澳大利亚/墨尔本更改为此处找到的任何时区,它可以用于其他时区.
try {
//Get the time for the current time zone.
String TIME_SERVER = "time-a.nist.gov";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime …Run Code Online (Sandbox Code Playgroud) 我目前正在使用Apache commons配置库来编写和读取文件中的数据.我能够将键值对colors = hello保存到user..properties文件中,但是当我尝试读取该值时,获取以下异常.
Exception in thread "main" java.lang.IllegalArgumentException: 'hello' does not contain an equals sign
at org.apache.commons.configuration.AbstractConfiguration.getProperties(AbstractConfiguration.java:625)
at org.apache.commons.configuration.AbstractConfiguration.getProperties(AbstractConfiguration.java:579)
at com.code.prep.CommonsMain.readProperties(CommonsMain.java:21)
at com.code.prep.CommonsMain.main(CommonsMain.java:12)
Run Code Online (Sandbox Code Playgroud)
代码如下
package com.code.prep;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class CommonsMain {
public static void main(String[] args) {
CommonsMain main = new CommonsMain();
main.readProperties();
// main.writeProperties();
}
public void readProperties(){
PropertiesConfiguration config = new PropertiesConfiguration();
try {
config.load("user.properties");
System.out.println(config.getProperties("colors"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public void writeProperties(){
PropertiesConfiguration config = new PropertiesConfiguration();
try { …Run Code Online (Sandbox Code Playgroud) 我正在尝试做的事:尝试使用Apache电子邮件验证程序查看电子邮件是否有效.我用它作为基本代码来测试它是否有效:
import org.apache.commons.validator.*;
public class main {
public static void main(String[] args) {
String email = "example@gmail.com";
EmailValidator emailvalidator = new EmailValidator();
if(emailvalidator.isValid(email)) {
System.out.println("Email is valid");
}
else {
System.out.println("Email is invalid");
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题:我在EmailValidator构造函数中遇到错误"构造函数EmailValidator()不可见".这导致整个程序无法正常工作.
我的问题:我该如何解决这个错误.请非常详细,因为我对Java很新.
我试图在java中实现ftp下载.我正在使用apache common-net library但是我得到了这个异常.我打印出下面的堆栈跟踪我不知道我错过了什么
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:317)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:608)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:582)
at org.apache.commons.net.ftp.FTP.pasv(FTP.java:1007)
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:869)
at org.apache.commons.net.ftp.FTPClient._retrieveFile(FTPClient.java:1854)
at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1845)
Run Code Online (Sandbox Code Playgroud)
我有一个方法如下
public void downloadFromFtp(Map<String, String> ftpMap, String sourceWithPath,
String destinationFolder) throws IOException {
String hostname = ftpMap.get("hostname");
String username = ftpMap.get("username");
String password = ftpMap.get("password");
if (null == hostname || null == username || null == password) {
throw new InvalidInputException(
"Invalid RMS FTP hostname/username/password");
}
//Connect to ftp url
ftpClient.connect(hostname, 21);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//login …Run Code Online (Sandbox Code Playgroud) 我正在使用 apachecommons-text:RandomStringGenerator来生成这样的随机数String:
//Utilities
private static RandomStringGenerator generator(int minimumCodePoint, int maximumCodePoint, CharacterPredicates... predicates) {
return new RandomStringGenerator.Builder()
.withinRange(minimumCodePoint, maximumCodePoint)
.filteredBy(predicates)
.build();
}
public static String randStringAlpha(int length) {
return generator('A', 'z', CharacterPredicates.LETTERS).generate(length);
}
public static String randStringAlphaNum(int length) {
return generator('1', 'z', CharacterPredicates.LETTERS, CharacterPredicates.DIGITS).generate(length);
}
//Generation
private void foo() {
String alpha = randStringAlpha(255);
String num = randStringAlphaNum(255);
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种使用相同库生成以下内容的方法:
我有一个用例,我需要存储大约大小的键 - 值对。大小为 8 GB 的单个 VM 中的 5 亿个条目。Key 和 Value 属于 Long 类型。键从 1、2、3 开始自动递增,依此类推。
仅当我在程序开始时构建这个 Map[KV] 结构作为独占操作,一旦构建,仅用于查找,在此结构中不会执行更新或删除。
我已经用 java.util.hashMap 尝试过这个,但正如预期的那样,它消耗了大量内存并且程序给 OOM:堆使用超过错误。
我需要一些有关以下方面的指导,这有助于减少内存占用,我对访问性能有所下降感到满意。
apache-commons ×10
java ×10
collections ×2
file-upload ×2
email ×1
ftp ×1
guava ×1
java-8 ×1
jsf ×1
list ×1
random ×1
servlets ×1
time ×1