에 쓰십시오. 아래 코드를 사용하여 서버에서 이미지 파일을 읽으려고합니다. 그것은 예외로 계속 간다. 받은 바이트를 인쇄 할 때 올바른 바이트 수가 전송된다는 것을 알고 있습니다. Im은 python에서 이미지 파일을 보내려고합니다.Android, 이진 데이터를 읽고 파일
#open the image file and read it into an object
imgfile = open (marked_image, 'rb')
obj = imgfile.read()
#get the no of bytes in the image and convert it to a string
bytes = str(len(obj))
#send the number of bytes
self.conn.send(bytes + '\n')
if self.conn.sendall(obj) == None:
imgfile.flush()
imgfile.close()
print 'Image Sent'
else:
print 'Error'
여기는 android 부분입니다. 여기가 문제가되는 곳입니다. 이미지를 받고 파일에 쓰는 가장 좋은 방법에 대한 제안?
//read the number of bytes in the image
String noOfBytes = in.readLine();
Toast.makeText(this, noOfBytes, 5).show();
byte bytes [] = new byte [Integer.parseInt(noOfBytes)];
//create a file to store the retrieved image
File photo = new File(Environment.getExternalStorageDirectory(), "PostKey.jpg");
DataInputStream dis = new DataInputStream(link.getInputStream());
try{
os =new FileOutputStream(photo);
byte buf[]=new byte[1024];
int len;
while((len=dis.read(buf))>0)
os.write(buf,0,len);
Toast.makeText(this, "File recieved", 5).show();
os.close();
dis.close();
}catch(IOException e){
Toast.makeText(this, "An IO Error Occured", 5).show();
}
편집 : 아직 작동하지 않는 것 같습니다. 그 이후로 내 모든 노력의 결과로 전체 크기가 아닌 파일이 생성되었거나 응용 프로그램이 손상되었습니다. 파일이 서버 쪽을 보내기 전에 손상되지 않았 음을 알고 있습니다. 지금까지 파이썬에서 모든 메소드를 보내면 모든 것을 전송하거나 에러가 발생했을 때 예외를 던지며 지금까지는 예외를 던지지 않았다는 것을 확실히 전달할 수 있습니다. 그래서 클라이언트 쪽이 엉망입니다. 브라이언이 제안한 제안을 사용할 수 없도록 서버에서 파일을 보내야합니다.
데이터를 얻었습니까? 어떤 오류가 발생 했습니까? – dongshengcn
예 바이트 수가 있지만 이미지 데이터는 없습니다. 서버에서 Image가 전송되었고 클라이언트가 io 예외를 포착하고 IO 오류가 발생했다고 표시합니다. – Shpongle