1.页面代码
< tr >
< t d >
图片上传
< /td >
< td colspan="5" align="left" style="text-align: left;" >
< div id="filediv_infvaluelog_pic" >
< /div >
< input type="button" id="file_btn_infvaluelog_add_pic" value="上传" />
< /td >
< /tr >
2.脚本代码
var uploadbutton3 = KindEditor.uploadbutton( {
button : KindEditor('#file_btn_infvaluelog_add_pic'),
fieldName : 'file',
url : 'upload_file_XXX_json?dir=image', //文件上传的action,设置dir为image
afterUpload : function(data) {
if (data.error === 0) {
//正确的时候执行
} else {
//上传错误后,提示
alert(data.message);
}
},
afterError : function(str) {//没正确执行时异常
alert('自定义错误信息: ' + str);
}
});
uploadbutton3.fileBox.change(function(e) {
uploadbutton3.submit();
});
});
3.action方法
public String fileupload() throws FileNotFoundException{
//最大文件大小
long maxSize = 1000000;
InputStream is = null;
if(imgFile!=null&&imgFile.isFile()){
is = new FileInputStream(imgFile);//传过来的文件
}else{
if(file.isFile()){
imgFile=file;
imgFileFileName=fileFileName;
is = new FileInputStream(imgFile);//传过来的文件
}else{
error = 1;
message = "请选择要上传的文件。";
}
}
HttpServletRequest request = ServletActionContext.getRequest();
String savePath = null;
String saveUrl = null;
//检查目录
File rootDir = new File(savePath);
if(!rootDir.isDirectory()){
error = 1;
message = "上传根目录不存在。";
}
//检查目录写权限
if(!rootDir.canWrite()){
error = 1;
message = "上传根目录没有写权限。";
}
String dirName = null;
if (dir == null) {
dirName = "other";
}else{
dirName = dir;
}
//创建文件夹
savePath += "/"+dirName + "/";
saveUrl += "/"+dirName + "/";
File saveDirFile = new File(savePath);
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String ymd = sdf.format(new Date());
savePath += ymd + "/";
saveUrl += ymd + "/";
File dirFile = new File(savePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
//检查文件大小
if(imgFile.length() > maxSize){
error = 1;
message = "上传文件大小超过限制。";
}else{
//定义允许上传的文件扩展名
HashMap extMap = new HashMap();
extMap.put("image", "gif,jpg,jpeg,png,bmp");
extMap.put("flash", "swf,flv");
extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
extMap.put("file","gif,jpg,jpeg,png,bmp,doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
//检查扩展名
String fileExt = imgFileFileName.substring(imgFileFileName.lastIndexOf(".") + 1).toLowerCase();
if(!Arrays.asList(extMap.get(dirName).split(",")).contains(fileExt)){
error = 1;
message = "上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。";
}else{
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "_";
try{
if(imgFileFileName.indexOf(",")!=-1){
imgFileFileName=imgFileFileName.replaceAll(",", "");
}
File deskFile = new File(savePath+newFileName+imgFileFileName);
OutputStream os = new FileOutputStream(deskFile);
byte[] bytefer = new byte[1024];
int length = 0;
while ((length = is.read(bytefer)) != -1) {
os.write(bytefer, 0, length);
}
os.close();
is.close();
error = 0;
//message = "上传文件成功";
//url = urlRoot + saveUrl + newFileName + imgFileFileName;
if("file".equals(dir)){
url = savePath + newFileName + imgFileFileName;
}else{
url = saveUrl + newFileName + imgFileFileName;
}
url = url.trim().replaceAll("\\\\\\\\", "/");
url = url.replaceAll("\\\\", "/");
//检查是否是图片,是才进行压缩
if(url.endsWith(".jpg")||url.endsWith(".jpeg")||url.endsWith(".png")||url.endsWith(".bmp")||url.endsWith(".gif")){
//压缩
ImgProce ip = new ImgProce();
ip.setWideth(400);
ip.proce(savePath+newFileName+imgFileFileName, "400");
}
}catch(Exception e){
error = 1;
message = "上传文件失败。";
}
}
}
this.map=new HashMap();
this.map.put("error", error);
if(error==0){
this.map.put("url", url);
this.map.put("filename",imgFileFileName);
}else{
this.map.put("message", message);
}
return "SUCCESS";
}