ajaxfileupload.js 异步上传文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%
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>
<script type="text/javascript" src="${ctxStatic}/jquery/jquery-1.9.1.js"></script>
<script type="text/javascript" src="${ctxStatic }/fileupload/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="${ctxStatic}/jquery/ajaxfileupload.js"></script>
<link href="${ctxStatic}/fileupload/uploadify.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript">



function pp(){

$("#form1").submit();
}



function ajaxFileUpload() {
$.ajaxFileUpload({
url: ‘${ctx}/urge-server/caseCenterCtrl/test1.do‘, //后台接收的url
type: ‘post‘,
secureuri: false, //一般设置为false
fileElementId:‘uploadify‘, // 上传文件的id、name属性名
dataType: ‘text‘, //返回值类型,一般设置为json、application/json
success: function(data, status){
var sta = data.split("###");
$("#q_cvdId").val(sta[1]);//存放临时图片的路径
document.getElementById(‘f1‘).src =sta[0];//展示图片的路径
},
error: function(data, status, e){
alert(e);
}
});
return false;
}






</script>
</head>
<body>
<form id="form1" action="${ctx}/urge-server/caseCenterCtrl/test12.do" method="post" enctype="multipart/form-data">
<input type="file" name="uploadify" id="uploadify" onchange="ajaxFileUpload();" />
<!-- <input type="file" name="uploadify1" id="uploadify1" /> -->
<input type="hidden" id="q_cvdId" name="q_cvdId">
<input type="button" onclick="pp();" value="上传"/>
<img id="f1" src="">
</form>
</body>
</html>

-------------------------------------------------------------------------------


@RequestMapping(value="test1")
public void test1( HttpServletRequest request,HttpServletResponse response) throws Exception{
MultipartFile file = null; //初始化file
response.setCharacterEncoding("UTF-8"); //此方法是解决乱码的
PrintWriter out = response.getWriter();
//获取上传的类型
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for(Map.Entry<String, MultipartFile> entity:fileMap.entrySet()){
file = entity.getValue();
}
String dir = new File(this.getClass().getClassLoader().getResource("/").getPath(), "../..").getCanonicalPath();
String GENERATE_TEMP_DIR = "/zhou";// 临时目录
File CC= new File(dir+GENERATE_TEMP_DIR);
if (!CC.exists()) {//创建文件加判断如果没有则直接创建
CC.mkdirs();
}
SimpleDateFormat timestampDir = new SimpleDateFormat("yyyyMMddHHmmssSSS");//临时时间戳
String date =timestampDir.format(new Date())+".jpg";//给图片重命名防止中文乱码
String pdf_dir=CC+"\\"+date+"";
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
FileOutputStream fos = new FileOutputStream(pdf_dir); // 上传到写死的上传路径
fos.write(bytes); //写入文件
fos.close(); //关闭流
}
String path=request.getRequestURL().substring(0,request.getRequestURL().indexOf("/welcome"))+"/zhou"+"/"+date;//此路径是tomact 下临时图片路径
System.out.println ("name: "+file.getOriginalFilename()+" size: "+file.getSize()+"上传路径=="+path ); // 打印文件大小和文件名称
out.println(path+"###"+pdf_dir);


}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。