package com.servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import com.hsun.HLog;
import com.utils.UpdateMerInterface;
public class UploadImportExcel extends HttpServlet {
String upDirPathName;
String upDirDatePathName;
File tmpDir = null;
File saveDir = null;
static int sUploadFileIndex = 1000000;
private static final ReentrantLock m_lock = new ReentrantLock();
public static int getNextUploadFileIndex() {
m_lock.lock();
int iretindex = sUploadFileIndex;
sUploadFileIndex += 1;
if (sUploadFileIndex > 9000000)
sUploadFileIndex = 1000000;
m_lock.unlock();
return iretindex;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
if (ServletFileUpload.isMultipartContent(request)) {
String UserNameId = request.getParameter("UserNameId");
String mtype = request.getParameter("mtype");
HLog.m_messagelog.debug("UserNameId:" + UserNameId);
DiskFileItemFactory dff = new DiskFileItemFactory();
dff.setRepository(this.tmpDir);
dff.setSizeThreshold(1024000);
ServletFileUpload sfu = new ServletFileUpload(dff);
sfu.setFileSizeMax(100000000L);
sfu.setSizeMax(3000000000L);
FileItemIterator fii = sfu.getItemIterator(request);
while (fii.hasNext()) {
FileItemStream fis = fii.next();
if ((!fis.isFormField()) && (fis.getName().length() > 0)) {
String fileName = fis.getName();
String fieldName = fis.getFieldName();
if (fieldName.length() < 5)
continue;
String subfileName = fieldName.substring(0, 4);
System.out.println("333333:" + subfileName);
if (!subfileName.equals("file"))
continue;
String strFileIndexId = fieldName.substring(4);
HLog.m_messagelog.debug("fileName:" + fileName
+ ",subfilename:" + subfileName
+ ",strFileIndex:" + strFileIndexId);
HLog.m_messagelog
.debug("UploadQualifiFile:dopost,filename:"
+ fileName + ",fieldName:" + fieldName);
BufferedInputStream in = new BufferedInputStream(
fis.openStream());
Date now = new Date();
// SimpleDateFormat sdateformat = new SimpleDateFormat(
// "yyyyMMddHHmmss");
// String strNowDate = sdateformat.format(now);
String newFileName = UserNameId + ".xls";
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(
this.upDirPathName + newFileName)));
Streams.copy(in, out, true);
System.out.println("1111:" + this.upDirPathName
+ newFileName);
UpdateMerInterface.readExcel(this.upDirPathName + newFileName);
}
}
response.setContentType("text/html;charset=UTF-8");
String retString = "<html><body><h1>通道切换成功</h1></body></html>";
response.getWriter().println(retString);
}
} catch (Exception e) {
e.printStackTrace();
HLog.m_messagelog.error("UploadQualifiFile:", e);
response.setContentType("text/html;charset=UTF-8");
response.getWriter()
.println(
"<html><body><input type=\"button\" value=\"上传失败\"/></html>");
}
}
public void init() throws ServletException {
super.init();
String rootpathname = getServletContext().getRealPath("/");
String tmpPath = rootpathname + "/tmpdir/";
String savePath = rootpathname + "batchexcel";
this.upDirPathName = savePath;
Date now = new Date();
SimpleDateFormat sdateformat = new SimpleDateFormat("yyyyMMdd");
String strNowDate = sdateformat.format(now);
this.upDirDatePathName = strNowDate;
this.upDirPathName = (this.upDirPathName + "/");
this.tmpDir = new File(tmpPath);
this.saveDir = new File(savePath);
if (!this.tmpDir.isDirectory())
this.tmpDir.mkdir();
if (!this.saveDir.isDirectory()) {
this.saveDir.mkdir();
this.saveDir = new File(this.upDirPathName);
this.saveDir.mkdir();
} else {
this.saveDir = new File(this.upDirPathName);
if (!this.saveDir.isDirectory())
this.saveDir.mkdir();
}
}
}
package com.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import DBConnect.DBConnectManager;
import DBConnect.DBConnection;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class UpdateMerInterface {
//读取Excel文件
public static void readExcel(String file)
throws BiffException, IOException{
List<String> list = new ArrayList<String>();
Workbook rwb = null;
Cell cell = null;
//创建输入流
InputStream stream = new FileInputStream(file);
//获取Excel文件对象
rwb = Workbook.getWorkbook(stream);
//获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
//行数
for(int i=0; i<sheet.getRows(); i++){
//获取第i行,第j列的值 (本次为全部获取第i行第一列的值)
cell = sheet.getCell(0,i);
String str = cell.getContents();
list.add(str);
}
if(list!=null&&list.size()>0){
updateMerInterface(list);
}else{
System.out.println("表数据为空");
}
}
public static void updateMerInterface(List<String> list){
// 切换通道到 27 | 000022 | new--SYX | 1
String sql = "update merchant set up_interface_no = '000022' where merchant_no in(";
for(int i=0;i<list.size();i++){
//判断商户号长度
if(list.get(i)!=null&&list.get(i).length()==15){
sql+=list.get(i)+",";
}
}
sql = sql.substring(0,sql.length()-1)+")";
List tmplist_par = new ArrayList();
DBConnection tmpconn = DBConnectManager.getDbConn();
int iret = tmpconn.updateSQL(sql, tmplist_par);
if (iret < 0){
DBConnectManager.releaseDbConn(tmpconn);
}else{
DBConnectManager.freeDbConn(tmpconn);
}
}
}
分享到:
相关推荐
利用jxl操作excel文件,提取exce文件数据生成txt文件。
jxl JAVA Excel解析,主要用于修改、上传、下载Excel文件。
jxl实现excel上传下载全部功能, 详细代码和java文件
读写Excel jxl.jar 读xml (log4j 文件上传下载笔记)
利用java开发技术,jxl方式上传Excel表格,使用时请修改pojo实体类,运行main方法时,则不使用import及success的jsp动态页面。此源码无数据库配置文件,内有数据库连接工具类
数据导入Excel运用JXL的各类方法介绍
jxl 上传、下载 excel表格后台测试范例(整个工程文件); 只是本人所写的后台案例,可以直接通过void main 测试。可读取单个sheet,也可以读取多个sheet。具体类名是 ReadWriteExcel.java
运用apache的fileupload库实现文件上传,运用jxl分析导入的excel数据。 只需在现有页面上添加一个“导入”按钮就可实现excel导入数据库的功能,点击“导入”按钮,选择要上传的文件后会立即开始上传。 采用iframe的...
实现Excel的上传和下载 存在两种格式的上传和下载,分别对应POI和JXL 可以实现根据Excel模板导出,自定义Excel样式导入 形成自定义的Excel报表等功能。项目为Maven项目,JDK采用1.8
java文件上传时所需的架包,解析Excel的jxl包。
1.可在配置文件中配置上传路径。 2.支持合并单元格。 3.采用common_dbutils连接数据库。 4.采用jxl实现对excel的操作。 5.附excel模板。
重新上传一次,欢迎大家下载!
Java处理Excel使用的jxl.jar包,Java处理文件上传需要的Apache commons-fileupload.jar包,做JSP开发的朋友,支持下
个人JAVA笔记汇总,包括jxl读写Excel文件,文件上传保存下载, 读xml,log4j,hibernate/struts简单的笔记,j2se,java swing应用,j2me各方面编程等
java读写excel的操作文档,其中包括读取、新建以及拷贝修改EXCEL文件。只需要jxl.jar包,我已经上传,同样是0分下载的
利用java,将excel数据转换为xml测试用例,转换后可将xml文件直接导入Testlink 1.9.3; 内含xml所有jar包,省去搜索相关jar麻烦,lib文件内含dom4j-1.6.1.jar jdom.jar org.apache.poi.jar jxl.jar; 源代码可扩展,...
15个最实用的javaWeb开发工具直接拿来用, 包括以下: jfreechart报表 jxl导入excel log4j日志配置 ...poi报表 FCK文本编辑器 ajax校验 session失效自动跳转到登录...文件上传进度条 权限配置文件(dom4j) 验证码+记住我
springboot-demo项目介绍首次... JXL创建自定义excel、JXL解析excel (3). poi 创建自定义Excel文件、POI解析excel (4). ftp上传下载工具类(5). MP3播放码云特技使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.
第11章 文件基本操作及文件上传下载 372 11.1 文件的基本操作 373 11.2 无组件的文件上传 412 11.3 通过组件实现文件上传 418 11.4 文件下载 428 第12章 文件的批量管理 436 12.1 文件的批量操作 437 12.2 文件的...