我一直在使用RandomStringUtils生成随机 ID 来用作数据库密钥:
import org.apache.commons.lang.RandomStringUtils;\npublic class RandomStringTest {\n public static void main(final String[] args) {\n for (int i = 0; i <= 10; i++) {\n final String id = RandomStringUtils.random(8,\n "0123456789abcdefghijklmnopqrstuvwxyz");\n System.out.println(id);\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n按键空间足够大,
\n\nlen("0123456789abcdefghijklmnopqrstuvwxyz")^8 = 2821109907456 \xe2\x89\x83 10^12\nRun Code Online (Sandbox Code Playgroud)\n\n随机机制是否正确播种?在将其应用于生产之前,我需要知道密钥是否已正确分发。
\n\n顺便说一句,测试代码在执行几次后没有显示重复,但这还远非可靠的证明。
\nApache Commons 压缩库似乎专注于将 TarArchiveEntry 写入 TarArchiveOutputStream。但看起来创建 TarArchiveEntry 的唯一方法是使用 File 对象。
我没有要写入 Tar 的文件,内存中有 byte[] 或最好有流。我不想将一堆临时文件写入磁盘只是为了构建 tar.gz 文件。
有什么办法我可以做这样的事情:
TarEntry entry = new TarEntry(int size, String filename);
entry.write(byte[] fileContents);
TarArchiveOutputStream tarOut = new TarArchiveOutputStream();
tarOut.write(entry);
tarOut.flush();
tarOut.close();
Run Code Online (Sandbox Code Playgroud)
或者,甚至更好......
InputStream nioTarContentsInputStream = .....
TarEntry entry = new TarEntry(int size, String filename);
entry.write(nioTarContentsInputStream);
TarArchiveOutputStream tarOut = new TarArchiveOutputStream();
tarOut.write(entry);
tarOut.flush();
tarOut.close();
Run Code Online (Sandbox Code Playgroud) 维基百科列出了计算正态分布累积概率的多种数值方法。然而,使用 Apache Commons Math,您不需要了解其中任何一个,因为该库只是为您完成工作:
NormalDistribution normal = new NormalDistribution(mu, sigma);
normal.cumulativeProbability(x);
Run Code Online (Sandbox Code Playgroud)
对于一些研究项目,我很想知道他们使用什么方法。有谁知道 Apache Commons Math 使用什么方法来近似正常累积值?是来自维基百科中列出的方法还是他们实现了不同的方法?
我有以下代码,用于将 CSV 下载为 ajax 按钮单击,但文件未下载。只在浏览器上显示黑色的新标签。
@RequestMapping(value = "/batch/download", method = RequestMethod.POST, produces = "text/csv")
@ResponseBody
public void downloadNGIBatchSelected(HttpServletResponse response) throws IOException {
List<String> ids = Arrays.asList("1312321","312313");
generateNewCustomerCSV(response.getWriter(),ids);
}
private void generateNewCustomerCSV(PrintWriter writer, List<String> ids){
String NEW_LINE_SEPARATOR = "\n";
//CSV file header
Object[] FILE_HEADER = {"Token Number",
"Token Expiry Date",
};
CSVPrinter csvPrinter = null;
try {
csvPrinter = new CSVPrinter(new BufferedWriter(writer), CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR));
//Create CSV file header
csvPrinter.printRecord(FILE_HEADER);
for (PolicyMap PolicyMap : policyMaps) {
List customerCSV = new ArrayList();
customerCSV.add(PolicyMap.getInsurancePolicy().getTokenNo());
try …Run Code Online (Sandbox Code Playgroud) 我最近刚刚开始使用 Eclipse,在尝试安装外部库时遇到了问题。按照在线教程,我将该.jar文件添加到类路径中,并在引用的库文件夹中看到它。尽管如此,当尝试导入时,我收到错误:
org.apache.commons 包无法访问
作为参考,我正在尝试安装 apache math commons 库。
当值被引用时,我需要在模式下使用 Apache 公共资源中的 CSVPrinter,但标头不是。看起来报价模式是唯一的选项,影响标题和值。这个可以独立完成吗?
CSVFormat format = CSVFormat.DEFAULT.withHeader(new String(){"a", "b"})
.withQuoteMode(QuoteMode.ALL);
CSVPrinter printer = new CSVPrinter(new FileWriter(new File("out.csv")), format);
printer.printRecord("a val", "b val");
printer.printRecord("1", "2");
printer.flush();
printer.close();
Run Code Online (Sandbox Code Playgroud)
给出:
"a", "b"
"a val", "b val"
"1", "2"
Run Code Online (Sandbox Code Playgroud)
但要求是这样的:
a,b
"a val", "b val"
"1", "2"
Run Code Online (Sandbox Code Playgroud) Apache在尝试上传文件时返回此错误(我只保留了堆栈跟踪的第一行和根本原因):
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /upload.jsp:40
37:
38: try {
39:
40: items = upload.parseRequest(request);
41: } catch (FileUploadException e) {
42: out.println(e);
43: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
root cause
javax.servlet.ServletException: org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(Lorg/apache/commons/fileupload/RequestContext;)Ljava/util/List;
root cause
java.lang.NoSuchMethodError: org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(Lorg/apache/commons/fileupload/RequestContext;)Ljava/util/List;
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
if(ServletFileUpload.isMultipartContent(request)){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items …Run Code Online (Sandbox Code Playgroud) 我试图使用DateUtilsfrom apache-commons3,但无法理解它所依赖的时区:
Date date = DateUtils.truncate(date, Calendar.DATE);
Run Code Online (Sandbox Code Playgroud)
怎么知道我在哪个时区?
我需要进入一个项目的Apaches Common Chain。因此,我尝试在下面运行一个基本示例:http : //www.onjava.com/pub/a/onjava/2005/03/02/commonchains.html
Commons Chain是通过Maven安装的。
我写了以下Chain Base:
public class PFChain extends ChainBase {
public PFChain() {
super();
addCommand(new CalcE());
addCommand(new CalcDOEB());
addCommand(new CalcG());
}
public static void executePFChain() {
Command process = new PFChain();
Context context = new ContextBase();
try {
process.execute(context);
} catch (Exception e) {
System.out.println("errortext");
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的三个命令类如下所示:
public class CalcDOEB implements Command {
@Override
public boolean execute(Context context) throws Exception {
System.out.println("Calculating DOEB...");
return true;
}
}
public class CalcE implements …Run Code Online (Sandbox Code Playgroud) 鉴于以下内容:
public class Person {
private Car car;
// .. bean stuff
}
Run Code Online (Sandbox Code Playgroud)
.
public class Car {
private CarStuff carStuff;
// .. bean stuff
}
Run Code Online (Sandbox Code Playgroud)
我可以使用BeanUtils来设置Person的"car"属性:
BeanUtils.setProperty(person, "car", theirCar);
Run Code Online (Sandbox Code Playgroud)
好的,这非常容易.现在如何通过它的名字获得"汽车"?
BeanUtils.getProperty(person, "car")
Run Code Online (Sandbox Code Playgroud)
将返回一个String,但Car不是String
我试过了:
Map<String, ? extends Object> props = new HashMap<>();
BeanUtils.populate(person, props);
Run Code Online (Sandbox Code Playgroud)
但是没有"car"条目,并且调查文档,输出(虽然很有可能是Object类型)仍然是String或String []
我的类都有适当的bean getter和setter方法.我怎样才能获得价值?
apache-commons ×10
java ×7
apache ×1
compression ×1
csv ×1
eclipse ×1
file ×1
getter ×1
javabeans ×1
jsp ×1
maven ×1
properties ×1
random ×1
spring-boot ×1
tar ×1
upload ×1