鼠标右键自定义菜单下载重命名图片
页面
<style> #myMenu{ list-style: none; width: 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(); } }
--------------------------------------------------------------------------邪恶的分割线------------------------------------------------------------------------------------------------
凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数