2013-10-25 88 views
1

我試圖將文件上傳到WebServer時遇到問題。嘗試將文件上傳到Apache服務器時出現Android問題

這是我使用的代碼:

File file = new File(fileUri.getPath()); 
try { 
    HttpClient httpclient = new DefaultHttpClient(); 

    HttpPost httppost = new HttpPost(URL_connect); 
    Log.e("subiendo archivo",fileUri.getPath()); 
    InputStreamEntity reqEntity = new InputStreamEntity(
      new FileInputStream(file), -1); 
    reqEntity.setContentType("binary/octet-stream"); 
    reqEntity.setChunked(true); // Send in multiple parts if needed 
    httppost.setEntity(reqEntity); 
    HttpResponse response = httpclient.execute(httppost); 
    //Do something with response... 

} catch (Exception e) { 
    e.printStackTrace(); 
    Toast toast1 = Toast.makeText(getApplicationContext(),"Error "+e, Toast.LENGTH_SHORT); 
    toast1.show(); 
    Vibrator vibrator =(Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
    vibrator.vibrate(200); 
} 

但是沒有辦法,文件沒有被上傳到服務器,我在服務器端嘗試這樣的:

$file = $_FILES["file"]; 

$nombre_archivo = $_FILES['file']['name']; 
$tipo_archivo = $_FILES['file']['type']; 
$tamano_archivo = $_FILES['file']['size']; 
$temporal = $_FILES['file']['tmp_name']; 

move_uploaded_file($temporal, "../img/media/".$nombre_archivo); 

在LogCat中沒有關於上傳的內容,它只是打印Log.e("uploading file","name and path of the file")

有什麼問題?

+0

我得到145 HTTP錯誤與此代碼 – SkyHunter

+0

是的,我幾個月前,我已經添加了答案和正確的代碼,您可以在下面看到它。 –

回答

1

解決,而不是使用發佈的代碼我用這個之前:

以下這些信息:

http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/

try { 
        MultipartEntity entity = new MultipartEntity(); 
       Log.e("enviando", fileUri.getPath()); 
        entity.addPart("reporte", new StringBody(reporte)); 
        entity.addPart("usuarioID", new StringBody(user)); 
        entity.addPart("archivo", new FileBody(file)); 
        httppost.setEntity(entity); 
        HttpResponse response = httpclient.execute(httppost); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
+0

上傳2MB文件時是否發現有問題? – kabuto178

相關問題