如何在spring mvc中将图像byte []值发布到数据库

时间:2014-04-04 05:51:05

标签: android web-services spring-mvc

大家好我想把图片发布到db.Here我发布了一个从android客户端到DB.Spring的图像MVC REST Web服务是mid layer.can你请建议我如何发布图像byte []值到来自android客户端的db。提前感谢您的宝贵建议。

1 个答案:

答案 0 :(得分:0)

您可以使用MultipartEntity

    public static HttpResponse sendImage(byte[] image) {
    HttpResponse responsePOST = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(postURL+"user_profile");
        post.setHeader("Accept", "application/json");                                   
        ByteArrayBody bab = new ByteArrayBody(image, firstname+".png");
        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);          
        reqEntity.addPart("user_profile[image_attributes[attachment]]", bab);           

        post.setEntity(reqEntity);                      
        responsePOST = client.execute(post);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responsePOST;
}