Java 文件未正确保存

Java 文件未正确保存,java,android,Java,Android,我正在尝试使用webservice获取图像并保存到sd卡。文件已保存,但我无法打开该文件。一旦我打开文件,它就会说“无法加载图像”。下面是我的代码 httpTransport.call(SOAP_ACTION, envelope); Object response = envelope.getResponse(); test = response.toString(); Blob picture = org.hibernate.Hibernate.createBlob(test.replaceA

我正在尝试使用webservice获取图像并保存到sd卡。文件已保存,但我无法打开该文件。一旦我打开文件,它就会说“无法加载图像”。下面是我的代码

httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
test = response.toString();
Blob picture = org.hibernate.Hibernate.createBlob(test.replaceAll("-", "").getBytes());
String FILENAME = "voucher1.jpg"; 
File root = Environment.getExternalStorageDirectory();
FileOutputStream f = new FileOutputStream(new File(root, FILENAME));
InputStream x=picture.getBinaryStream();
int size=x.available();
byte b[]= new byte[size];
x.read(b);
f.write(b);
f.close();

请帮忙。谢谢

我想您需要调用f.flush(),以便将流中的所有数据写入文件

f.flush();
f.close();

我改变了格式,改为使用web服务,我只使用图像url来检索图像,它可以正常工作

我试过这个,效果很好。谢谢

URL url = new URL(fileURL);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/caldophilus.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();

测试的内容是什么,为什么要将“-”替换为“”?它是一个字节[],我在webservice中将其转换为字符串并在此处返回值。我之所以替换“-”,是因为当我检查测试字符串时..默认情况下有“-”,这可能是个问题。byte[]->String->byte[]如果使用不同的编码,则不起作用,我很可能这样做。另外,创建Blob的意义是什么?如果删除所有连字符,就会丢失部分文件文档:“此流未缓冲。大多数调用者应使用BufferedOutputStream包装此流。”关闭流足以导致所有内容都被刷新