用户上传头像,上传的头像图片需要上传至服务器的MySQL数据库保存,当用户上传成功之后每次登陆后从数据库获取头像信息显示。
B:服务器接收代码:
C:客户端解析并显示:
由于之前一直使用JSON作为客户端和服务器端数据传输形式,JSON虽然使用方便但是不支持自定义类型,所以遇到的问题就是将二进制图片文件转成String,再传到服务器并保存到数据库。
在查阅了一些资料后想出了一个方法:
1,将图片转换成Byte[]
2,利用Base64将Byte[]加密编码转换成String
3,利用JSON将String传到服务器,由于图片一般转换后的String 是很大的,所以在mysql中保存为MEDIUMTEXT类型。方便以后从数据库直接读出来通过JSON再返回客户端解析后显示。
4,显示时将从数据库读出来的String通过Base64解码成Byte[];
5,用BitmapFactory将Byte[]转换成Bitmap显示。
A客户端向服务器端发送:
- private void edit_headPicture(Bitmap bitmap) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();//将Bitmap转成Byte[]
- bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);//压缩
- headPicture =Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT);//加密转换成String
- int n=headPicture.length();
- Thread submit=new Thread(new SubmitThread());
- submit.start();
- }
- class SubmitThread implements Runnable
- {
- Message msg = handler.obtainMessage();
- @Override
- public void run() {
- // TODO Auto-generated method stub
- JSONObject object=new JSONObject();
- try {
- object.put("headPicture", headPicture);
- msg.what=Server(object);
- handler.sendMessage(msg);
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public int Server(JSONObject object)
- {
- String path=LoginActivity.URL+"SetInfoManager";
- try{
- URL url=new URL(path);
- String content = String.valueOf(object);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5000);
- conn.setDoOutput(true);
- conn.setRequestMethod("POST");
- conn.setRequestProperty("User-Agent", "Fiddler");
- conn.setRequestProperty("Content-Type", "application/json");
- OutputStream os = conn.getOutputStream();
- os.write(content.getBytes());
- os.close();
- int code = conn.getResponseCode();
- if(code == 200)
- {
- BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String result="";
- result = in.readLine();
- in.close();
- return Integer.parseInt(result);
- }
- else return 0;
- }catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return 0;
- }
private void edit_headPicture(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();//将Bitmap转成Byte[]
bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);//压缩
headPicture =Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT);//加密转换成String
int n=headPicture.length();
Thread submit=new Thread(new SubmitThread());
submit.start();
}
class SubmitThread implements Runnable
{
Message msg = handler.obtainMessage();
@Override
public void run() {
// TODO Auto-generated method stub
JSONObject object=new JSONObject();
try {
object.put("headPicture", headPicture);
msg.what=Server(object);
handler.sendMessage(msg);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int Server(JSONObject object)
{
String path=LoginActivity.URL+"SetInfoManager";
try{
URL url=new URL(path);
String content = String.valueOf(object);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Fiddler");
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
int code = conn.getResponseCode();
if(code == 200)
{
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String result="";
result = in.readLine();
in.close();
return Integer.parseInt(result);
}
else return 0;
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
B:服务器接收代码:
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- StringBuffer buffer=new StringBuffer();
- String line="";
- request.setCharacterEncoding("UTF-8");
- BufferedReader reader=request.getReader();
- while((line=reader.readLine())!=null)
- {
- buffer.append(line);
- }
- try {
- JSONObject object=new JSONObject(buffer.toString());
- int result=new SetInfoManagerDB(object).Update();
- response.getOutputStream().write((result+"").getBytes());
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- <span style="white-space:pre"> </span> sql="update companyview set headPicture=? where e_Name=?";
- try {
- try {
- stmt=conn.prepareStatement(sql);
- stmt.setString(1, object.getString("headPicture"));
- stmt.setString(2, username);
- stmt.executeUpdate();
- stmt.close();
- return 4;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return 6;
- }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StringBuffer buffer=new StringBuffer();
String line="";
request.setCharacterEncoding("UTF-8");
BufferedReader reader=request.getReader();
while((line=reader.readLine())!=null)
{
buffer.append(line);
}
try {
JSONObject object=new JSONObject(buffer.toString());
int result=new SetInfoManagerDB(object).Update();
response.getOutputStream().write((result+"").getBytes());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
<span style="white-space:pre"> </span> sql="update companyview set headPicture=? where e_Name=?";
try {
try {
stmt=conn.prepareStatement(sql);
stmt.setString(1, object.getString("headPicture"));
stmt.setString(2, username);
stmt.executeUpdate();
stmt.close();
return 4;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 6;
}
C:客户端解析并显示:
- String s=view.getString("headPicture");
- if(s.equals(""))
- btn_user.setImageResource(R.drawable.ic_launcher);
- else
- {
- byte[] bytes = Base64.decode(view.getString("headPicture"), Base64.DEFAULT);
- btn_user.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
- }