- public class FeatureXmlExportServlet extends HttpPostServlet {
- /**
- *
- */
- private static final long serialVersionUID = -6732796071756836554L;
- @Override
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- this.doPost(request, response);
- }
- @Override
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- try {
- request.setCharacterEncoding("UTF-8");
- response.setCharacterEncoding("UTF-8");
- String version = request.getParameter("version");
- String tdt = request.getParameter("tdtname");
- tdt = new String(tdt.getBytes("UTF-8"), "UTF-8");
- String tdtName = this.getTDTValue(tdt);
- String fileName = "character";
- // 设置导出文件参数
- fileName = new String(fileName.getBytes("UTF-8"), "UTF-8");
- // 文件部署
- response.setHeader("Content-disposition",
- "attachment; filename=" + fileName + ".xml");
- // 文件头
- response.setHeader("Pragma", "public");
- // 文件缓存控制,必须刷新
- response.setHeader("Cache-Control", "must-revalidate");
- // 失效期
- response.setDateHeader("Expires", 0);
- // 单元样式
- response.setContentType("text/csv; charset=UTF-8");
- // 获得WEB-INF路径
- // String sb =Utility.getWebInfPath().substring(0,44);
- // String webInfPath = sb+"\\temp";
- String webInfPath = Utility.getWebInfPath() ;
- File file = new File(webInfPath + File.separator + tdt + "-"
- + version);
- if (file.exists()) {
- Document doc = getXmlDocument(file.getPath());
- doc.write(response.getWriter());
- } else {
- SelfDefineImportExport ie = new SelfDefineImportExport();
- Document doc = DocumentHelper.createDocument();
- // 获得模板对象
- ie.creatXML(doc, version, tdtName);
- // 导出
- save(doc,file.getPath());
- doc.write(response.getWriter());
- }
- //特性接口访问统计
- ToolSpyLogger.init("iFree");
- ToolSpyLogger.log("E3-View","特性树Web服务接口");
- ToolSpyLogger.release();
- response.getWriter().flush();
- } catch (Exception e) {
- e.printStackTrace();
- this.doResponse(response, new GeneralHttpResponse(
- ResponseResult.FAILURE, "没有找到相关内容"));
- }
- }
- public static Document getXmlDocument(String fileName)
- {
- Document document = null;
- try {
- String xml = readTxt(fileName);
- document = DocumentHelper.parseText(xml);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return document;
- }
- public static String readTxt(String txtPath) {
- String content = " ";
- try {
- InputStream is = new FileInputStream( new File(txtPath) );
- BufferedReader buffReader = new BufferedReader(new InputStreamReader(is,"utf-8"));
- content = buffReader.readLine();
- String line;
- try
- {
- while ((line = buffReader.readLine()) != null)
- {
- if (line.equals(""))
- continue;
- else
- content += line + "\r\n";
- }
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("Error reading file");
- }
- buffReader.close();
- is.close();
- } catch (FileNotFoundException e) {
- System.out.println("file is not exist,can't read...");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return content;
- }
- public static void save(Document document,String fileName)
- {
- try
- {
- FileOutputStream mOutputStream = new FileOutputStream(fileName);
- OutputFormat fmt = new OutputFormat(document.asXML());
- fmt.setTrimText(true);
- fmt.setPadText(true);
- fmt.setIndent(true);
- fmt.setIndentSize(0);
- fmt.setNewlines(true);
- fmt.setEncoding("utf-8");
- XMLWriter writer = new XMLWriter( mOutputStream, fmt );
- writer.write(document);
- mOutputStream.flush();
- mOutputStream.close();
- }catch (IOException e)
- {
- e.printStackTrace();
- }
- catch( Exception e )
- {
- e.printStackTrace();
- }
- }
- @SuppressWarnings("deprecation")
- private String getTDTValue(String tdt){
- Connection con = null;
- Statement state = null;
- String tdtKey=null;
- Session session = HibernateSessionFactory.getSession();
- try {
- con = session.connection();
- con.setAutoCommit(true);
- session.beginTransaction();
- //根据TDT获取对应的Value
- String sq="select dict_value from e3_dict_table where dict_describe='" + tdt + "' and dict_type_id = 'FEATURE_TDT'" ;
- state = con.createStatement();
- ResultSet result = state.executeQuery(sq);
- while(result.next()){
- tdtKey=result.getString("DICT_VALUE");
- }
- con.commit();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- try {
- if(state != null)
- state.close();
- if(con != null)
- HibernateSessionFactory.closeSession();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return tdtKey;
- }
- }public class HttpFeatureTree {
- /**
- * 以URL方式发送数据
- *
- * @param urlStr
- * 发送地址
- * @param contentStr
- * 发送内容
- * @param charset
- * 发送字符集
- * @param sResult
- * 返回数据Buffer
- * @return boolean 发送是否成功
- */
- public boolean sendStrOfPost(String urlStr, String contentStr, String charset, StringBuffer sResult) {
- boolean bResult = false;
- String charsetName = charset;
- URL url = null;
- HttpURLConnection httpConnection = null;
- InputStream httpIS = null;
- java.io.BufferedReader http_reader = null;
- try {
- url = new URL(urlStr);
- httpConnection = (HttpURLConnection) url.openConnection();
- // 设置连接主机超时(单位:毫秒)
- httpConnection.setConnectTimeout(10000);
- // 设置从主机读取数据超时(单位:毫秒)
- httpConnection.setReadTimeout(10000);
- httpConnection.setRequestMethod("POST"); // POST方式提交数据
- httpConnection.setDoOutput(true);
- httpConnection.setRequestProperty("Content-Length", String.valueOf(contentStr.getBytes().length));
- PrintWriter out = null;
- out = new PrintWriter(new OutputStreamWriter(httpConnection.getOutputStream(), charsetName));// 此处改动
- // 发送请求
- out.print(contentStr);
- out.flush();
- out.close();
- int responseCode = httpConnection.getResponseCode();
- if (responseCode == HttpURLConnection.HTTP_OK) {
- // 发送正常
- bResult = true;
- // 读取数据
- httpIS = httpConnection.getInputStream();
- http_reader = new java.io.BufferedReader(new java.io.InputStreamReader(httpIS, charsetName));
- String line = null;
- while ((line = http_reader.readLine()) != null) {
- if (sResult.length() > 0) {
- sResult.append("\n");
- }
- sResult.append(line);
- }
- //logger.info("[URL][response][success]" + sResult);
- } else {
- //logger.info("[URL][response][failure][code : " + responseCode + " ]");
- }
- } catch (IOException e) {
- //logger.error("[HttpUtil]sendStrOfPost error", e);
- }
- finally {
- try {
- if (http_reader != null)
- http_reader.close();
- if (httpIS != null)
- httpIS.close();
- if (httpConnection != null)
- httpConnection.disconnect();
- } catch (IOException e) {
- //logger.error("[HttpUtil]finally error", e);
- }
- }
- return bResult;
- }
- public static void main(String[] args)
- {
- HttpFeatureTree http = new HttpFeatureTree();
- StringBuffer sResult = new StringBuffer();
- String str ="http://e3/GetFeatureTDTServlet?version=U2000V1R7C00";
- str = str.replaceAll(" ", "%20");
- http.sendStrOfPost(str, "", "utf-8", sResult);
- String s=sResult.toString();
- System.out.print(s);
- }
- }
服务接口
最新推荐文章于 2023-12-29 20:45:00 发布