转自:http://wesker0918.iteye.com/blog/286642和http://www.oschina.net/code/snippet_54371_3138两处
String --> InputStream
- InputStream StringToInputStream(String str){
- ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
- return stream;
- }
InputStream --> String
- String inputStreamToString(InputStream is){
- BufferedReader in = new BufferedReader(new InputStreamReader(is));
- StringBuffer buffer = new StringBuffer();
- String line = "";
- while ((line = in.readLine()) != null){
- buffer.append(line);
- }
- return buffer.toString();
- }
int i = -1; //org.apache.commons.io.output.ByteArrayOutputStream ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((i = is.read()) != -1) { baos.write(i); } String content = baos.toString();int i = -1; byte[] b = new byte[1024]; StringBuffer sb = new StringBuffer(); while ((i = is.read(b)) != -1) { sb.append(new String(b, 0, i)); } String content = sb.toString();