Copy kode kodenya sebagai berikut:
mport java.io.File;
impor java.io.FileInputStream;
impor java.io.FileNotFoundException;
impor java.io.FileOutputStream;
impor java.io.IOException;
impor java.io.InputStream;
impor java.util.ArrayList;
impor java.util.List;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
kelas publik DeployByExcel {
logger Logger statis pribadi= Logger.getLogger(DeployByExcel.class);
BUFFER int akhir statis = 8192;
//Unggul
buku kerja HSSFWorkbook pribadi;
/**
* Baca file Excel dan masukkan daftar file ke dalam daftar
* @param sheetNumber
* @param dir Direktori tempat file excel berada
* @kembali
* @melempar FileNotFoundException
* @throwsIOException
*/
Daftar publik<String> getDatasInSheet(int sheetNumber,File dir) menampilkan FileNotFoundException, IOException{
File[] file = dir.listFiles();
Daftar<String> hasil = ArrayList<String>();
untuk(File f : file)
{
if(!f.getName().toLowerCase().endsWith(".xls"))
{
melanjutkan;
}
buku kerja = Buku Kerja HSSF baru(FileInputStream(f) baru);
//Dapatkan tabel yang ditentukan
Lembar HSSFSheet = buku kerja.getSheetAt(sheetNumber);
//Dapatkan jumlah total baris data
int rowCount = lembar.getLastRowNum();
logger.info("jumlah baris excel yang ditemukan: " + rowCount);
if (jumlah baris < 1) {
hasil pengembalian;
}
//Baca data baris demi baris
untuk (int indeks baris = 4; indeks baris <= jumlah baris; indeks baris++) {
//Dapatkan objek baris
Baris HSSFRow = sheet.getRow(rowIndex);
jika (baris != nol) {
Daftar<Objek> rowData = Daftar Array baru<Objek>();
//Dapatkan jumlah sel pada baris ini
int jumlah kolom = baris.getLastCellNum();
//Dapatkan data di setiap sel pada baris ini
Sel HSSFCell = baris.getCell(1);
//Dapatkan data di sel yang ditentukan
String str = (String)ini.getCellString(sel);
jika (str!=null && str.panjang()>1)
hasil.tambahkan(str);
}
}
}
hasil pengembalian;
}
salinan kekosongan pribadi (String sourcePath,String destPath,Daftar<String> fileList,String webContent) melempar IOException{
int angka =1;
untuk (String str : Daftar file){
str = str.replace(".java", ".class");
jika (str.indexOf("/")!=-1){
jika (str.indexOf("src")==0){
str = str.replace("src", "WEB-INF/kelas");
}lainnya jika (str.toUpperCase().indexOf(webContent.toUpperCase())==0){
str = str.replace(webContent+"/", "");
}
boolean f = copyFile(str,sourcePath,destPath);
jika(f)
{
logger.info("Filenya adalah:" + nomor);
nomor++;
Stringnamafile1 = str;
ke dalam n = 1;
while(namafile1.berakhirDengan(".kelas"))
{
str = namafile1.replace(".class", "$" + n +".class");
if(!copyFile(str,sourcePath,destPath))
{
merusak;
}
n++;
}
}
}
}
}
/**
* salin str ke path tujuan
*
* @param str
* @param sourcePath
* @param jalur tujuan
* @return boolean isFile mengembalikan true;else return false;
* @throwsIOException
*/
copyFile boolean pribadi (String str, String sourcePath, String destPath) melempar IOException
{
boolean f = salah;
String destFilePath = destPath+str;
String sourceFilePath = sourcePath+str;
File newDir = File baru(destFilePath.substring(0,destFilePath.lastIndexOf('/')));
File sourceFile = File baru(sourceFilePath.trim());
if(!sourceFile.ada())
{
kembali f;
}
logger.info("tujuan:"+destFilePath+" "+"sumber:"+sourceFilePath);
File destFile = File baru(destFilePath.trim());
if (!newDir.ada()){
newDir.mkdirs();
}
if(!sourceFile.isDirectory())
{
InputStream di=FileInputStream baru(filesumber);
FileOutputStream keluar=FileOutputStream baru(file tujuan);
byte[] buffer=byte baru[1024];
int dalam;
while((ins=in.read(buffer))!=-1){
keluar.tulis(buffer,0,in);
}
melampirkan();
keluar.flush();
keluar.tutup();
f = benar;
}
kembali f;
}
/**
* Dapatkan isi sel
* @sel param
* @kembali
*/
objek yang dilindungi getCellString(sel HSSFCell){
Hasil objek = null;
jika (sel != nol) {
int tipe sel = sel.getCellType();
beralih(Tipe sel){
kasus HSSFCell.CELL_TYPE_STRING:
hasil = sel.getRichStringCellValue().getString();
merusak;
kasus HSSFCell.CELL_TYPE_NUMERIC:
hasil=sel.getNumericCellValue();
merusak;
kasus HSSFCell.CELL_TYPE_FORMULA:
hasil = sel.getNumericCellValue();
merusak;
kasus HSSFCell.CELL_TYPE_ERROR:
hasil=nol;
merusak;
kasus HSSFCell.CELL_TYPE_BOOLEAN:
hasil=sel.getBooleanCellValue();
merusak;
kasus HSSFCell.CELL_TYPE_BLANK:
hasil=nol;
merusak;
}
}
hasil pengembalian;
}
/**
*
* @param args args[0]: direktori tempat file Excel berada; args[1]: direktori sumber (direktori file yang dikompilasi);
* @throwsException
*/
public static void main(String[] args) melempar Pengecualian {
if(args == null || args.panjang <3 )
{
logger.info("file tidak ditemukan;");
logger.fatal("java cn.id5.deploy.DeployByExcel $0 $1 $2 $3 /n$0: direktori tempat file Excel berada; $1: direktori sumber (direktori file yang dikompilasi); $2: direktori rilis; $3: direktori tempat jsp berada (defaultnya adalah webContent, boleh kosong)/nexiting.");
Sistem.keluar(0);
}
File berkas = File baru(args[0]);
DeployByExcel deploy = new DeployByExcel();
Daftar<String> fileList = deploy.getDatasInSheet(0,file);
String classPath = args[1];
String tujuanPath = args[2];
String webContent = (args.length> 3 && args[3] != null && args[3].length() > 1) ?
deploy.copy(classPath, destPath, fileList, webContent);
///tmp/gboss /media/terry/doc/Project_ID5/gboss/WebContent/
}
}