最近在做文件上传的时候看到ajaxfileupload来做文件的上传,感觉很有意思。记录下来方便以后在做相关的功能是自己查阅。
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset="utf-8">
<title>上传印章</title>
<link href="../../lib/ligerUI/skins/Aqua/css/ligerui-all.css"
rel="stylesheet" type="text/css" />
<script src="../../lib/jquery/jquery-1.3.2.min.js"
type="text/javascript">
</script>
<script src="../../lib/ligerUI/js/core/base.js" type="text/javascript">
</script>
<script src="../../lib/ligerUI/js/plugins/ligerDateEditor.js"
type="text/javascript">
</script>
<script src="../../lib/ligerUI/js/plugins/ligerComboBox.js"
type="text/javascript">
</script>
<script src="../../lib/ligerUI/js/plugins/ligerButton.js"
type="text/javascript">
</script>
<script src="../../lib/ligerUI/js/plugins/ligerDialog.js"
type="text/javascript">
</script>
<script src="../../lib/ligerUI/js/plugins/ligerDialog.js"
type="text/javascript">
</script>
<script src="../../lib/ligerUI/js/plugins/ligerTextBox.js"
type="text/javascript">
</script>
<script type="text/javascript"
src="../../lib/jquery/ajaxfileupload.js">
</script>
<style type="text/css">
* {
font-size: 12px;
}
</style>
<script type="text/javascript">
$(function() {
$("#start").ligerDateEditor();
$("#end").ligerDateEditor();
});
function saveSeal() {
var beginDate = document.getElementById("start").value;
var endDate = document.getElementById("end").value;
if (beginDate != "" && endDate != "") {
if (beginDate > endDate) {
$.ligerDialog.warn("开始日期必须小于结束日期");
return false;
}
}
$.ajax( {
type : 'post',
async : false,
url : 'saveEditSeal_sealManager.action',
data : $('#form1').serialize(),
success : function(jsons) {
if (!jsons) {
$.ligerDialog.error("操作失败");
} else {
$.ajaxFileUpload( {
url : "saveEditSealFile_sealManager.action?id="
+ $("#sealNO").val(),
secureuri : false,
fileElementId : [ 'sealfile', 'signfile', 'idfile' ],
dataType : 'json',
/**success : function(data,status){
alert(data);
}**/
success : function(json) {
if (json == true) {
$.ligerDialog.warn("操作成功", function(value) {
if (value) {
window.parent.location.reload();
//window.location.reload();
}
});
} else {
$.ligerDialog.warn("操作失败", function(value) {
if (value) {
window.parent.location.reload();
}
});
}
}
});
}
}
})
}
/**
* 取消按钮事件
*/
function cancel() {
//alert("kkk");
//$(".l-dialog-close").close();
parent.$.ligerDialog.close();
parent.$(".l-dialog,.l-window-mask").remove();//只隐藏遮罩层
}
</script>
<style type="text/css">
body {
padding: 10px;
}
.showTit {
background: url(../../lib/ligerUI/skins/icons/communication.gif);
background-repeat: no-repeat;
line-height: 25px;
padding-left: 25px;
font-weight: bold;
border-bottom: 1px solid #ccc;
}
.showTab {
line-height: 30px;
text-align: left;
}
</style>
</head>
<body>
<% String userid=(String)request.getAttribute("userid");
String id=(String)request.getAttribute("id");
%>
<%-- 编辑印章信息--%>
<div id="target1" align="center" style="margin: 3px; ">
<form action="saveEditSeal_sealManager.action" method="post"
id="form1" name="form1">
<table class="showTab" style="width: 500px; margin-top: 20px;">
<tr>
<td width="90px" align="left">
印章编号:
</td>
<td>
<input type="text" id="sealNO" name="sealinfo.sealno" />
</td>
<td align="left">
印章类型:
</td>
<td>
<select style='width: 130px' id="type" name="sealinfo.type">
<option value="1">
公章
</option>
<option value="0">
签名章
</option>
</select>
</td>
</tr>
<tr>
<td width="100px">
印章刻文:
</td>
<td>
<input type="text" id="content" name="sealinfo.content" />
</td>
<td>
使用年限:
</td>
<td>
<select style='width: 130px' id="indate" name="sealinfo.indate">
<option value="1">
1
</option>
<option value="3">
3
</option>
<option value="5">
5
</option>
<option value="0">
永久
</option>
</select>
</td>
</tr>
<tr>
<td>
数字证书:
</td>
<td>
<input id="signfile" name="signfile" style='width: 130px'
type="file" />
</td>
<td>
印章文件:
</td>
<td>
<input id="sealfile" name="sealfile" style='width: 130px'
type="file" />
</td>
</tr>
<tr>
<td align="left">
开始日期:
</td>
<td>
<input type="text" id="start" name="sealinfo.start" />
</td>
<td>
结束日期:
</td>
<td>
<input type="text" id="end" name="sealinfo.end" class="time"/>
</td>
</tr>
<tr>
<td align="left">
持章人姓名:
</td>
<td>
<input type="text" id="name" name="sealinfo.sealusername" />
</td>
<td>
手机号码:
</td>
<td>
<input type="text" id="phone" name="sealinfo.phone" />
</td>
</tr>
<tr>
<td>
邮箱:
</td>
<td>
<input type="text" id="email" name="sealinfo.email" />
</td>
<td>
key:
</td>
<td>
<select style='width: 130px' id="key" name="sealinfo.usbkey">
<option value="1">
配置
</option>
<option value="0">
不配置
</option>
</select>
</td>
</tr>
<tr>
<td align="left">
持章人***:
</td>
<td>
<input id="idNO" name="sealinfo.idno" />
</td>
<td>
上传***:
</td>
<td>
<input id="idfile" name="idfile" style='width: 130px' type="file" />
</td>
</tr>
<tr>
<td align="left">
证书类型:
</td>
<td>
<select id="certype" name="sealinfo.certype" style='width: 130px'>
<option value="免费证书">免费证书</option>
</select>
</td>
</tr>
<input type="hidden" id="sealid" name="sealinfo.id" />
<input type="hidden" id="inventory" name="sealinfo.inventory" value="<%=id %>" />
<input type="hidden" id="sealuserid" name="sealinfo.userid" value="<%=userid %>" />
<tr>
<td colspan="4" align="right"
style="height: 50px; line-height: 50px;">
<input type="button" value="保存" class="l-button"
style="width: 80px; display: inline-block; margin-top: 30px;"
/>
<input type="button" value="取消" class="l-button"
style="width: 80px; display: inline-block; margin-top: 30px;"
/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
action层只要代码代码
public class SealManagerAction{
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(SealManagerAction.class);
private ISealManagerService sealManagerService;
//private PageBean pageBean = new PageBean();
//private MinicollectSeals minicollectSeals;
private ISealUserService sealUserService;
private ISealinfoService sealinfoService;
private File sealfile;// 印章文件
private String sealfileFileName;
private File signfile;// 数字签名证书
private String signfileFileName;
private File idfile;// 个人***
private String idfileFileName;
private Sealinfo sealinfo;
private Sealuser sealuser;
..............
public void saveEditSeal() {
try {
Sealinfo sealinfo1 = new Sealinfo();
User user = (User) request.getSession().getAttribute("loginUser");
Sealinfo sealinfo2=sealinfoService.findSealinfoByInventory(sealinfo.getInventory());
if (sealinfo2!=null) {
sealinfo2.setUserid(sealinfo.getUserid());
sealinfo2.setApplytype(sealinfo.getApplytype());
sealinfo2.setCertype(sealinfo.getCertype());
sealinfo2.setContent(sealinfo.getContent());
sealinfo2.setEmail(sealinfo.getEmail());
sealinfo2.setEnd(sealinfo.getEnd());
sealinfo2.setIdno(sealinfo.getIdno());
sealinfo2.setIndate(sealinfo.getIndate());
sealinfo2.setPhone(sealinfo.getPhone());
sealinfo2.setSealno(sealinfo.getSealno());
sealinfo2.setSealusername(sealinfo.getSealusername());
sealinfo2.setStart(sealinfo.getStart());
sealinfo2.setType(sealinfo.getType());
sealinfo2.setUsbkey(sealinfo.getUsbkey());
sealinfo2.setInventory(sealinfo.getInventory());
sealinfoService.addSealinfo(sealinfo2);
} else {
sealinfo1.setUserid(sealinfo.getUserid());
sealinfo1.setApplytype(sealinfo.getApplytype());
// sealinfo1.setCerpath(cerpath)
sealinfo1.setCertype(sealinfo.getCertype());
sealinfo1.setContent(sealinfo.getContent());
sealinfo1.setEmail(sealinfo.getEmail());
sealinfo1.setEnd(sealinfo.getEnd());
sealinfo1.setIdno(sealinfo.getIdno());
sealinfo1.setIndate(sealinfo.getIndate());
sealinfo1.setPhone(sealinfo.getPhone());
sealinfo1.setSealno(sealinfo.getSealno());
sealinfo1.setSealusername(sealinfo.getSealusername());
sealinfo1.setStart(sealinfo.getStart());
sealinfo1.setType(sealinfo.getType());
sealinfo1.setUsbkey(sealinfo.getUsbkey());
sealinfo1.setInventory(sealinfo.getInventory());
sealinfoService.addSealinfo(sealinfo1);
}
/*
* String id=sealinfo.getId(); Gson gson = new
* GsonBuilder().setDateFormat("yyyy-MM-dd").create(); String
* outString=gson.toJson(sealinfo);
*/
response.getWriter().print(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error(e.getMessage());
}
}
public void saveEditSealFile() {
String idno = request.getParameter("id");
String sealpath = UploadConfigurationRead.getInstance().getConfigItem(
"uploadSealFilePath").trim();
String signpath = UploadConfigurationRead.getInstance().getConfigItem(
"uploadSignFilePath").trim();
String idpath = UploadConfigurationRead.getInstance().getConfigItem(
"uploadIdFilePath").trim();
try {
Sealinfo sealinfo = sealinfoService.findSealinfo(idno);
//Sealuser sealuser = sealinfo.getSealuser();
if(sealfile!=null&&sealfile.length()>0){
File xmlFile = uploadXmlFile(sealpath, sealinfo.getId(), sealfile);
sealpath = sealXMLParse(xmlFile,sealpath,sealinfo.getId());
// if(xmlFile.exists()){
// xmlFile.delete();
// }
sealinfo.setSealpath(sealpath);
}
if(signfile!=null&&signfile.length()>0){
signpath = uploadFile(signpath, sealinfo.getSealno()
+ signfileFileName.substring(signfileFileName.lastIndexOf(".")), signfile);
sealinfo.setCerpath(signpath);
}
if(idfile!=null&&idfile.length()>0){
idpath = uploadFile(idpath, sealinfo.getSealno()
+ idfileFileName.substring(idfileFileName.lastIndexOf(".")), idfile);
sealinfo.setIdpath(idpath);
}
//sealUserService.save(sealuser);
sealinfoService.addSealinfo(sealinfo);
response.getWriter().print(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error(e.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error(e.getMessage());
}
}
public String uploadFile(String directory, String filename, File file)
throws IOException {
String fiePath = directory + File.separator + filename;
File target = new File(fiePath);
FileUtils.copyFile(file, target);
return fiePath;
}
public File uploadXmlFile(String directory,String filename,File file) throws IOException{
File target = new File(directory + File.separator + filename + ".xml");
FileUtils.copyFile(file, target);
return target;
}
}