springmvc webuploader 附件上传

页面
<!--引入CSS-->
<link rel="stylesheet" type="text/css" href="${ctx}scripts/webuploader-0.1.5/webuploader.css">
<!--引入JS-->
<script type="text/javascript" src="${ctx}scripts/webuploader-0.1.5/webuploader.js"></script>

<div id="uploader-demo">
<div id="filePicker">选择图片</div>
</div>
<div id="fileList" class="uploader-list"></div>
<script type="text/javascript">
var uploader = WebUploader.create({
    auto: true,
    swf: '${ctx}scripts/webuploader-0.1.5/Uploader.swf',//对应引入的文件
    server: '${ctx}/article/uploadFile',//对应你的后台接收方法
    pick: {
        id: '#filePicker',
        multiple:false, 
        label: '点击选择图片'
    },
fileNumLimit: 1,
    accept: {
        title: 'Images',
        extensions: 'gif,jpg,jpeg,bmp,png',
        mimeTypes: 'image/*'
    }
});
uploader.reset();
//生成缩略图和上传进度
uploader.on("fileQueued", function(file) {
        $("#fileList").append("<div id='" + file.id + "'><img/><div><span class='percentage'><span></div></div>")
        uploader.makeThumb(file, function(error, src) {
            if (error) {
                $("#" + file.id).find("img").replaceWith("<span>无法预览&nbsp;</span>");
            } else {
                $("#" + file.id).find("img").attr("src", src);
            }
        });
    }
);
uploader.on( 'uploadSuccess', function( file , response) {
	var imgurl = response._raw; //上传图片的路径
    $('#picpath').val(imgurl);
});
uploader.on('click', '.remove-this', function(file) {
    uploader.removeFile( file );
})
</script >
java后台
//上传文件
		@RequestMapping(value = "/uploadFile")
		@ResponseBody
		public String uploadFile(@RequestParam("file") MultipartFile attach, HttpServletRequest request){
			String basepath ="E:/1111/"; //request.getSession().getServletContext().getRealPath("/");
			String functionPath = request.getParameter("functionPath");
			if(functionPath == null || "".equals(functionPath)) {
				functionPath = "other";
			}
			
			String name = attach.getOriginalFilename();
			String type = name.substring(name.lastIndexOf(".") + 1);
			if (!FileUploadUtil.checkFileName(type, FileUploadUtil.IMG_SUFFIX)) {
				return "error";
			}
			
			String replyPath = FileUploadUtil.uploadImg(attach, functionPath, basepath);
			return replyPath;
		}
/**
	 * 上传图片
	 * @param attach MultipartFile类型图片
	 * @param functionPath 功能路径
	 * @return error:出异常;图片保存后相对路径
	 */
	public static String uploadImg(MultipartFile attach,String functionPath,String basepath){
		
		String nowPath=FormatDate.toShortFormat(new Date());
		String path = basepath + "upload/img/"+ functionPath+"/"+nowPath  +"/";
		
		 File dicimg = new File(path);
	     if (!dicimg.exists()) {
	         dicimg.mkdirs();
	     }
		
		String name = attach.getOriginalFilename();
        String type = name.substring(name.lastIndexOf(".") + 1);
        

        String now=System.nanoTime()+"";
        String newName=now+"."+type;
        
        String filePath = path + newName;
		
		FileOutputStream write = null;
			try {
					write = new FileOutputStream(new File(filePath));
				  	write.write(attach.getBytes());
			        write.flush();			        
			        return "/upload/img/" + functionPath +"/"+ nowPath  +"/" + newName;
			} catch (Exception e) { 
				e.printStackTrace();
				return "error";
			} finally{
				try {
					if(write !=null){
						write.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值