uploadify 简单使用

页面代码:

<img id="imgPrew"  src="../images/toTraveller/pic1.jpg" class="avatar-mickey">
<input type="file" name="file_upload" id="file_upload"/>

<input type="button" onclick="javascript:$('#file_upload').uploadify('upload','*')" value="上传"/>
<input type="button" onclick="javascript:$('#file_upload').uploadify('cancel','*')" value="停止上传"/>

<script type="text/javascript">
	$(function(){
		$('#file_upload').uploadify({
			'swf':'uploadify/uploadify.swf',//(flash上传插件),uploadify.css(上传样式表)
			'uploader':'/traveller/uploadImg',
			'fileObjName':'file_upload',
			'buttonText':"选择图片",
			//选择文件后自动上传
			'auto':true,
			'onUploadSuccess':function(file,data,response){
				$("#imgPrew").attr("src",data);
			},
			'onUploadError':function(file,errorCode,errorMsg,errorString){
				alert("上传失败");
			},
			//检测FLASH失败调用
			'onFallback':function(){
				alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。");
			}
		});
	});
</script>

Controller代码:

 /*上传头像*/
    @RequestMapping(value = "/uploadImg",method = RequestMethod.POST)
    @ResponseBody
    public String uploadImg(@RequestParam("file_upload") CommonsMultipartFile file_upload, HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println("=====上传图片。。。");
        //上传文件信息
        String fileName = file_upload.getOriginalFilename();
        System.out.println("上传文件名:"+fileName);
        String fileType = fileName.split("[.]")[1];

        //设置文件保存的本地路径
        ServletContext servletContext=request.getSession().getServletContext();
        String realPath=servletContext.getRealPath("images/upload");
        System.out.println("文件上传路径:"+realPath);

        File file = new File(realPath);
        if(!file.exists()){
            file.mkdirs();
        }

        //显示文件在磁盘的所有信息
        DiskFileItem fi = (DiskFileItem)file_upload.getFileItem();
        System.out.println("文件的磁盘信息:"+fi);

        //存到tomcat中,生成临时文件
        //File result = fi.getStoreLocation();
        //System.out.println(result);
        //System.out.println(result.getPath());

        //重命名图片
        String lastName = UUID.randomUUID().toString()+"."+fileType;
        System.out.println(lastName);
        //保存文件————具体路径,包括图片全名
        File f = new File(realPath,lastName);
        System.out.println(f);
        //将文件保存到服务器
        FileCopyUtils.copy(file_upload.getBytes(),f);
        System.out.println("上传成功!");
        //返回页面上传文件的路径
        return "images/upload/"+lastName;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值