java 字符串转为流





public static InputStream getStringStream(String str) throws Exception{
if (str != null && !str.trim().equals("")) {
ByteArrayInputStream stringStream = new ByteArrayInputStream(str.getBytes());
return stringStream;
}
return null;
}

public static String getStreamString(InputStream tInputStream) throws Exception {
if (tInputStream != null) {
BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(tInputStream));
StringBuffer tStringBuffer = new StringBuffer();
String sTempOneLine = new String("");
while ((sTempOneLine = tBufferedReader.readLine()) != null) {
tStringBuffer.append(sTempOneLine);
}
return tStringBuffer.toString();
}
return null;
}

public static String getStrStream(String key) throws Exception {
StringBuilder sb = new StringBuilder();
OSSClient client = new OSSClient(ENDPOINT, ACCESSKEYID, SECRECTACCESSKEY);
OSSObject ossObject = client.getObject(BUCKET, key);
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
while (true) {
String line = reader.readLine();
if (line == null) break;
sb.append(line);
}
reader.close();
client.shutdown();
return sb.toString();
}

你可能感兴趣的:(java)