写项目,debug,处理头像照片问题,将用户头像照片通过客户端发送保存在服务器端底下,使用时再从服务器端下载使用,
public static byte[] PhotoFileToByteArray(String filePath) {
File src = new File(filePath);
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
is = new FileInputStream(src);
baos = new ByteArrayOutputStream();
byte[] flush = new byte[1024 * 10];
int len = -1;
try {
while ((len = is.read(flush)) != -1) {
baos.write(flush,0,len);
}
baos.flush();
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (null != fis) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void ByteArrayToPhotoFile(byte[] src,String filePath) {
File dest = new File(filePath);
InputStream is = null;
OutputStream os = null;
try {
is = new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
byte[] flush = new byte[5];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush,0,len);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != os) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
写项目