zoukankan      html  css  js  c++  java
  • 鼠标右键自定义菜单下载重命名图片

    页面

    <style>
            
                #myMenu{
                list-style: none;
                 150px;
                border: 1px solid #ccc;
                border-bottom: none;
                position: absolute;
                display: none;
                z-index: 999;
           background-color:#fff; } #myMenu li{ border
    -bottom: 1px solid #ccc; padding: 5px 10px; cursor: pointer; } #myMenu li:hover{ background-color: #ccc; } </style>
    <ul id="myMenu">
        <li><a href="/testimg?username=小小&imgpath=图片路径">下载身份证</a></li>
    </ul>
    <script type="text/javascript">
    var myMenu = document.getElementById("myMenu");
    //需要右键事件的元素 document.getElementById(
    "test").addEventListener("contextmenu", function(event){ event.preventDefault(); myMenu.style.display = "block"; //获取鼠标视口位置 myMenu.style.top = event.clientY + "px"; myMenu.style.left = event.clientX + "px"; }); document.addEventListener("click", function(event){ myMenu.style.display = "none"; }); </script>

    java代码

    @GetMapping("/testimg")
         public void downloadImg(String username,
                 String imgpath,
                 HttpServletRequest request,
                 HttpServletResponse response) {
            URL url = null;
            try {
                String filename = dealername+".jpg";
                // path是指欲下载的文件的路径。
                 url = new URL(imgpath);
                 DataInputStream dataInputStream = new DataInputStream(url.openStream());
                // 以流的形式下载文件。
              //  if(!file.exists()) return;
                
                ByteArrayOutputStream output = new ByteArrayOutputStream();
    
                byte[] buffer = new byte[1024];
                int length;
    
                while ((length = dataInputStream.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
                byte[] context=output.toByteArray();
                // 清空response
                response.reset();
                response.setCharacterEncoding("utf-8");
                   String userAgent = request.getHeader("USER-AGENT");
                if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
                  filename = URLEncoder.encode(filename,"UTF8");
                }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
                  filename = new String(filename.getBytes(), "ISO8859-1");
                }else{
                  filename = URLEncoder.encode(filename,"UTF8");//其他浏览器
                }
                // 设置response的Header
                response.addHeader("Content-Disposition", "attachment;filename="+ filename);
             //   response.addHeader("Content-Length", "" + file.length());
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("image/jpeg");
                toClient.write(context);
                toClient.flush();
                toClient.close();
                dataInputStream.close();
                output.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

    --------------------------------------------------------------------------邪恶的分割线------------------------------------------------------------------------------------------------

    凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数

  • 相关阅读:
    工作中搜索页面搜索记录功能的封装(存储到本地)
    工作中遇到的git问题
    Git 常用命令
    帮女票做的报表(用到了angular的一些指令)
    HTML和CSS的知识点
    动画制作 手机APP制作以及响应式的实现
    CSS新内容
    JS中的循环嵌套 BOM函数
    fullpage的使用以及less, Sass的属性和JQuery自定义插件的声明和使用
    Javascript的内容
  • 原文地址:https://www.cnblogs.com/changhai/p/8343446.html
Copyright © 2011-2022 走看看