您好,登錄后才能下訂單哦!
最近在做文件上傳的時候看到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" >
<form action="saveEditSeal_sealManager.action" method="post"
id="form1" name="form1">
<table class="showTab" >
<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"
>
<input type="button" value="保存" class="l-button"
/>
<input type="button" value="取消" class="l-button"
/>
</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;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。