다음과 같이 코드 코드를 복사합니다.
java.io.파일 가져오기;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
java.util.List 가져오기;
org.apache.log4j.Logger 가져오기;
org.apache.poi.hssf.usermodel.HSSFCell 가져오기;
org.apache.poi.hssf.usermodel.HSSFRow 가져오기;
org.apache.poi.hssf.usermodel.HSSFSheet 가져오기;
org.apache.poi.hssf.usermodel.HSSFWorkbook 가져오기;
공개 클래스 DeployByExcel {
개인 정적 로거 로거 logger= Logger.getLogger(DeployByExcel.class);
정적 최종 int BUFFER = 8192;
//뛰어나다
개인 HSSFWorkbook 통합 문서;
/**
* 엑셀 파일을 읽어서 파일 목록을 목록에 넣습니다.
* @param sheetNumber
* @param dir 엑셀 파일이 위치한 디렉토리
* @반품
* @throws FileNotFoundException
* @throwsIOException
*/
public List<String> getDatasInSheet(int sheetNumber,File dir)는 FileNotFoundException, IOException을 발생시킵니다.
파일[] 파일 = dir.listFiles();
List<String> 결과 = new ArrayList<String>();
for(파일 f : 파일)
{
if(!f.getName().toLowerCase().endsWith(".xls"))
{
계속하다;
}
통합 문서 = new HSSFWorkbook(new FileInputStream(f));
//지정된 테이블 가져오기
HSSFSheet 시트 = workbook.getSheetAt(sheetNumber);
//데이터의 총 행 수를 가져옵니다.
int rowCount = sheet.getLastRowNum();
logger.info("발견된 Excel 행 개수: " + rowCount);
if (rowCount < 1) {
결과 반환;
}
//데이터를 한 줄씩 읽어옵니다.
for (int rowIndex = 4; rowIndex <= rowCount; rowIndex++) {
//행 객체 가져오기
HSSFRow 행 = sheet.getRow(rowIndex);
if (행 != null) {
List<Object> rowData = new ArrayList<Object>();
//이 행의 셀 수를 가져옵니다.
int columnCount = row.getLastCellNum();
//이 행의 각 셀에 있는 데이터를 가져옵니다.
HSSFCell 셀 = row.getCell(1);
//지정된 셀의 데이터를 가져옵니다.
String str = (String)this.getCellString(cell);
if (str!=null && str.length()>1)
결과.추가(str);
}
}
}
결과 반환;
}
private void copy(String sourcePath,String destPath,List<String> fileList,String webContent)에서 IOException이 발생합니다.
정수 번호 =1;
for(문자열 str : fileList){
str = str.replace(".java", ".class");
if (str.indexOf("/")!=-1){
if (str.indexOf("src")==0){
str = str.replace("src", "WEB-INF/classes");
}else if (str.toUpperCase().indexOf(webContent.toUpperCase())==0){
str = str.replace(webContent+"/", "");
}
부울 f = copyFile(str, sourcePath, destPath);
만약(에프)
{
logger.info("파일은:" + num);
숫자++;
문자열 파일명1 = str;
int n = 1;
while(fileName1.endsWith(".class"))
{
str = fileName1.replace(".class", "$" + n +".class");
if(!copyFile(str,sourcePath,destPath))
{
부서지다;
}
n++;
}
}
}
}
}
/**
* str을 destPath에 복사
*
* @param str
* @param 소스패스
* @param destPath
* @return boolean isFile은 true를 반환하고, 그렇지 않으면 false를 반환합니다.
* @throwsIOException
*/
private boolean copyFile(String str,String sourcePath,String destPath)에서 IOException이 발생합니다.
{
부울 f = 거짓;
문자열 destFilePath = destPath+str;
문자열 sourceFilePath = sourcePath+str;
File newDir = new File(destFilePath.substring(0,destFilePath.lastIndexOf('/')));
파일 sourceFile = new File(sourceFilePath.trim());
if(!sourceFile.exists())
{
f를 반환;
}
logger.info("dest:"+destFilePath+" "+"source:"+sourceFilePath);
파일 destFile = new File(destFilePath.trim());
if (!newDir.exists()){
newDir.mkdirs();
}
if(!sourceFile.isDirectory())
{
InputStream in=new FileInputStream(sourceFile);
FileOutputStream out=new FileOutputStream(destFile);
바이트[] 버퍼=새 바이트[1024];
int ins;
while((ins=in.read(buffer))!=-1){
out.write(버퍼,0,ins);
}
넣다();
out.flush();
종료.닫기();
f = 사실;
}
f를 반환;
}
/**
* 셀의 내용을 가져옵니다
* @param 셀
* @반품
*/
보호된 객체 getCellString(HSSFCell 셀){
개체 결과 = null;
if (셀 != null) {
int cellType = cell.getCellType();
스위치(셀 유형){
케이스 HSSFCell.CELL_TYPE_STRING:
결과 = cell.getRichStringCellValue().getString();
부서지다;
케이스 HSSFCell.CELL_TYPE_NUMERIC:
결과=cell.getNumericCellValue();
부서지다;
사례 HSSFCell.CELL_TYPE_FORMULA:
결과 = cell.getNumericCellValue();
부서지다;
사례 HSSFCell.CELL_TYPE_ERROR:
결과=널;
부서지다;
사례 HSSFCell.CELL_TYPE_BOOLEAN:
결과=cell.getBooleanCellValue();
부서지다;
케이스 HSSFCell.CELL_TYPE_BLANK:
결과=널;
부서지다;
}
}
결과 반환;
}
/**
*
* @param args args[0]: 엑셀 파일이 위치한 디렉토리 args[1]: 소스 디렉토리(컴파일된 파일 디렉토리); args[2]: 릴리스 디렉토리
* @throwsException
*/
public static void main(String[] args)에서 예외가 발생합니다.
if(args == null || args.length <3 )
{
logger.info("파일을 찾을 수 없습니다;");
logger.fatal("java cn.id5.deploy.DeployByExcel $0 $1 $2 $3 /n$0: Excel 파일이 있는 디렉터리; $1: 소스 디렉터리(컴파일된 파일 디렉터리); $2: 릴리스 디렉터리; $3: jsp가 있는 디렉터리 (기본값은 webContent이며 비어 있을 수 있음)/nexiting.");
시스템.exit(0);
}
파일 file = new File(args[0]);
DeployByExcel 배포 = 새로운 DeployByExcel();
List<String> fileList = 배포.getDatasInSheet(0,file);
문자열 classPath = args[1];
문자열 destPath = args[2];
String webContent = (args.length> 3 && args[3] != null && args[3].length() > 1) ? args[3] : "WebContent";
배포.복사(classPath, destPath, fileList, webContent);
///tmp/gboss /media/terry/doc/Project_ID5/gboss/WebContent/
}
}