html篇
var el=null;
var up = $('#upload').Huploadify({
auto:true,
fileTypeExts:'*.*',
multi:false,
showUploadedPercent:true,
showUploadedSize:true,
buttonText: '上传资料',
uploader:'uploadImage',
onUploadStart:function(file){
console.log(file.name+'开始上传');
el = $.loading({
content:'上传中...'
});
},
onUploadComplete:function(file,data){
//console.log(file.name+'上传完成');
//alert("file.name"+file.name);
//alert("date"+date);
var obj = JSON.parse(data);
$('[name="shop.shop_image"]').val(obj.path);
el.loading("hide");
}
});
JAVA篇:
public void uploadImage() {
//获取上传图片 上传
UploadFile uploadFile=this.getFile();
String fileName=uploadFile.getOriginalFileName();
String delImgName = fileName;
//获取上传文件
//UploadFile uf = getFile("Filedata", "mobile/setshop",DictUtil.maxImagSize,DictUtil.encoding);
//获取上传文件的类型 如 .jpg .png.....
String imageType=fileName.substring(uploadFile.getFileName().lastIndexOf("."),uploadFile.getFileName().length());
String imageName=System.currentTimeMillis()+imageType;
String rootPath =this.getSession().getServletContext().getRealPath("/upload/");
//拼接重新生成的图片名字
String file =rootPath+"/"+imageName;
//重命名
uploadFile.getFile().renameTo(new File(file));
ServiceHandler.delFile(rootPath, delImgName);
JSONObject urlJson = new JSONObject();
urlJson.put("imgName", imageName);
urlJson.put("path", file.replace(file, this.getCxt().concat("/upload/".concat(imageName))));
this.setAttr("shop_image", file.replace(file, this.getCxt().concat("/upload/".concat(imageName))));
renderJson(urlJson.toJSONString());
}