java 导入导出txt

Oracle数据导出成txt格式

dao
                
/**
* 获得所有数据
*/
public String getAllWord() {
Session session=super.getSession();
StringBuffer strb = new StringBuffer();
String sql="SELECT * FROM WORD_T";
try {
Connection conn = session.connection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
String word = rs.getString("word");
long num = rs.getLong("num");
strb.append(word + "\t" + num + "\r\n");
}
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strb.toString();
}


/**
* 批量导入数据
* @throws SQLException
*/
@SuppressWarnings("deprecation")
public void insertWord(String word,Long num){
Session session=super.getSession();
Connection conn=session.connection();
PreparedStatement stmt;
try
{
stmt = conn.prepareStatement("insert into KEYWORD_T(id,word,num,last_time) values(WORD_ID_SEQ.nextval,?,?,sysdate)");
stmt.setString(1, word);
stmt.setLong(2, num);
stmt.execute();
stmt.close();
} catch (SQLException e)
{

}
}


Servlet
/**
*导出成txt格式
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try {
WebApplicationContext cxt = ContextLoader.getCurrentWebApplicationContext();
WordDao wordDao=(WordDao)cxt.getBean("wordDao");
String txt = wordDao.getAllWord();
response.setHeader("Content-Disposition", "attachment; filename=\"download.txt\"");
response.setHeader("Content-Type ", "application/octet-stream");

/* 创建输出流 */
ServletOutputStream servletOS = response.getOutputStream();
try {
IOUtils.write(txt, servletOS);
} catch (RuntimeException e) {
}
servletOS.flush();

} catch (Exception e) {
System.out.println("servlet导出异常 ");
} finally {

}

Action
/**
*导入txt到oracle数据库
*/

/**
* 关键字导入
*
* @return
*/
public String fileUp() {
if (super.getRequest().getMethod().equalsIgnoreCase("post")) {
if (txtWord.length() == 0) {
saveMessage("上传失败:请选择上传文件");
return INPUT;
}
if (txtWord.length() > 10000000) {
saveMessage("上传失败:请选择上传文件");
return INPUT;
}
try {
if (null != getTxtWordFileName()
& idEnableUploadType(1, getTxtWordFileName())) {
FileInputStream inStream = new FileInputStream(txtWord);
List<String> lines = IOUtils.readLines(inStream, "GBK");
Iterator<String> itr = lines.iterator();

while (itr.hasNext()) {
String line = itr.next();
String[] temps = line.split("\t");
Word word = new Word();
try {
if (temps.length > 1) {
wordService.insertWord(lines.size(), temps[0], Long
.valueOf(temps[1]));

} else {
wordService.insertWord(lines.size(), temps[0], Long
.valueOf(1));
}
} catch (Exception e) {

}
}
}
} catch (Exception e) {

}

}
return SUCCESS;

} /**
* 判断文件格式
*
* @param fileType
* @param filename
* @return
*/
public static boolean idEnableUploadType(int fileType, String filename) {
String enableExtNames = null;
int p = filename.lastIndexOf(".");
String fileExtName = filename.substring(p).toLowerCase();
if (fileType == 1) {
enableExtNames = ".txt";
}
// else if(fileType==2)
// {
// enableExtNames="";
// }
if (enableExtNames != null) {
if (enableExtNames.indexOf(fileExtName) != -1)
return true;
else
return false;
} else {
return true;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值