标签: apache-commons

Java:RandomStringUtils 随机种子

我一直在使用RandomStringUtils生成随机 ID 来用作数据库密钥:

\n\n
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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

按键空间足够大,

\n\n
len("0123456789abcdefghijklmnopqrstuvwxyz")^8 = 2821109907456 \xe2\x89\x83 10^12\n
Run Code Online (Sandbox Code Playgroud)\n\n

随机机制是否正确播种?在将其应用于生产之前,我需要知道密钥是否已正确分发。

\n\n

顺便说一句,测试代码在执行几次后没有显示重复,但这还远非可靠的证明。

\n

java random apache-commons

3
推荐指数
1
解决办法
9966
查看次数

我可以将流或字节写入 Apache Commons 压缩 Tarfile 吗?

Apache 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)

compression apache file tar apache-commons

3
推荐指数
1
解决办法
3082
查看次数

Apache Commons 数学正态累积概率

维基百科列出了计算正态分布累积概率的多种数值方法。然而,使用 Apache Commons Math,您不需要了解其中任何一个,因为该库只是为您完成工作:

NormalDistribution normal = new NormalDistribution(mu, sigma);
normal.cumulativeProbability(x);
Run Code Online (Sandbox Code Playgroud)

对于一些研究项目,我很想知道他们使用什么方法。有谁知道 Apache Commons Math 使用什么方法来近似正常累积值?是来自维基百科中列出的方法还是他们实现了不同的方法?

java normal-distribution apache-commons apache-commons-math

3
推荐指数
1
解决办法
2389
查看次数

使用 spring boot 和 apache commons 下载 csv

我有以下代码,用于将 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)

apache-commons export-to-csv spring-boot

3
推荐指数
1
解决办法
6239
查看次数

错误:无法访问包 org.apache.commons

我最近刚刚开始使用 Eclipse,在尝试安装外部库时遇到了问题。按照在线教程,我将该.jar文件添加到类路径中,并在引用的库文件夹中看到它。尽管如此,当尝试导入时,我收到错误:

org.apache.commons 包无法访问

错误

作为参考,我正在尝试安装 apache math commons 库。

java eclipse apache-commons

3
推荐指数
1
解决办法
2万
查看次数

CSVPrinter 仅删除标题中的引号

当值被引用时,我需要在模式下使用 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)

java csv apache-commons

3
推荐指数
1
解决办法
1525
查看次数

Apache commons - > File Upload - > parseRequest()错误

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)

upload jsp apache-commons

2
推荐指数
1
解决办法
1万
查看次数

DateUtils使用哪个时区?

我试图使用DateUtilsfrom apache-commons3,但无法理解它所依赖的时区:

Date date = DateUtils.truncate(date, Calendar.DATE);
Run Code Online (Sandbox Code Playgroud)

怎么知道我在哪个时区?

java apache-commons

2
推荐指数
1
解决办法
2149
查看次数

Apache Commons Chain基本示例

我需要进入一个项目的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)

java apache-commons maven

2
推荐指数
1
解决办法
2810
查看次数

Java获取bean值

鉴于以下内容:

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方法.我怎样才能获得价值?

java getter properties javabeans apache-commons

2
推荐指数
1
解决办法
2625
查看次数