一、FileInput 上传插件下载地址:
http://www.jq22.com/yanshi4099
二、 初始化设置:
$("#fuploadinput").fileinput({
uploadAsync:true,//默认异步上传
previewFileType:"pdf",
maxFileCount: 2,
maxFileSize: 5120,
maxFilePreviewSize: 25600,// 25 MB
msgFilesTooMany:"选择的上传文件个数 ({n}) 超出最大文件的限制个数 {m}. 请重新上传!",
dropZoneTitle:"拖拽文件到这里或者点击选择上传 …",
uploadExtraData:function() {
//注意:一般只支持js,
varfileuploadtitleval = document.getElementById("fileuploadtitleid").value;
if(fileuploadtitleval =="")
{
alert(11);
}else{
return{
'fileuploadtitle':fileuploadtitleval,
};
}
},
});
//异步上传成功结果处理
$("#fuploadinput").on("fileuploaded",function(event, data, previewId, index) {
});
//异步上传错误结果处理
$("#fuploadinput").on('fileerror',function(event, data, msg) {
});
三、 Options 中文文档说明:
http://blog.csdn.net/u012526194/article/details/69937741
四:java 配置
①:FileuploadController
@Resource
FileuploadOperatorfileuploadOperator;
/**
* 上传文件(保存文件以及保存相对路径入数据库)
*@paramreq
*@paramrep
*@throwsException
*/
@RequestMapping("uploadFile")
publicvoiduploadFile(HttpServletRequestreq, HttpServletResponserep)throwsException {
req.setCharacterEncoding("utf-8");
rep.setContentType("text/html;charset=utf-8");
rep.setHeader("Cache-Control","no-cache");
HttpSessionhttpSession=req.getSession();
Stringuserid=httpSession.getAttribute("userid").toString();
Mapusermso=newHashMap<>();
usermso.put("userid",userid);
Mapmap=fileuploadOperator.queryUser(usermso);
Stringfileuploadtitle=req.getParameter("fileuploadtitle");
Mapumso=newHashMap();
umso.put("fileuploadname",map.get("nickname"));
umso.put("fileuploadtitle",fileuploadtitle);
try{
Stringfilerealpath="";
StringBufferurl=req.getRequestURL();
StringtempContextUrl=url.delete(url.length() -req.getRequestURI().length(),url.length()).append("/").toString();
System.out.println(tempContextUrl);
MultipartHttpServletRequestmultipartRequest= (MultipartHttpServletRequest)req;
MultipartFilefile1=multipartRequest.getFile("file_data");
Stringfile_ture_name=file1.getOriginalFilename();
Stringfiletype=file1.getContentType();
System.out.println(filetype);
filerealpath=req.getSession().getServletContext().getRealPath("/uploadfiles/pdf/temp/") +file_ture_name;
if(filetype.equals("application/pdf")) {
fileuploadOperator.saveFile(filerealpath,file1.getBytes());
Stringfilepath=tempContextUrl+"qas/uploadfiles/pdf/temp/"+file_ture_name;
Stringmsg=fileuploadOperator.uploadFile(umso,filepath);
Mapmp=newHashMap();
mp.put("msg",msg);
JSONObjectjsonobj= JSONObject.fromObject(mp);
Stringjson=jsonobj.toString();
rep.getWriter().print(json);
}else{
thrownewException();
}
}catch(Exceptione) {
e.printStackTrace();
Mapmp=newHashMap();
mp.put("msg","上传失败!格式不正确,只允许上传pdf! ");
JSONObjectjsonobj= JSONObject.fromObject(mp);
Stringjson=jsonobj.toString();
rep.getWriter().print(json);
}
}
② FileuploadOperator
// 保存pdf的方法
publicvoidsaveFile(StringfilePath,byte[]content)throwsException {
BufferedOutputStreambos=null;
try{
Filefile=newFile(filePath);
// 判断文件路径是否存在
if(!file.getParentFile().exists()) {
//文件路径不存在时,创建保存文件所需要的路径
file.getParentFile().mkdirs();
}
/*if (file.exists()) {
throw new Exception("已存在名称相同的文件,请修改文件名重新上传!");
}*/
// 创建文件(这是个空文件,用来写入上传过来的文件的内容)
file.createNewFile();
bos=newBufferedOutputStream(newFileOutputStream(file));
bos.write(content);
bos.flush();
}catch(FileNotFoundExceptione) {
thrownewFileNotFoundException("pdf文件不存在");
}finally{
if(null!=bos) {
bos.close();
}
}
}