Struts2代码
/*****发送短信导入手机列表********************************************************************/
/*****发送短信导入手机列表********************************************************************/
/**
* (发送短信)导入手机列表
* */
public String importPhone()
{
try {
String account = getAccount();
Validate.notEmpty(account, "用户未登录");
// 开始导入数据,并取得返回的表名
Validate.notNull(upload, "请选择要上传导入的数据文件,或所选择文件不符合上传导入格式要求。"); //校验
Validate.isTrue(uploadFileName.endsWith(".txt"), "对不起,你所选的文件不是txt文件,请重新选择!");//校验
String fileRealPath = ServletActionContext.getServletContext().getRealPath("/readxls")
+ "//upload//temp//";
File imageFile = new File(fileRealPath + uploadFileName);
String rename = uploadFileName;
Date now=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
int i = 1;
while (imageFile.exists()) {
rename =sdf.format(now) +"_"+(i++) + ".txt";
imageFile = new File(fileRealPath + rename);
if (i > 65535) {
break;
}
}
uploadFileName = rename;
FileUtils.copyFile(upload, imageFile);
String filePath=fileRealPath + uploadFileName; //绝对路径
String fp = "/readxls/upload/temp/"+uploadFileName; //相对路径
filePaths = fp;
//System.out.println("---------------4 filePath:"+filePath);
}catch(Exception ex)
{
setOpResult("error"); //便于在jsp页面中提取相关错误信息
setMessage(ex.getMessage());//便于在jsp页面中提取相关错误信息
}
return SUCCESS;
}
/
JSP代码:
<%@page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
request.setAttribute("webPath", path);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>服务站</title>
<link rel="stylesheet" rev="stylesheet"
href="${webPath}/main/css/mycss.css" type="text/css" media="all" />
<script type="text/javascript"
src="${webPath}/common/js/util/checkBoxUtil.js"></script>
<jsp:include page="/common/ext2/ext-all.jsp" flush="true" />
<script type="text/javascript"
src="${webPath}/common/js/helper/extAjaxCommon.js"></script>
<!-- jsp:include page="/common/ext2/ext-simple.jsp" flush="true" /-->
<script language="javascript">
//以下是提取java类中相关提示错误信息
var opresult = '${opResult}';
var msg ="${message}";
if(opresult =="error"){
alert(msg);
}
function doImport(){
if(document.myform.upload.value==""){
alert("请选择要上传的文件!");
return false;
}
myform.action="importPhone.action";
myform.submit();
}
<s:if test='ifSave.equals("yes")'>
alert("保存成功!");
</s:if>
<s:elseif test='ifSave.equals("no")'>
alert("没有要保存的数据!");
</s:elseif>
</script>
</head>
<body>
<div id="bluebox">
<form name="myform" enctype="multipart/form-data" method="post"
action="">
<table>
<tr>
<td width="40%"><span style="color: #FF0000">*</span><input name="upload" type="file" class="A" r onMouseOver="this.className='AA'" onMouseOut="this.className='A'" size="20"></td>
<td>
<input type="button" class="B" onMouseOver="this.className='BA'" onMouseOut="this.className='B'" value="模板下载" onClick="window.location.href='${webPath}/fileUpload/download.action?fileName=phonetemplate.txt&inputPath=/readxls/upload/template/phonetemplate.txt'" />
<input type="button" name="btnImp" class="B" onMouseOver="this.className='BA'" onMouseOut="this.className='B'" onClick="doImport();" value="上传">
<input type="hidden" name="filePaths" id="filePaths" value="${filePaths}" ><span style="color: #FF0000">注:只支持.txt文件</span>
</td>
</tr>
</table>
</form>
</div>
<script type="text/javascript">
var filePaths = document.getElementById("filePaths").value;
if(!(''==filePaths)){
parent.setFilePaths(filePaths);
}
</script>
</body>
</html>