- 新建MAVEN工程
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class App {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("D:\\aaa.xlsx");
byte[] buff = new byte[is.available()];
is.read(buff);
is.close();
int threadTotal = 2;
int start = 1;
ExecutorService es = Executors.newFixedThreadPool(threadTotal);
for (int i = 0; i < threadTotal; i++) {
es.execute(new MyTask(start, threadTotal, i, buff));
}
}
}
class MyTask implements Runnable {
static String result = null;
byte[] buf = null;
int start = 0;
int step = 0;
int mark = 0;
public MyTask(int start, int threadTotal, int mark, byte[] buf) {
this.start = start + mark;
this.mark = mark;
this.step = threadTotal;
this.buf = buf;
}
public String mm(long i) {
if (i < 52) {
if (i < 26)
return String.valueOf((char) (65 + i));
else
return String.valueOf((char) (97 + i - 26));
} else {
return mm(i / 52 - 1) + mm(i % 52);
}
}
private boolean work(String pwd) {
try {
InputStream is = new ByteArrayInputStream(buf);
WorkbookFactory.create(is, pwd);
} catch (EncryptedDocumentException e) {
return false;
} catch (IOException e) {
} catch (InvalidFormatException e) {
}
MyTask.result = pwd;
return true;
}
@Override
public void run() {
Thread.currentThread().setName("T" + mark);
int cnt = 0;
while (MyTask.result == null) {
if (cnt++ == 100 && this.mark == 0) {
System.out.println(Thread.currentThread().getName() + " running at " + start + ", " + mm(start));
cnt = 1;
}
if (work(mm(start)))
break;
start += step;
}
System.err.println(">>>>>>>>>>>>>>>>" + MyTask.result);
}
}