我想将照片从 android 上传到服务器。我在 Jersey Api 中创建了 Web 服务。但是我在发送照片时收到 415 错误。
请帮我解决这个问题。
我尝试了整整一天..
安卓代码:
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";
}