我尝试将图片从我的Android应用上传到休息服务器。图像确实上传了,但是在服务器上,照片查看器或其他图像应用程序无法打开上传的图像。 我在服务器计算机上使用netbeans 7中的glassfish 3。我没有使用maven,我更喜欢非移动解决方案。
这里是用于在Runnable
中从android上传图像的代码@Override
public void run() {
// TODO Auto-generated method stub
try{
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
Bitmap original = BitmapFactory.decodeFile(path);
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 77, out);
byte[] data = out.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data,"image.JPG");
MultipartEntityBuilder mpeb = MultipartEntityBuilder.create();
mpeb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
mpeb.addBinaryBody("file", data, ContentType.create("image/jpg"), "image.jpeg");
HttpPost request = new HttpPost(url);
request.setEntity(mpeb.build());
request.addHeader("Content-type","multipart/form-data");
HttpResponse response = httpClient.execute(request,httpContext);
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode()== 201){
Log.w("ced", "pic success");
}else{
Log.w("ced", "pic failed ,code "+response.getStatusLine().getStatusCode());
}
}catch(Exception e){
Log.e("error",e.getMessage());
}
}
用于在服务器计算机上接收图像的代码。
@POST
@Path("/imgs/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response saveImage(@FormDataParam("file") InputStream is
){
try {
FileOutputStream os = new FileOutputStream(new File("C:/Users/files/Downloads/"+"img.jpg"));
Utils.copyFileStream(is, os);
os.flush();
os.close();
return Response.status(201).entity("reached").build();
} catch (FileNotFoundException ex) {
Logger.getLogger(RestW.class.getName()).log(Level.SEVERE, null, ex);
}catch(IOException e){
Logger.getLogger(RestW.class.getName()).log(Level.SEVERE, null, e);
}
return Response.status(500).entity("error").build();
}
我在服务器网站中包含了以下广告(并非一下子),但仍然没有改变: httpmime-4.4.1.jar 的HttpCore-4.4.1.jar 新泽西服务器1.18.jar 新泽西州的servlet-1.18.jar mimepull-1.9.3.jar 新泽西媒体多-2.0.jar Jersey的芯 - 1.18.jar
我尝试将mime类型更改为multipart / form-data和addBinaryBody中的multipart / *。但是,图像无法打开。
当我添加另一个@FormDataParam()(用于内容处理)时,它会显示以下错误:
严重:缺少方法public javax.ws.rs.core.Response的依赖项 SEVERE:方法,公共javax.ws.rs.core.Response com.proj.test.RestW.saveImage(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition),使用POST资源注释,类com.proj.test.RestW,不被视为有效的资源方法。
当我删除内容配置参数时,它工作正常但图像无法打开。
提前致谢
答案 0 :(得分:0)
尝试将“image / jpg”替换为“image / jpeg”
答案 1 :(得分:0)
这里有适合我的代码
private void sendPics(ArrayList<String> paths){
for(int i = 0;i<paths.size();i++){
String p = paths.get(i);
try{
Bitmap original = BitmapFactory.decodeFile(p);
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] data = out.toByteArray();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Content-Type", "multipart/form-data");
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data);
dataOutputStream.flush();
dataOutputStream.close();
int c = connection.getResponseCode();
connection.disconnect();
}catch(Exception e){
Log.e("cedError",e.getMessage());
}
}
}
}