我想将照片从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";
}
答案 0 :(得分:2)