1

我在尝试将文件上传到 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").

有什么问题?

4

1 回答 1

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();
                }
于 2013-10-25T04:48:01.650 回答