Android image file sent to php upload image file is application/octet-stream type and not image/jpeg?

前端 未结 4 1635
不思量自难忘°
不思量自难忘° 2020-12-09 22:38

Hello all hope someone can can help- To fininsh my android app I just need to finish the class were the user can upload there image to the server(MySql).

All works w

4条回答
  •  半阙折子戏
    2020-12-09 23:19

    At Android side to make proper call we should make the web-service call like this:

    try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("https://someserver.com/api/path/");
            postRequest.addHeader("Authorization",authHeader);
            //don't set the content type here            
            //postRequest.addHeader("Content-Type","multipart/form-data");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    
    
            File file = new File(filePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            reqEntity.addPart("parm-name", new InputStreamBody(fileInputStream,"image/jpeg","file_name.jpg"));
    
            postRequest.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(postRequest);
    
            }catch(Exception e) {
                Log.e("URISyntaxException", e.toString());
            }
    

提交回复
热议问题