【问题标题】:Android-image upload issue to a REST server: Image format not supportedAndroid 图像上传到 REST 服务器的问题:不支持图像格式
【发布时间】:2015-04-10 03:58:35
【问题描述】:

我正在尝试将图像从我的 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();

}

我在服务器 web 中包含了以下 jars(不是一次全部),但仍然没有任何改变: httpmime-4.4.1.jar httpcore-4.4.1.jar jersey-server-1.18.jar jersey-servlet-1.18.jar mimepull-1.9.3.jar jersey-media-multipart-2.0.jar jersey-core-1.18.jar

我尝试在 addBinaryBody 中将 mime 类型更改为 multipart/form-data 和 multipart/*。仍然无法打开图像。

当我添加另一个 @FormDataParam() (用于内容处理)时,它会显示以下错误:

严重:缺少公共 javax.ws.rs.core.Response 方法的依赖项 严重:方法,公共 javax.ws.rs.core.Response com.proj.test.RestW.saveImage(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition),用资源、类的 POST 注释com.proj.test.RestW,未被识别为有效的资源方法。

当我删除内容处置参数时,它工作正常,但图像无法打开。

提前致谢

【问题讨论】:

    标签: android image rest netbeans multipart


    【解决方案1】:

    这是对我有用的代码

    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());
            }
            }   
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 2021-08-03
      • 1970-01-01
      • 2013-12-17
      • 2014-03-21
      相关资源
      最近更新 更多