/**
* 上传文件
*
* @return
* @throws Exception
*/
public String uploadSensitiveWords() throws Exception {
String suffix = fileName.substring(fileName.lastIndexOf('.'), fileName.length());
if (suffix.equals(".xls") || suffix.equals(".xlsx") || suffix.equals(".Xlsx")) {
String savePath = createTemporaryFile();
String realPath = savePath + "/" + fileName;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(docmentFile));
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(realPath));
byte[] buff = new byte[8192];
for (int len = -1; (len = bis.read(buff)) != -1;) {
bos.write(buff, 0, len);
}
bos.flush();
} catch (IOException ie) {
ie.printStackTrace();
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
ReadDemo(savePath);
}
return "none";
}
/**
* 读取文件
*
* @return
* @throws Exception
*/
public void ReadDemo(String savePath) throws Exception {
InputStream inputStream = new FileInputStream(savePath+"/" + fileName);
Workbook workbook = WorkbookFactory.create(inputStream);
int sheetCount = workbook.getNumberOfSheets();
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = workbook.getSheetAt(i);
int rows = sheet.getLastRowNum() + 1;
Row tmp = sheet.getRow(0);
if (tmp == null) {
continue;
}
for (int row = 1; row < rows; row++) {
Row r = sheet.getRow(row);
Domain domain = new Domain();
if (r.getCell(0) != null) {
r.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
domain.setId(Integer.valueOf(r.getCell(0).getStringCellValue()));
} else {
domain.setSensitiveId(null);
}
dao.insert(domain);
}
}
deleteTemporaryFile();
}
/**
* 创建临时文件
*/
public String createTemporaryFile() {
String savePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
File file = new File(savePath);
if (!file.exists() && !file.isDirectory()) {
file.mkdir();
}
return savePath;
}
/**
* 删除临时文件
*/
public void deleteTemporaryFile() {
String savePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
File fileDelete = new File(savePath);
for (File fileDeletes : fileDelete.listFiles()) {
fileDeletes.delete();
}
fileDelete.delete();
}