尝试将文件上传到Apache Server时出现Android问题

时间:2013-10-25 03:03:17

标签: android file upload http-post

我在尝试将文件上传到WebServer时遇到了问题。

这是我正在使用的代码:

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")

有什么问题?

1 个答案:

答案 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();
                }
相关问题