从android上传照片

时间:2013-06-17 13:28:00

标签: java android jersey

我想将照片从android上传到服务器。我在Jersey Api做了网络服务。但发送照片时我收到415错误。

请帮我解决这个问题。

我尝试了完整的一天..

Android代码:

FileBody bin = new FileBody(file, "image/jpg");
MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mp.addPart("file", bin);

httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "multipart/form-data");
httppost.setEntity(mp);
HttpResponse response = httpClient.execute(httppost);

if (response.getStatusLine().getStatusCode() == 200) {
    ByteArrayOutputStream outstream = new ByteArrayOutputStream();
    response.getEntity().writeTo(outstream);
    return true;
}

网络服务代码:

@POST
@Path("uploadphoto")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/plain")
public String uploadNotices(@FormDataParam("file") InputStream picStream) {
    try {
        OutputStream out = new FileOutputStream(new File("d://1.png"));
        int read = 0;
        byte[] bytes = new byte[1024];

        out = new FileOutputStream(new File("d://1.png"));
        while ((read = picStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "yes";
}

1 个答案:

答案 0 :(得分:2)

当请求中发送的实体(POST或PUT中的内容)具有不受支持的媒体类型时,服务器返回

415。

确保您发送的服务器要求的媒体类型相同。

为什么会发生500错误?阅读this ...