0

我正在尝试将图像上传到 webService,但我做不到,我在这里和互联网上搜索了很多主题,但找不到好的解决方案。

当我运行此代码时,我收到错误请求错误。

更新:我使用了此链接中的一些代码:Uploading MS Word files from Android to .Net WCF?

但是给我FileNotFoundException,但我的文件路径是:/mnt/sdcard/ImageDir/images/ilan_1360917248037__thumb_.jpeg

这是我正在尝试的代码:

public static String imgUpload(String url, List<NameValuePair> list, List<String> images){

        String result = Constants.EMPTY;
        Bitmap bm;

        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, ServiceConstant.TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, ServiceConstant.TIMEOUT_MILLISEC);

        HttpParams p = new BasicHttpParams();
        HttpClient httpclient = new DefaultHttpClient(p);


        HttpPost httppost = new HttpPost(url);        

        String resimYol = images.get(0);
        resimYol = resimYol.replace("file:///", "/");
        bm = BitmapFactory.decodeFile(resimYol);
        Log.d("RESIL_YOL", resimYol.toString());


        try{                        


            ByteArrayBody bab = new ByteArrayBody(b, resimYol);


            ResponseHandler<String> responseHandler = new BasicResponseHandler();  

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();            

            ByteArrayBody bab = new ByteArrayBody(data, resimYol);


            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            httppost.setEntity(reqEntity);    
        result = httpclient.execute(httppost,responseHandler);

        }

        catch (Exception e) {
              // handle exception here
              Log.e(e.getClass().getName(), e.getMessage());
            }
        return result; 
    }

这是我的日志:

02-12 11:43:07.467: E/org.apache.http.client.HttpResponseException(19112): Bad Request
4

3 回答 3

0

试试这个例子链接它可能对你有帮助。

并添加所有需要的 jar 文件。

于 2013-02-12T10:22:48.373 回答
0

尝试转换imageinBase64然后将其String响应作为参数发送到 Web 服务以成功上传到服务器。

于 2013-02-12T10:37:20.810 回答
0
public static String __imgUpload(String url, List<NameValuePair> list, List<String> images)
    {
         URL url1=null;
         HttpURLConnection conn = null;
         DataOutputStream dos = null;
         DataInputStream inStream = null;
         String existingFileName= null;

            String urlServer = url;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "*****"; 
            String str = null;

         int bytesRead, bytesAvailable, bufferSize;
         byte[] buffer;
         int maxBufferSize = Integer.MAX_VALUE;
         String responseFromServer = "";


          try {
            url1 = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }                

          for(int resimID=0;resimID<images.size();resimID++)
          {
             existingFileName=    images.get(resimID); 
             existingFileName = existingFileName.replace("file:///", "/");

             try
             {
             FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );


              conn = (HttpURLConnection) url1.openConnection();

              conn.setDoInput(true);

              conn.setDoOutput(true);

              conn.setUseCaches(false);

              conn.setRequestMethod("POST");
              conn.setRequestProperty("Connection", "Keep-Alive");
              conn.setRequestProperty("Content-Type", "application/stream");
              dos = new DataOutputStream( conn.getOutputStream() );

              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              buffer = new byte[bufferSize];
              // read file and write it into form...
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              while (bytesRead > 0)
              {
               dos.write(buffer, 0, bufferSize);
               bytesAvailable = fileInputStream.available();
               bufferSize = Math.min(bytesAvailable, maxBufferSize);
               bytesRead = fileInputStream.read(buffer, 0, bufferSize);

              }

              dos.writeBytes(lineEnd);


              // close streams
              Log.e("Debug",twoHyphens + boundary + twoHyphens + lineEnd);
              fileInputStream.close();
              dos.flush();
              dos.close();
             }
             catch (MalformedURLException ex)
             {
                  Log.e("Debug", "error 1: " + ex.getMessage(), ex);
             }
             catch (IOException ioe)
             {
                  Log.e("Debug", "error 2: " + ioe.getMessage(), ioe);
             }

             try {
                   inStream = new DataInputStream ( conn.getInputStream() );


                   while (( str = inStream.readLine()) != null)
                   {
                        Log.e("Debug","Server Response "+str);
                   }
                   inStream.close();

             }
             catch (IOException ioex){
                  Log.e("Debug", "error 3: " + ioex.getMessage(), ioex);
             }  
          }

             return str;
    }

这段代码肯定有效。

于 2013-04-27T07:58:59.870 回答