直接上代码
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outsStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = inStream.read(buffer))!=-1){
outsStream.write(buffer,0,len);
}
outsStream.close();
inStream.close();
return outsStream.toByteArray();
}
public static byte[] readFileImage(File file)throws IOException{
BufferedInputStream bufferedInputStream=new BufferedInputStream(
new FileInputStream(file));
int len =bufferedInputStream.available();
byte[] bytes=new byte[len];
int r=bufferedInputStream.read(bytes);
if(len !=r){
bytes=null;
throw new IOException("读取文件不正确");
}
bufferedInputStream.close();
return bytes;
}
public static byte[] readFileImage(String filename)throws IOException{
BufferedInputStream bufferedInputStream=new BufferedInputStream(
new FileInputStream(filename));
int len =bufferedInputStream.available();
byte[] bytes=new byte[len];
int r=bufferedInputStream.read(bytes);
if(len !=r){
bytes=null;
throw new IOException("读取文件不正确");
}
bufferedInputStream.close();
return bytes;
}
/**
* 读取返回的信息
* @param in
* @return
*/
private static String getData(InputStream in) {
String result = "";
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = "";
try {
while ((line = br.readLine()) != null) {
// result = result + line;
sb.append(line);
}
br.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (result != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sb.toString();
}