Saturday, May 28, 2005

Reading Date in Excel

We can use Jakarta COMMON POI to do it:

E.g. of Itertate over a file:

// load the excel
FileInputStream fin = new FileInputStream("simple1.xls");
POIFSFileSystem fs = new POIFSFileSystem(fin);
//create a Workbook
HSSFWorkbook wb = new HSSFWorkbook(fs);

// get a reference to the worksheet
int numberPage = wb.getNumberOfSheets();
// TODO:debug information
out.printf("Number Of Page %s%n", numberPage);

//read each sheet
for (int i = 0; i < numberPage; ++i) {
HSSFSheet sheet = wb.getSheetAt(i);

// TODO:debug information
int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum();
out.printf("FirstRow: %s, LastRow: %s%n", firstRow, lastRow);
out.printf("Physical Row: %s%n", sheet.getPhysicalNumberOfRows());

//read each Row
for (int j = firstRow; j <= lastRow; ++j) {
HSSFRow row = sheet.getRow(j);

//read each Cell
for (short k = row.getFirstCellNum(); k < row.getLastCellNum(); ++k) {
HSSFCell cell = (HSSFCell) row.getCell(k);

switch (cell.getCellType()) {

case (HSSFCell.CELL_TYPE_STRING): {
out.print(cell.getStringCellValue() + "\t");
break;
}

case (HSSFCell.CELL_TYPE_NUMERIC): {
out.print(cell.getNumericCellValue() + "\t");
break;
}

case (HSSFCell.CELL_TYPE_BOOLEAN): {
out.print(cell.getBooleanCellValue() + "\t");
break;
}

}
}
out.println();
}
}
fin.close();

Sunday, May 15, 2005

Security

http://java.sun.com/j2se/1.5.0/docs/api/index.html
http://java.sun.com/j2se/1.5.0/docs/guide/security/doprivileged.html
http://java.sun.com/j2se/1.5.0/docs/guide/security/PolicyFiles.html#DefaultLocs

STill working add more later..

Upload file in a JSP

利用 Oreilly MultiPartRequest 做的範例

http://www.javaworld.com.tw/Old/High/Upload.htm

利用 jspsmart SmartUpload 做的範例

http://www.javaworld.com.tw/Old/High/S_Upload.htm

Jakarta 當然也不能缺席嘛, 來個利用 Jakarta Commons FileUpload 做的範例 Tongue

都是拿 browser 大大的東西來改的, 所以 File.html 拿這裡的就對了 Big Smile

http://www.javaworld.com.tw/Old/High/Upload.htm

也請參閱 jini 葛格的電子報

http://mychannel.pchome.com.tw/channel/class/class_paper_open.htm?d=2003-07-04&e=jakarta&t=.htm&j=17&f=main&v=1

解決了有些瀏覽器會傳送 path + filename 的問題

在電子報這行

// 因為不同的瀏覽器會造成傳遞 path + filename, 有些則只有 filename

註解下就是 jini 葛格加的程式碼

File.jsp (2.36k)

Sunday, May 08, 2005

Call other program in Java

Today some one asked me about calling other porgram in Java. In java 5.0 beside Runtime.exec() we can use the class Process and ProcessBuilder to have a better control on the execute:

ProcessBuilder:
This class is used to create operating system processes.
Note that this class is not synchronized.
When calling the ProcessBuilder().start() we can get a Process object and monitor the output thru it.

Process p = new ProcessBuilder("ping", "localhost").start();
String line = null;
Scanner in = new Scanner(p.getInputStream());
while (in.hasNextLine()) {
line = in.nextLine();
System.out.printf("%s\n", line);
}
Beside when can get the Environment variables by Map ProcessBuilder().environment() and set our own variable to it, usually the values is a copy of the environment of the current process System.getenv() :
PS. o start a process with an explicit set of environment variables, first call Map.clear() before adding environment variables.
 ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory("myDir");
Process p = pb.start();

Process

It provides Process.getInputStream() and Process.getOutputStream() to read and write data.