您的位置:首页 > 其它

每日记载内容总结29

2013-12-26 14:37 417 查看
1.struts下上传以及解析excel文件

jsp页面代码:

<form action="email_importExcel.do" method="post" enctype="multipart/form-data">
<input type="file" size="30" name="uploadExcel" id="fileup"/>
<!--注意name要与后台对应 -->
<br><br>
<input name="up" type="button" value="Upload" onclick="onSubmit()"/>
</form>


action代码:

public File uploadExcel ;//页面传来的file
public String uploadExcelFileName ;//页面传来的file的名字
public String uploadExcelContentType ;//页面传来的file的类型
//以上三个在action里面get set之后可以直接获取。注意变量的名字,File类的变量与页面name一样,然后+FileName就是文件名 ,+ContentType就是文件类型


public String importExcel(){

if (uploadExcel != null) {
try {
//上传开始
InputStream is = new FileInputStream(uploadExcel);
File file = new File(getAppPropValue("excel.email.path"));
if (!file.exists()) {
file.mkdirs();
}
File excelPath =new File(getAppPropValue("excel.email.path")+ File.separator + uploadExcelFileName);
if (excelPath.exists()) {
excelPath.delete();
}
// 创建一个输出流
OutputStream os = new FileOutputStream(excelPath);
//设置缓存
byte[] buffer = new byte[1024];
int length = 0;
//读取myFile文件输出到toFile文件中
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
//关闭输入流
is.close();
//关闭输出流
os.close();
//上传结束
//读取文件开始
InputStream iStream = new FileInputStream(excelPath);
Workbook wb = Workbook.getWorkbook(iStream); // 从文件流中获取Excel工作区对象(WorkBook)
Sheet sheet = wb.getSheet(0); // 从工作区中取得页(Sheet)
int ignoreRows = 1 ;
emails = new ArrayList<Email>();
if (sheet.getRows() > 0 && sheet.getColumns() > 0 ) {
for (int i = ignoreRows; i < sheet.getRows(); i++) { // 循环打印Excel表中的内容
Email email= new Email();
String emailId = sheet.getCell(0, i).getContents();
String emStr =sheet.getCell(1, i).getContents();
if (StringUtils.isNotEmpty(emailId) && Integer.parseInt(emailId) > 0 && StringUtils.isNotEmpty(emStr)) {
email.setEmailId(Integer.parseInt(emailId));
email.setEmail(emStr);
email.setFirstName(sheet.getCell(2, i).getContents());
email.setLastName(sheet.getCell(3, i).getContents());
email.setCompany(sheet.getCell(4, i).getContents());
email.setPhone(sheet.getCell(5, i).getContents());
email.setDescri(sheet.getCell(6, i).getContents());
emailManager.addEmail(email);
emails.add(email);
}
}
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS ;
}else {
return INPUT ;
}

}


2.异常:java.io.FileNotFoundException:******(拒绝访问)

File file = new File(fileName);
InputStream is = new FileInputStream(file);

如果fileName不是文件,而是文件夹,而且路径中存在这个文件夹,就是报上述异常。

把fileName路径指向文件即可

关于mkdir以及mkdirs:

mkdir:如果路径最底层的目录的上级目录不存在,则不会创建,也就是说只会创建一层文件夹,多层则不行
mkdirs:会按照路径创建出文件夹,每层都是文件夹.如果filePath指向的是文件,会创建(文件名.扩展名)的文件夹

3.lucene 多个field相同时 如
Field name1=new Field("name","zhangle",Field.Store.YES,Field.Index.TOKENIZED);
Field name2=new Field("name","liwei",Field.Store.YES,Field.Index.TOKENIZED);
Field name3=new Field("name","wangwu",Field.Store.YES,Field.Index.TOKENIZED);
在StandardAnalyzer分析器下,与
Field name1=new Field("name","zhangle liwei wangwu",Field.Store.YES,Field.Index.TOKENIZED);
是一样的

但是在获取域里面的值的时候,是有区别的
第一种返回的是数组
String[] catpaths = inSearcher.doc(scoreDoc.doc).getValues("name");
第二种返回的是字符串
String name = inSearcher.doc(scoreDoc.doc).get("name");

4.实时监控数据库变化 (来源:http://blog.csdn.net/winter13292/article/details/7466289)

使用mysql自带的功能监控数据库变化:

(1)打开数据库配置文件my.ini (一般在数据库安装目录)(D:\MYSQL)

(2)在数据库的最后一行添加 log=log.txt

(3)重启mysql

(4)去数据库数据目录 (例如D:\MYSQL\data) 你会发现多了一个log.txt文件,里面就是记录数据库变化

5.mybatis遇到的问题以及解决方案(来源:/article/1911148.html):

(1)Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'education' in 'class esd.bean.Personal'

<!-- 这个属性是多余的,在对应的bean类中没有对应的字段,删除后OK -->
<if test="education != null and education != ''">
education = #{education},
</if>


解决方法:

在mapper配置文件中删除多配置的属性/或则在自己的bean类中补全该字段

(2)<if>标签中不支持 && 符号

<if test="code != null && code !=''">
code=#{code},
</if>
<!-- 要改为-->
<if test="code != null and code !=''">
code=#{code},
</if>


(3)同一sql配置文件中id重复

Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for GeographyMapper.getByCode

两个sql语句的id重复了,就会报这个错.

(4)没有在configuration.xml配置对应的sql配置文件

错误:

Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ***Mapper.*** Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ***Mapper.***

解决方法:

在configuration.xml配置文件中引用对应的sql配置文件

<mappers>
<mapper resource="esd/db/mapper/ResumeMapper.xml" />
</mappers>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: