//文本框获取焦点,将光标移动到最末端
$("#textId").focus(function(){
var textNode = document.getElementById("textId");
var count = textNode.value.length;
var text = textNode.createTextRange();
text.moveStart('character',count);
text.collapse(true);
text.select();
});
//上传下载控件
<script type="text/javascript" src="../js/jquery.form.js"></script>
<span class="cgreen pl5 i18ntitle" title="service.batch.upload.file">
<form id="upLoadServiceForm" action="/ssp/ui/upAndDownload?moduleFlag=cusBatchSend&operFlag=upload" method="post" ENCTYPE="multipart/form-data">
<fieldset >
<input type="file" name="filePath" id="filePath" style="width:300px"/>
</fieldset>
</form>
</span><span class="pl15"><a href="#" id="upLoadServiceFile" class="ssp_btn_01"><span class="i18nsrc">pon.button.upload</span></a></span>
//上传文件按钮 $("#upLoadServiceFile").on("click",function(){
var upLoadServiceForm = $('#upLoadServiceForm');
var uploadFilePath = $("#filePath").val();
if("" == uploadFilePath || "undefined" == uploadFilePath)
{
showAlertDialog(getMsgSrc("select.import.obj"), 0, okHandler);
return;
}
var options = {
dataType : "json",
success : function(data) {
if('successful' == data.result)
{
showAlertDialog(getMsgSrc("service.upload.success"), 0, okHandler);
//上传成功,将文件名作为全局变量保存
uploadFileName = data.content;
}
if('failed' == data.result)
{
showAlertDialog(data.content, 0, okHandler);
}
},
error : function(data)
{
showAlertDialog(getMsgSrc("databaseError.msg"), 2, okHandler);
}
};
upLoadServiceForm.ajaxSubmit(options);
return !1;
});
private void upLoadBatchService(HttpServletRequest request, String savepath)
{
PrintWriter writer = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// 文件上传的部分
if (isMultipart)
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 得到每个表单域,暂时将它们都当做FileItem
List<FileItem> fileItems = null;
try
{
upload.setFileSizeMax(MAX_FILE_SIZE);
fileItems = (List<FileItem>) upload.parseRequest(request);
}
catch (FileUploadException e)
{
writer.write(JSONUtil.getResultStr(AsConst.ResultValue.FAILED, ResourceManager.getRes("upAndDownload.max.size.prompt") +" "+ MAX_FILE_SIZE/(1024*1024) + " M"));
writer.flush();
writer.close();
}
String uploadFileName = "";
// 对每个表单域进行处理
for (FileItem fileItem : fileItems)
{
// 判断每个item是否是文件上传的域(若为 true,则是非文件上传控件的表单域)
if (!fileItem.isFormField())
{
try
{
String fileName = fileItem.getName();
fileName = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
uploadFileName = fileName;
File dir = new File(FileUtil.getIEMPHome() + savepath);
// 如果路径不存在,则直接创建
if (!dir.exists())
{
dir.mkdirs();
}
File filePath = new File(FileUtil.getIEMPHome() + savepath + fileName);
// 保存上传的文件到指定的目录
fileItem.write(filePath);
FileUtil.setFileSecurityPermission(FileUtil.getIEMPHome() + savepath + fileName);
}
catch (Exception e)
{
writer.write(JSONUtil.getResultStr(AsConst.ResultValue.FAILED, e.getMessage()));
writer.flush();
writer.close();
return;
}
// 当前只支持单文件上传
}
}
writer.write(JSONUtil.getResultStr(AsConst.ResultValue.SUCCESSFUL, uploadFileName));
writer.flush();
writer.close();
}
}
//下载文件
private void dLoadBatchService(HttpServletResponse response, HttpServletRequest request, PrintWriter writer,
String savepath, String fName, String isSecondReq) throws IOException, MsgException
{
// 清空response
response.reset();
String fileName = "";
String browser = request.getHeader("USER-AGENT");
// 判断是ie浏览器
if (browser.contains("MSIE"))
{
fileName = URLEncoder.encode(fName, "utf-8");
fileName = fileName.replaceAll("\\+", "%20");
fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
}
// 判断是谷歌浏览器
if (browser.contains("Chrome"))
{
fileName = new String(fName.getBytes("utf-8"), "ISO8859-1");
}
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.setContentType("text/html;charset=" + RESOURCE_CHAR_SET);
// 如果是第一次请求,将成功与否结果返回给前台ajax
if (null == isSecondReq || "".equals(isSecondReq))
{
writer.write(JSONUtil.getResultStr(AsConst.ResultValue.SUCCESSFUL));
return;
}
OutputStream out = null;
// 从服务器端读取文件
File file = new File(FileUtil.getIEMPHome() + savepath + fName);
FileInputStream fis = null;
out = response.getOutputStream();
try
{
fis = new FileInputStream(file);
// byte缓存数组
byte[] b = new byte[1024];
int len = -1;// 每次读取的字节数
// 输出流 到客户端
while ((len = fis.read(b)) != -1)
{
out.write(b, 0, len);
}
}
catch (FileNotFoundException e)
{
// 定义错误码找不到文件
throw new MsgException(ErrorCode.DOWNLOAD_FILE_ERRORCODE);
}
finally
{
if (null != writer)
{
fis.close();
writer.flush();
writer.close();
}
}
}