服务接口

Java代码   收藏代码
  1. public class FeatureXmlExportServlet extends HttpPostServlet {  
  2.     /** 
  3.      *  
  4.      */  
  5.     private static final long serialVersionUID = -6732796071756836554L;  
  6.   
  7.     @Override  
  8.     protected void doGet(HttpServletRequest request,  
  9.             HttpServletResponse response) throws ServletException, IOException {  
  10.         this.doPost(request, response);  
  11.     }  
  12.   
  13.     @Override  
  14.     protected void doPost(HttpServletRequest request,  
  15.             HttpServletResponse response) throws ServletException, IOException {  
  16.   
  17.         try {  
  18.             request.setCharacterEncoding("UTF-8");  
  19.             response.setCharacterEncoding("UTF-8");  
  20.             String version = request.getParameter("version");  
  21.             String tdt = request.getParameter("tdtname");  
  22.             tdt = new String(tdt.getBytes("UTF-8"), "UTF-8");  
  23.             String tdtName = this.getTDTValue(tdt);  
  24.   
  25.             String fileName = "character";  
  26.             // 设置导出文件参数  
  27.             fileName = new String(fileName.getBytes("UTF-8"), "UTF-8");  
  28.             // 文件部署  
  29.             response.setHeader("Content-disposition",  
  30.                     "attachment; filename=" + fileName + ".xml");  
  31.             // 文件头  
  32.             response.setHeader("Pragma""public");  
  33.             // 文件缓存控制,必须刷新  
  34.             response.setHeader("Cache-Control""must-revalidate");  
  35.             // 失效期  
  36.             response.setDateHeader("Expires"0);  
  37.             // 单元样式  
  38.             response.setContentType("text/csv; charset=UTF-8");  
  39.               
  40.             // 获得WEB-INF路径  
  41.               
  42. //          String sb =Utility.getWebInfPath().substring(0,44);  
  43. //          String webInfPath = sb+"\\temp";  
  44.             String webInfPath = Utility.getWebInfPath() ;  
  45.             File file = new File(webInfPath + File.separator + tdt + "-"  
  46.                     + version);  
  47.             if (file.exists()) {  
  48.                 Document doc = getXmlDocument(file.getPath());  
  49.                 doc.write(response.getWriter());  
  50.             } else {  
  51.                 SelfDefineImportExport ie = new SelfDefineImportExport();  
  52.                 Document doc = DocumentHelper.createDocument();  
  53.                 // 获得模板对象  
  54.                 ie.creatXML(doc, version, tdtName);  
  55.                 // 导出  
  56.                 save(doc,file.getPath());  
  57.                 doc.write(response.getWriter());  
  58.             }  
  59.             //特性接口访问统计  
  60.             ToolSpyLogger.init("iFree");  
  61.             ToolSpyLogger.log("E3-View","特性树Web服务接口");  
  62.             ToolSpyLogger.release();  
  63.             response.getWriter().flush();  
  64.         } catch (Exception e) {  
  65.             e.printStackTrace();  
  66.             this.doResponse(response, new GeneralHttpResponse(  
  67.                     ResponseResult.FAILURE, "没有找到相关内容"));  
  68.         }  
  69.   
  70.     }  
  71.       
  72.     public static Document getXmlDocument(String fileName)  
  73.     {  
  74.         Document document = null;  
  75.         try {  
  76.             String xml = readTxt(fileName);  
  77.             document = DocumentHelper.parseText(xml);  
  78.         } catch (Exception e) {  
  79.             e.printStackTrace();  
  80.         }  
  81.         return document;  
  82.     }  
  83.       
  84.     public static String readTxt(String txtPath) {  
  85.         String content = " ";  
  86.         try {  
  87.             InputStream is = new FileInputStream( new File(txtPath) );  
  88.             BufferedReader buffReader = new BufferedReader(new InputStreamReader(is,"utf-8"));  
  89.             content = buffReader.readLine();  
  90.             String line;  
  91.             try   
  92.             {  
  93.                 while ((line = buffReader.readLine()) != null)   
  94.                 {  
  95.                     if (line.equals(""))  
  96.                         continue;  
  97.                     else  
  98.                         content += line + "\r\n";  
  99.                 }  
  100.             } catch (IOException e) {  
  101.                 e.printStackTrace();  
  102.                 System.out.println("Error reading file");  
  103.             }  
  104.             buffReader.close();  
  105.             is.close();  
  106.         } catch (FileNotFoundException e) {  
  107.             System.out.println("file is not exist,can't read...");  
  108.         } catch (IOException e) {  
  109.             e.printStackTrace();  
  110.         }  
  111.         return content;  
  112.     }  
  113.   
  114.       
  115.     public static void save(Document document,String fileName)   
  116.     {       
  117.         try  
  118.         {  
  119.             FileOutputStream mOutputStream = new FileOutputStream(fileName);   
  120.             OutputFormat fmt = new OutputFormat(document.asXML());  
  121.             fmt.setTrimText(true);  
  122.             fmt.setPadText(true);  
  123.             fmt.setIndent(true);  
  124.             fmt.setIndentSize(0);  
  125.             fmt.setNewlines(true);  
  126.             fmt.setEncoding("utf-8");  
  127.               
  128.             XMLWriter writer = new XMLWriter( mOutputStream, fmt );  
  129.             writer.write(document);  
  130.             mOutputStream.flush();  
  131.             mOutputStream.close();              
  132.         }catch (IOException e)  
  133.         {  
  134.             e.printStackTrace();  
  135.         }  
  136.         catch( Exception e )  
  137.         {  
  138.             e.printStackTrace();  
  139.         }  
  140.     }  
  141.       @SuppressWarnings("deprecation")  
  142.         private String getTDTValue(String tdt){  
  143.             Connection con = null;  
  144.             Statement state = null;  
  145.             String tdtKey=null;  
  146.             Session session = HibernateSessionFactory.getSession();  
  147.             try {  
  148.                 con = session.connection();  
  149.                 con.setAutoCommit(true);  
  150.                 session.beginTransaction();  
  151.                 //根据TDT获取对应的Value  
  152.                 String sq="select dict_value from e3_dict_table where  dict_describe='" + tdt + "' and  dict_type_id = 'FEATURE_TDT'" ;  
  153.                 state = con.createStatement();  
  154.                 ResultSet result = state.executeQuery(sq);  
  155.                 while(result.next()){  
  156.                 tdtKey=result.getString("DICT_VALUE");  
  157.                 }  
  158.                 con.commit();  
  159.             } catch (Exception e) {  
  160.                 e.printStackTrace();  
  161.             }finally{  
  162.                 try {   
  163.                     if(state != null)  
  164.                         state.close();  
  165.                     if(con != null)  
  166.                         HibernateSessionFactory.closeSession();  
  167.                 } catch (Exception e) {  
  168.                     e.printStackTrace();  
  169.                 }  
  170.             }       
  171.               return tdtKey;  
  172.           }  
  173.   
  174. }public class HttpFeatureTree {  
  175.       
  176.     /** 
  177.      * 以URL方式发送数据 
  178.      *  
  179.      * @param urlStr 
  180.      *            发送地址 
  181.      * @param contentStr 
  182.      *            发送内容 
  183.      * @param charset 
  184.      *            发送字符集 
  185.      * @param sResult 
  186.      *            返回数据Buffer 
  187.      * @return boolean 发送是否成功 
  188.      */  
  189.     public boolean sendStrOfPost(String urlStr, String contentStr, String charset, StringBuffer sResult) {  
  190.         boolean bResult = false;  
  191.         String charsetName = charset;  
  192.         URL url = null;  
  193.         HttpURLConnection httpConnection = null;  
  194.         InputStream httpIS = null;  
  195.         java.io.BufferedReader http_reader = null;  
  196.         try {  
  197.             url = new URL(urlStr);  
  198.             httpConnection = (HttpURLConnection) url.openConnection();  
  199.   
  200.             // 设置连接主机超时(单位:毫秒)  
  201.             httpConnection.setConnectTimeout(10000);  
  202.             // 设置从主机读取数据超时(单位:毫秒)  
  203.             httpConnection.setReadTimeout(10000);  
  204.   
  205.             httpConnection.setRequestMethod("POST"); // POST方式提交数据  
  206.             httpConnection.setDoOutput(true);  
  207.             httpConnection.setRequestProperty("Content-Length", String.valueOf(contentStr.getBytes().length));  
  208.             PrintWriter out = null;  
  209.             out = new PrintWriter(new OutputStreamWriter(httpConnection.getOutputStream(), charsetName));// 此处改动  
  210.             // 发送请求  
  211.             out.print(contentStr);  
  212.             out.flush();  
  213.             out.close();  
  214.             int responseCode = httpConnection.getResponseCode();  
  215.             if (responseCode == HttpURLConnection.HTTP_OK) {  
  216.                 // 发送正常  
  217.                 bResult = true;  
  218.   
  219.                 // 读取数据  
  220.                 httpIS = httpConnection.getInputStream();  
  221.                 http_reader = new java.io.BufferedReader(new java.io.InputStreamReader(httpIS, charsetName));  
  222.                 String line = null;  
  223.                 while ((line = http_reader.readLine()) != null) {  
  224.                     if (sResult.length() > 0) {  
  225.                         sResult.append("\n");  
  226.                     }  
  227.                     sResult.append(line);  
  228.                 }  
  229.                 //logger.info("[URL][response][success]" + sResult);  
  230.             } else {  
  231.                 //logger.info("[URL][response][failure][code : " + responseCode + " ]");  
  232.             }  
  233.         } catch (IOException e) {  
  234.             //logger.error("[HttpUtil]sendStrOfPost error", e);  
  235.         }  
  236.         finally {  
  237.             try {  
  238.                 if (http_reader != null)  
  239.                     http_reader.close();  
  240.                 if (httpIS != null)  
  241.                     httpIS.close();  
  242.                 if (httpConnection != null)  
  243.                     httpConnection.disconnect();  
  244.             } catch (IOException e) {  
  245.                 //logger.error("[HttpUtil]finally error", e);  
  246.             }  
  247.         }  
  248.   
  249.         return bResult;  
  250.     }  
  251.   
  252.     public static void main(String[] args)   
  253.     {  
  254.         HttpFeatureTree http = new HttpFeatureTree();  
  255.         StringBuffer sResult = new StringBuffer();  
  256.         String str ="http://e3/GetFeatureTDTServlet?version=U2000V1R7C00";  
  257.         str = str.replaceAll(" ""%20");  
  258.         http.sendStrOfPost(str, """utf-8", sResult);  
  259.           
  260.         String s=sResult.toString();  
  261.         System.out.print(s);  
  262.           
  263.     }  
  264. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值