Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果要对100k文档进行编码,则将PDF文件编码到base64需要更多时间_Java_Base64_Fileinputstream - Fatal编程技术网

Java 如果要对100k文档进行编码,则将PDF文件编码到base64需要更多时间

Java 如果要对100k文档进行编码,则将PDF文件编码到base64需要更多时间,java,base64,fileinputstream,Java,Base64,Fileinputstream,我正在尝试将pdf文档编码为base64,如果它的数量较少(比如2000个文档),那么它工作得很好。但是我有100k加上要编码的两个元素 对所有这些文件进行编码需要更多的时间。有没有更好的方法对大数据集进行编码 请找到我目前的方法 String filepath=doc.getPath().concat(doc.getFilename()); file = new File(filepath); if(file.exists() && !file.isDirecto

我正在尝试将pdf文档编码为base64,如果它的数量较少(比如2000个文档),那么它工作得很好。但是我有
100k
加上要编码的两个元素

对所有这些文件进行编码需要更多的时间。有没有更好的方法对大数据集进行编码

请找到我目前的方法

 String filepath=doc.getPath().concat(doc.getFilename());

 file = new File(filepath);
    if(file.exists() && !file.isDirectory()) {
        try {
            FileInputStream fileInputStreamReader = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            fileInputStreamReader.read(bytes);
            encodedfile = new String(Base64.getEncoder().encodeToString(bytes));
            fileInputStreamReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
试试这个:

  • 计算出需要编码的文件数

    int files = Files.list(Paths.get(directory)).count();
    
  • 将它们拆分为线程可以在java中处理的合理数量。也就是说,如果您有100k个文件需要编码。把它分成1000个列表,1000个,类似的

    int currentIndex = 0;
    for (File file : filesInDir) {
        if (fileMap.get(currentIndex).size() >= cap)
            currentIndex++;
        fileMap.get(currentIndex).add(file);
    }
    /** Its going to take a little more effort than this, but its the idea im trying to show you*/
    
  • 如果计算机资源可用,则逐个执行每个工作线程

    for (Integer key : fileMap.keySet()) {
         new WorkerThread(fileMap.get(key)).start();
    }
    
  • 您可以通过以下方式检查当前可用资源:

     public boolean areResourcesAvailable() {
         return imNotThatNice();
     }
    
    /**
     * Gets the resource utility instance
     * 
     * @return the current instance of the resource utility
     */
    private static OperatingSystemMXBean getInstance() {
        if (ResourceUtil.instance == null) {
            ResourceUtil.instance = ManagementFactory.getOperatingSystemMXBean();
        }
        return ResourceUtil.instance;
    }
    
    你看过这个帖子吗?