java 实现 图片上传

        //存放上传图片的服务器地址

        List<String> imgList = null;


        if (ServletFileUpload.isMultipartContent(request)) {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setSizeThreshold(1024 * 512);
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            fileUpload.setFileSizeMax(10 * 1024 * 1024);
            String newName = null;
            List<FileItem> items = fileUpload.parseRequest(request);
            imgList = new ArrayList<String>();
            for (FileItem item : items) {
                if (!item.isFormField()) {
                    String image = null;
                    String name = item.getName();
                    String extName = name.substring(name.lastIndexOf("."));
                    newName = UUID.randomUUID().toString();
                    item.write(new File(request.getRealPath("/upload/" + newName + extName)));
                    image = "/upload/" + newName + extName;
                    imgList.add(image);
                }
            }

        }



 //jquery 实现上传图片并显示

 function PreviewImage(imgFile, showId) {
                var pattern = /(\.*.jpg$)|(\.*.png$)|(\.*.jpeg$)|(\.*.gif$)|(\.*.bmp$)/;
                var show = $(showId);
                var path;
                if (!pattern.test(imgFile.value))
                {
                    alert("仅支持jpg/jpeg/png/gif/bmp格式的照片!");
                    imgFile.focus();
                }
                else {
                    if (/msie/.test(navigator.userAgent.toLowerCase())) {
                        imgFile.select();
                        path = document.selection.createRange().text;
                        show.html = "";
                        show.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + path + "\")");
                    } else {
                        path = URL.createObjectURL(imgFile.files[0]);
                        show.html("<img src='" + path + "'/>");
                    }
                }
            }

   

    <input  type="file" name="front_image" class="upload" id="uoload_front" οnchange='PreviewImage(this, "#front_image")'/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值