前台表单
<form action="" method="post" enctype="multipart/form-data"
style="padding: 0px; height: 31px; line-height: 31px; display: inline;"
name="inputForm" id="inputForm">
<table border="0" cellpadding="0" cellspacing="0" align="center"
width="100%">
<tr>
<td style="text-align: left;">
<label>
<input type="hidden" name="type" id="type" value="${type}"/>
选中导入的Excel:
<input type="file" name="excelFile" id="uf"
style="width: 200px; height: 23px; line-height: 23px;" />
<input type="button" value="导入" οnclick="input()"
style="width: 50px; height: 23px;" />
<input type="button" value="下载导入格式"
οnclick="javascript:window.location.href='imp!download.ssl?type=${type}'"
style="width: 100px; height: 23px;" />
</label>
</td>
<td style="width: 80px; text-align: right;"></td>
</tr>
</table>
</form>
前台验证js
<script type="text/javascript" language="javascript">
//导入
function input() {
var fileName = $("#uf").val();
if (fileName == "") {
alert("请选择要导入的Excel文件!");
} else {
var extension = fileName.substring(fileName.lastIndexOf(".") + 1,
fileName.length);
if (extension != null && extension != "") {
var c_extension = extension.toLowerCase();
if (c_extension == "xls") {
document.inputForm.action = "imp!importDriver.ssl";
$("#inputForm").submit();
} else {
alert("选择的文件格式不正确,请重新选择!");
}
} else {
alert("选择时出现异常!");
}
}
}
</script>
action方法
File excelFile;// 导入文件
String excelFileFileName;// 导入文件的名称
/**
* 批量导入数据
*
* @return
*/
public String importDriver() {
HttpServletRequest request = ServletActionContext.getRequest();
Map session = ActionContext.getContext().getSession();// 获得session
String realPath = ServletActionContext.getServletContext().getRealPath(
"/ExcelUpload");
String upfileFileName = "";
String extendName = "";
try {
if (excelFile != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm");
// 扩展名
extendName = excelFileFileName.substring(excelFileFileName
.lastIndexOf("."));
upfileFileName = "导入机手" + df.format(new Date()) + extendName;
File saveFile = new File(new File(realPath), upfileFileName);
if (!saveFile.getParentFile().exists())
saveFile.getParentFile().mkdirs();
FileUtils.copyFile(excelFile, saveFile);
}
} catch (Exception e) {
request
.setAttribute("warnInfo",
"<script>alert(\'上传失败\')</script>");
e.printStackTrace();
}
String filePath = realPath + "/" + upfileFileName;
if (!"".equals(extendName) && ".xls".equals(extendName)) {
importService.importDriver(filePath);
}
request.setAttribute("warnInfo", "<script>alert(\'上传成功\')</script>");
return "import";
}
public String download() {
HttpServletRequest request = ServletActionContext.getRequest();
String type = request.getParameter("type");
setImportType(type);
return "exe";
}
public InputStream getDownloadFile() {
InputStream is = null;
String path = "";
path = ServletActionContext.getServletContext().getRealPath(
"/ExcelUpload/导入机手格式.zip");
try {
is = new FileInputStream(path);
} catch (Exception e) {
e.printStackTrace();
}
return is;
}
public String getFileName() {
String name = "";
try {// 解决下载文件中文文件名问题
name = new String("导入机手格式.zip".getBytes("GBK"), "ISO8859-1");
} catch (Exception e) {
e.printStackTrace();
}
return name;
}