SVG小工具_JAVA_01

1、打开 svg文件时 自动计算整个文件的 BBox,并设置 <svg/>节点的viewBox属性

2、源码结构:

  2.1、TservletSvg.java (位于目录“项目-->Java Resource -->src-->servletSvg”)

 1 package servletSvg;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 @WebServlet("/TservletSvg")
13 public class TservletSvg extends HttpServlet
14 {
15     private static final long serialVersionUID = 1L;
16 
17     protected void doGet(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
18         //response.getWriter().append("Served at: ").append(request.getContextPath());
19         doPost(_request, _response);
20     }
21 
22     protected void doPost(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException
23     {
24         String strJumpPage = "/SvgShow.jsp";
25         String strFullFilePathName = _request.getParameter("p");
26         strFullFilePathName = new String(strFullFilePathName.getBytes("ISO8859-1"), "utf-8");
27         //System.out.println(strFullFilePathName);
28         
29         if (strFullFilePathName.isEmpty())
30         {
31             _request.setAttribute("FileName", "");
32             _request.setAttribute("FileContent", "");
33             _request.getRequestDispatcher(strJumpPage).forward(_request, _response);
34             return;
35         }
36         
37         String strFileName = GetFileName(strFullFilePathName);
38         String strFileContent = ReadFile(strFullFilePathName, "utf-8");
39         //System.out.println(strFileName);
40         //System.out.println(strFileContent);
41         _request.setAttribute("FileName", strFileName);
42         _request.setAttribute("FileContent", strFileContent);
43         _request.getRequestDispatcher(strJumpPage).forward(_request, _response);
44     }
45 
46     // ZC: 从文件的 完整路径文件名 中,获取文件名
47     String GetFileName(String _strFullFilePathName)
48     {
49         String str;
50         boolean bFound = false;
51         int iPos = _strFullFilePathName.lastIndexOf('\\');
52         if (iPos != -1)
53         {
54             str = _strFullFilePathName.substring(iPos+1, _strFullFilePathName.length());
55             bFound = true;
56         }
57         else
58             str = _strFullFilePathName;
59             
60         iPos = str.lastIndexOf('/');
61         if (iPos != -1)
62         {
63             str = str.substring(iPos+1, str.length());
64             bFound = true;
65         }
66         
67         if (! bFound)
68             return _strFullFilePathName;
69 
70         return str;
71     }
72     
73     // ZC: 从文件的 完整路径文件名 中,读取该文件的内容
74     String ReadFile(String _strFullFilePathName, String _strCharsetName)
75     {
76         try
77         {
78             File file = new File(_strFullFilePathName);
79             FileInputStream in = new FileInputStream(file);
80             // size  为字串的长度 ,这里一次性读完
81             int iSize = in.available();
82             byte[] buffer = new byte[iSize];
83             in.read(buffer);
84             in.close();
85             return (new String(buffer, _strCharsetName));
86             
87         } catch (IOException e) {
88             e.printStackTrace();
89         }
90         return null;
91     }
92 }

  2.2、index.jsp (位于目录“项目-->WebContent”)

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>SelectSVG</title>
 8 </head>
 9 <body>
10 
11 <%
12 String str = request.getContextPath();
13 %>
14 
15     <script type="text/javascript">
16         // ZC: 去掉字符串前后两端的空格
17         function StringTrim(_str)
18         {
19             return _str.replace(/(^\s*)|(\s*$)/g, '');
20         }
21     
22         function BtnClick01()
23         {
24             var idText01 = document.getElementById("idText01");
25             var strFileName = StringTrim(idText01.value);
26             if (! strFileName)
27             {
28                 alert("文件名为空 !");
29                 return;
30             }
31             var strFullFilePathName = "D:\\DRGIS\\Bin\\Graphics\\" + strFileName;
32             window.open("<%=str%>/TservletSvg?p="+strFullFilePathName);
33         }
34         
35         function BtnClick02()
36         {
37             var idText02 = document.getElementById("idText02");
38             var strFileName = StringTrim(idText02.value);
39             if (! strFileName)
40             {
41                 alert("文件名为空 !");
42                 return;
43             }
44             var strFullFilePathName = "D:\\DRGIS\\Bin\\PWGraphics\\" + strFileName;
45             window.open("<%=str%>/TservletSvg?p="+strFullFilePathName);
46         }
47         
48         function BtnClick_ChoseFile()
49         {
50             var idFile = document.getElementById("idFile");
51             var strFileName = StringTrim(idFile.value);
52             if (! strFileName)
53             {
54                 alert("文件名为空 !");
55                 return;
56             }
57             console.log("idFile : "+idFile+" , "+idFile.value);
58             console.log("strFileName : "+strFileName);
59             window.open("<%=str%>/TservletSvg?p="+strFileName);
60         }
61     </script>
62 
63 
64     D:\DRGIS\Bin\Graphics\
65     <input id="idText01" type="text" />
66     <input type="button" value="提交" onclick="BtnClick01()" />
67     <br/><br/>
68     
69     D:\DRGIS\Bin\PWGraphics\
70     <input id="idText02" type="text" />
71     <input type="button" value="提交" onclick="BtnClick02()" />
72     <br/><br/>
73     
74     <input id="idFile" type="file" />
75     <input type="button" value="提交" onclick="BtnClick_ChoseFile()" />
76 </body>
77 </html>

  2.3、SvgShow.jsp (位于目录“项目-->WebContent”)

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 
 6     <head>
 7     <meta charset="UTF-8">
 8     <%
 9     String strFullFilePathName = request.getParameter("p");
10     String strFileName = (String)request.getAttribute("FileName");
11     String strFileContent = (String)request.getAttribute("FileContent");
12     if (strFileName.isEmpty())
13     {
14     %>
15     <title>ErrorSvgFileName</title>
16     <%
17     }else{
18     %>
19     <title><%=strFileName%></title>
20     <%
21     }
22     %>
23     </head>
24 
25 <body>
26     <script type="text/javascript">
27     window.onload = function()
28     {
29         var svg = document.getElementsByTagName("svg")[0];
30         var svgrectBBox = svg.getBBox();
31         console.log("svgrectBBox : "+svgrectBBox.x+", "+svgrectBBox.y+", "+svgrectBBox.width+", "+svgrectBBox.height);
32 //*
33         // ZC: 扩大 viewBox的范围
34         var iExpand = 50;
35         var iWidth = svgrectBBox.width + iExpand * 2;
36         var iHeight = svgrectBBox.height + iExpand * 2;
37         var iX = svgrectBBox.x - iExpand;
38         var iY = svgrectBBox.y - iExpand;
39         svg.setAttribute("viewBox", iX+" "+iY+" "+iWidth+" "+iHeight);
40         svg.setAttribute("width", iWidth+"");
41         svg.setAttribute("height", iHeight+"");
42 //*/
43 /*
44         svg.setAttribute("viewBox", svgrectBBox.x+" "+svgrectBBox.y+" "+svgrectBBox.width+" "+svgrectBBox.height);
45         svg.setAttribute("width", svgrectBBox.width+"");
46         svg.setAttribute("height", svgrectBBox.height+"");
47 //*/
48     };
49     </script>
50 
51     <%
52     if (! strFileName.isEmpty())
53     {
54         out.println(strFileContent);
55     }
56     %>
57 
58 </body>
59 </html>

 

3、生成的项目 路径结构:

  3.1、TservletSvg.class 位于目录“??\WEB-INF\classes\servletSvg”

4、

5、

 

转载于:https://www.cnblogs.com/CodeHouseZ/p/6424759.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值