图片转储及预览

Controller

   PrintWriter printWriter = response.getWriter();
        String fileName = file.getOriginalFilename();

        //获取项目根目录下的某个文件夹
        String uploadpath = ResourceUtils.getURL("classpath:").getPath() + "static/images/partForImgCenter/source" + fileName;
//        System.out.println("uploadpath:"+uploadpath);
        try {
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(uploadpath)));
            stream.write(file.getBytes());
            printWriter.write("123");
            printWriter.close();
            stream.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }

js

 function upload(obj){
        //获取展示图片的区域
        var img = document.getElementById("....");
        //获取文件对象
        var file = obj.files[0];
        //获取文件阅读器
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function(){
            //给img的src设置图片url
            img.setAttribute("src", this.result);
        };
        var formData = new FormData();
        formData.append('file',file);  //添加图片信息的参数
        $.ajax({
            url:"${pageContext.request.contextPath}/ImageService/uploadImg",
            type:"post",
            data:formData,
            dataType:"json",
            async:false,
            cache: false,
            contentType: false, // 告诉jQuery不要去设置Content-Type请求头
            processData: false,// 告诉jQuery不要去处理发送的数据
        });

    }

html

  <form action=""class="imageSmallContent" id="img_a1"  mothod="post">
                 <img src="..." alt="代替input的图片" id="showImg" style="..; absolute;">
                 <input type="file" name="" id="" value="" onchange="upload(this)" style="width: 248px;height: 108px;opacity:0;"/>
             </form>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值