반응형
java.io.InputStream 객체가있는 경우 해당 개체를 처리하고 문자열 을 생성해야합니까?
텍스트 데이터가 포함 된 inputstream 가있는 string 을 문자열 로 변환하려고합니다.
inputstream 을 사용하여 string 로 변환하는 가장 쉬운 방법은 무엇입니까?
public String convertStreamToString(InputStream is) {
// ???
}
해결 방법
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();
또는
// NB: does not close inputStream, you'll have to use try-with-resources for that
String theString = IOUtils.toString(inputStream, encoding);
또는 스트림 및 작가를 혼합하고 싶지 않은 경우 bytearrayoutputstream 를 사용할 수 있습니다.
참조 페이지 https://stackoverflow.com/questions/309424
반응형
'자바' 카테고리의 다른 글
| JPanel에 이미지를 추가하는 방법? (0) | 2021.04.14 |
|---|---|
| Java에서 UTC 또는 GMT에서 현재 날짜와 시간을 어떻게 얻을 수 있습니까? (1) | 2021.04.14 |
| Eclipse에서 항아리를 가져 오는 방법 (0) | 2021.04.14 |
| thread.sleep (x) 또는 wait ()을 사용할 때 예외를 얻습니다. (0) | 2021.04.14 |
| 문자열에서 switch 문을 사용할 수없는 이유는 무엇입니까? (0) | 2021.04.14 |
댓글