代码如下:
//将一个字符串转化为输入流 public static InputStream getStringStream(String sInputString){ if (sInputString != null && !sInputString.trim().equals("")){ try{ ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes()); return tInputStringStream; }catch (Exception ex){ ex.printStackTrace(); } } return null; }
例如,你在做一个文件流解析的时候,别人返回来给你的就是一个字符串格式的报文xml,你就需要这样做:
/**将服务器返回的交易报文xml转换为交易结果对象 * * @author : EX-CHENWEIXIAN001 陈惟鲜 * @create_date :2013-8-2 下午01:40:25 * @param reponseXml * @return * @throws IOException * @throws JDOMException * @throws UnsupportedEncodingException */ public static CmsDealResponseDTO xmlToCmsDealResponseDTO(String reponseXml) throws UnsupportedEncodingException, JDOMException, IOException { if (reponseXml == null) { return null; } CmsDealResponseDTO cmsDealResponseDTO = null; SAXBuilder sax = new SAXBuilder(); Document doc = sax.build( new ByteArrayInputStream(reponseXml.getBytes("UTF-8"))); Element root = doc.getRootElement(); // 根结点 List<Element> chlidrenList = root.getChildren(); cmsDealResponseDTO = setCmsDealResponseDTO(chlidrenList); return cmsDealResponseDTO; }