index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <!-- 这样写也可以 -->
<script type="text/javascript" src="${pageContext.request.contextPath }/js/commons.js"></script> <!-- 这样写也可以 -->
<script type="text/javascript">
$(document).ready(function() {
$("#pdfa").click(function() {
var pdft = $("#pdft").val();
if (StringUtils.isBlank(pdft)) {
alert("输入为空");
return;
}
StringUtils.syncAjax('pdf.html', {
'username' : pdft
}, function(result) {
if (100 == result.statusCode) {
alert("111111");
} else {
alert("222222");
}
});
});
});
// ajax 同步发送,自己写的
function syncAjax(myUrl, myData, sufn) {
$.ajax({
url : myUrl,
data : myData,
type : 'post',
dataType : 'json',
cache : false,
async : false,
success : function(result) {
alert("suc:" + result);
if (result.statusCode == 200) {
alert("会话超时,请重新登录!");
window.location.href = "index.jsp";
} else {
if (sufn)
sufn(result);
}
},
error : function(msg) {
alert("error:" + msg);
}
});
};
</script>
</head>
<body>
<input type="text" id="pdft" />
<input type="button" name="button" id="pdfa" value="pdf" />
</body>
</html>
(function(window,$,undefined) {
window.SendUtils = {};
window.StringUtils = {};
window.CheckUtils = {};
window.Events = {};
window.Constant = {
projectName:"/test"
};
window.StatusCode = {
OK : 200,
SERVER_FAIL : 500,
CLIENT_FAIL : 400,
VALIDATE_FAIL:300,
USER_NOT_LOGIN:401,
TOKEN_FAIL:402,
BIZ_FAIL:501
};
// ajax 发送
SendUtils.ajax = function(myUrl, myData, sufn) {
$.ajax({
url : myUrl,
data : myData,
cache:false,
type:'post',
dataType : 'json',
success : function(result){
if(result.statusCode == StatusCode.USER_NOT_LOGIN){
alert("会话超时,请重新登录!");
window.location.href = Constant.projectName+"/login.jsp";
}else{
if(sufn)sufn(result);
}
}
});
};
// ajax 同步发送
SendUtils.syncAjax = function(myUrl, myData, sufn) {
$.ajax({
url : myUrl,
data : myData,
type:'post',
dataType : 'json',
cache:false,
async : false,
success : function(result){
if(result.statusCode == 200){
alert("会话超时,请重新登录!");
window.location.href = Constant.projectName+"/login.jsp";
}else{
if(sufn)sufn(result);
}
},
error : function(msg) {
alert("error:" + msg);
}
});
};
/**
*
*/
StringUtils.isBlank = function(str) {
if (str == undefined || str == null || str == '')
return true;
return false;
};
CheckUtils.isEmail = function(str) {
return(str.search(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/) != -1) ;
};
CheckUtils.isMobilePhone = function(str) {
var reg =/^0{0,1}(13[0-9]|15[5-9]|15[0-3]|18[5-9]|18[0])[0-9]{8}$/;
return reg.test(str);
};
SendUtils.ajaxFileUpload = function(ur, fid, sfn, efn) {
$.ajaxFileUpload({
url : ur,
secureuri : false,
fileElementId : fid,
dataType : 'text',
success : function(data, status) { // 服务器响应成功时的处理函数
data = data.substring(data.indexOf('{'), data.lastIndexOf('}') + 1);
var info = eval("(" + data + ")");
sfn(info);
},
error : function(data, status, e) { // 服务器响应失败时的处理函数
efn(data, status, e);
}
});
};
Events.uploadFileFun = function (obj,fn) {
var val = $(obj).val();
var png = val.lastIndexOf(".png") + 4;
var jpg = val.lastIndexOf(".jpg") + 4;
var jpeg = val.lastIndexOf(".jpeg") + 5;
if (png == val.length || jpg == val.length || jpeg == val.length) {
SendUtils.ajaxFileUpload(
'/seafood-admin/upload.action',
'exfile',
function(info) {
if (info.statusCode == StatusCode.SERVER_FAIL) {
alert(info.msg);
window.location.href = Constant.projectName+"/login.jsp";
//if($('#jc-hidden-dialog')) {
// $('#jc-hidden-dialog').dialog('close');
//}
} else if (info.result != null) {
alert(info.result);
} else {
var url = info.msg;
fn(url);
}
},
function(status, e) {
$("#info").html("<i style='color:red'> 导入失败请重试..</i>");
alert(e);
alert(status);
});
} else {
alert('请选择 *.jpg/*.png');
}
$("#fileDiv")
.html('<input type="file" name="file" id="exfile" onchange="Events.uploadFileFun(this,function(url){alert(url)});"/>');
};
Events.uploadVideoFileFun = function (obj,fn) {
var val = $(obj).val();
var mp4 = val.lastIndexOf(".mp4") + 4;
var flv = val.lastIndexOf(".flv") + 4;
if(mp4 == val.length || flv == val.length){
SendUtils.ajaxFileUpload(
'/seafood-admin/upload.action',
'video',
function(info) {
if (info.statusCode == StatusCode.SERVER_FAIL) {
alert(info.msg);
} else if (info.result != null) {
alert(info.result);
} else {
var url = info.msg;
fn(url);
}
},
function(status, e) {
$("#info").html("<i style='color:red'> 导入失败请重试..</i>");
alert(e);
alert(status);
});
} else {
alert('请选择 mp4或flv格式的视频文件');
}
$("#fileDivVideo")
.html('<input type="file" name="file" id="video" onchange="Events.uploadVideoFileFun(this,function(url){alert(url)});"/>');
};
})(window,jQuery);
(function($){
$.fn.serializeJson=function(){
var serializeObj={};
var array=this.serializeArray();
var str=this.serialize();
$(array).each(function(){
if(serializeObj[this.name]){
if($.isArray(serializeObj[this.name])){
serializeObj[this.name].push(this.value);
}else{
serializeObj[this.name]=[serializeObj[this.name],this.value];
}
}else{
serializeObj[this.name]=this.value;
}
});
return serializeObj;
};
})(jQuery);
PdfController.java
package net.spring.controller;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Semaphore;
import net.sf.json.JSONObject;
import org.apache.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
@Controller
public class PdfController {
@RequestMapping(value="pdf",method = RequestMethod.POST)
@ResponseBody
public String pdfRequest(@RequestParam String username) {
String um = username;<span style="color:#ff0000;">//这样就可以得到传入的值</span>
// 第一步:创建一个document对象。
Document document = new Document();
String filePath = null;
try {
// 第二步:
// 创建一个PdfWriter实例,
// 将文件输出流指向一个文件。
// 临时文件路径
String sysTemp = System.getProperty("java.io.tmpdir");
String path = sysTemp + File.separator;
String fileName = "test.pdf";
filePath = path + fileName;
PdfWriter.getInstance(document, new FileOutputStream(filePath));
// 第三步:打开文档。
document.open();
// 第四步:在文档中增加一个段落。
document.add(new Paragraph("Hello World" + "," + "Hello iText"
+ "," + "Hello xDuan"));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// 第五步:关闭文档。
document.close();
// 打开文件
Runtime rt = Runtime.getRuntime();
try {
Process proc = rt.exec("cmd /c start " + filePath.replace(" ", "\" \""));
//等待命令执行结束 获取执行结果
int exitVal = proc.waitFor();
System.out.println("["+exitVal+"]");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject result = new JSONObject();
result.put("statusCode", 100);
return result.toString();
}
}