어떻게해야하나요? 어떤 도움을 주시면 감사하겠습니다.InputStream (Image)을 ByteArrayInputStream으로 변환하십시오.
12
A
답변
18
입력 스트림에서 읽고 ByteArrayOutputStream에 쓰고 그 다음 toByteArray()
을 호출하여 바이트 배열을 가져옵니다.
바이트 배열을 중심으로 ByteArrayInputStream을 작성하여 읽습니다. 첫째
import java.io.*;
public class Test {
public static void main(String[] arg) throws Throwable {
File f = new File(arg[0]);
InputStream in = new FileInputStream(f);
byte[] buff = new byte[8000];
int bytesRead = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while((bytesRead = in.read(buff)) != -1) {
bao.write(buff, 0, bytesRead);
}
byte[] data = bao.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(data);
System.out.println(bin.available());
}
}
+0
거의 다 왔어요! 예를 들어 주셔서 감사합니다. IO의 진정한 마스터! – user398371
+0
당신은 환영합니다 :) – naikus
1
또는 다음이 InputStream에, 바이트 배열로 변환 :
여기에 빠른 테스트입니다. 이 InputStream 이후
File f = new File(arg[0]);
InputStream in = new FileInputStream(f);
// convert the inpustream to a byte array
byte[] buf = null;
try {
buf = new byte[in.available()];
while (in.read(buf) != -1) {
}
} catch (Exception e) {
System.out.println("Got exception while is -> bytearr conversion: " + e);
}
// now convert it to a bytearrayinputstream
ByteArrayInputStream bin = new ByteArrayInputStream(buf);
관련 문제
- 1. Java의 InputStream
- 2. 폐쇄에는 InputStream
- 3. InputStream 처리
- 4. InputStream 또는 InputStreamReader를 버퍼링해야합니까?
- 5. UTF-8로 InputStream 읽기
- 6. FileWriter 및 Java의 InputStream
- 7. InputStream 전에 FileSize 받기
- 8. Java : OutputStream 및 InputStream
- 9. InputStream 읽기 문제
- 10. java process and inputstream
- 11. 의 InputStream 리더는
- 12. 자바 프로세스 InputStream 버그?
- 13. Java InputStream! = Readable
- 14. Reader로 InputStream 열기
- 15. Http Request InputStream 복사하기
- 16. Android AssetManager/InputStream 예외
- 17. DocumentBuilder.parse (InputStream)가 null을 반환합니다.
- 18. 자바의 InputStream 0x0A 문제 자바에서
- 19. InputStream 형식의 XML을 읽는 방법
- 20. Process.waitFor(), 스레드, 그리고에는 InputStream 의사에
- 21. 자바의 InputStream 인코딩/캐릭터 세트
- 22. 안전하게 InputStream 읽기를 취소 하시겠습니까?
- 23. Byte [] to InputStream 또는 OutputStream
- 24. 주어진 바이트 시퀀스를 InputStream으로 변환하십시오.
- 25. jshort를 부호없는 short로 변환하십시오.
- 26. 문자열 백분율을 double로 변환하십시오.
- 27. json.net에서 json을 xml로 변환하십시오.
- 28. datetime js를 C로 변환하십시오.
- 29. 행을 행렬로 변환하십시오.
- 30. System.Windows.Media.Imaging.BitmapSource를 System.Drawing.Image로 변환하십시오.
는 http://stackoverflow.com/questions/2163644/in-java-how-can-i-convert-an-inputstream-into-a-byte-array- [] 바이트로 구성되어 byte http://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-in-java – h3xStream
'javax.imageio' 클래스를 사용하지 않는 이미지로 정확히 무엇을하고 있습니까? – Powerlord
Amazon S3에 업로드 중 ... Java 라이브러리에서 문자열 기반이 아닌 모든 데이터에 대해 ByteArrayInputStream이 필요합니다. – user398371