B4J Question java.io.FileNotFoundException Temp directory

Luis Felipe Andrade

Member
Licensed User
Hello, I am trying to upload an image to a server using B4J but I have not been successful, when uploading it I get the following error:
java.io.FileNotFoundException: C:\Users\landr\AppData\Local\Temp\1 (El sistema no puede encontrar el archivo especificado)

On the server side there is a php file that accepts the file and saves it, this has worked for me for a long time but my ISP has changed my server and now the service does not work, initially we tested that everything on the server was fine so I made a simple HTML to upload the image, and indeed it uploads without problem using the same php file, I would appreciate it if you could help me with this error.
B4X:
Dim j As  HttpJob
    Dim m As Map
    m.Initialize
    m.Put("file","Null")
    
    Dim files As List
    files.Initialize
    Dim fd1 As MultipartFileData
    fd1.Initialize
    fd1.KeyName = "File"
    fd1.Dir = "c:\BizetDS\Imagenes\"
    fd1.FileName = "111.jpg"
    fd1.ContentType = "image/jpg"
    files.Add(fd1)
    
    j.Initialize("", Me)
    
    j.PostMultipart("http://ServerIP/plesk-site-preview/seiton5s.com.mx/upload.php",m,files)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
        xui.MsgboxAsync(j.GetString,"Envio de archivo")
    Else
        xui.MsgboxAsync(j.GetString,"Error al enviar el archivo")
    End If
    j.Release
Also tryed with this code:
B4X:
Dim lline As String
    Dim lReader As TextReader
    Dim j As HttpJob
    Dim img As String = "111.jpg"
    j.Initialize("", Me)
    Dim mp As MultipartFileData
    mp.Initialize
    Dim imgruta As String = "c:\BizetDS\Imagenes\"
    mp.Dir = imgruta
    mp.FileName = img
    mp.KeyName = "file"
    mp.ContentType = "image/jpg"
    j.PostMultipart("http://Serverip/plesk-site-preview/seiton5s.com.mx/upload.php", Null, Array(mp)) 
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
        xui.MsgboxAsync(j.GetString,"Envio de archivo")
    Else
        xui.MsgboxAsync(j.GetString,"Error al enviar el archivo")
    End If
    j.Release
Also tryed with 'mp.ContentType = "multipart/form-data" but the same result
I attach the error image
Thank you in advance for your help!
 

Attachments

  • javaerror.jpg
    javaerror.jpg
    177.4 KB · Views: 35

Luis Felipe Andrade

Member
Licensed User
Here the php file:
phpfile upload.php:
<?php

define('KB', 1024);
define('MB', 1048576);
define('GB', 1073741824);
define('TB', 1099511627776);

$target_dir = "bizetds_ftp/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);

$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Compruebe si el archivo de imagen es una imagen real o una imagen falsa
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
    //echo "El archivo es una imagen - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "El archivo no es una imagen!";
    $uploadOk = 0;
}

if($uploadOk) {
    // Comprueba si el archivo ya existe.
    if (file_exists($target_file)) {
        echo " Status de Archivo: ";
        $uploadOk = 0;
    }
    // Verificar el tamaño del archivo
    if ($_FILES["file"]["size"] > 50 * MB) {
        echo "Se excedio el limite maximo del tamaño del archivo.  ";
     echo "Debe ser menor a 50 MB.  ";
        $uploadOk = 0;
    }
    // Permitir ciertos formatos de archivo
    if ($imageFileType != "jpg" && $imageFileType != "JPG"  && $imageFileType != "png" && $imageFileType != "PNG" && $imageFileType != "mp4" && $imageFileType != "MP4" && $imageFileType != "flv" && $imageFileType != "FLV") {
        echo " Tipo de archivo no valido, sólo se permiten archivos JPG, PNG y MP4. Revise la extensión de su archivo.";
        $uploadOk = 0;
    }
    // Compruebe si $uploadOk está establecido en 0 por un error
    if ($uploadOk == 0) {
        echo "Archivo ya existe en el servidor.";

    // Si esta bien, trata de guardar archivo
    } else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo "Archivo cargado con exito.   ";
            echo basename($_FILES["file"]["name"]);
        } else {
            echo "Error de comunicación.";
        }
    }
} else {
    echo "El archivo no es una imagen!!";
}
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Please post logs as text. Right click to copy.

2. The error is here:
B4X:
 If j.Success Then
        Log(j.GetString)
        xui.MsgboxAsync(j.GetString,"Envio de archivo")
    Else
        xui.MsgboxAsync(j.GetString,"Error al enviar el archivo") '<----------------------------
    End If

You cannot call j.GetString if the job failed. You can get the error message with j.ErrorMessage.
 
Upvote 0

Luis Felipe Andrade

Member
Licensed User
1. Please post logs as text. Right click to copy.

2. The error is here:
B4X:
 If j.Success Then
        Log(j.GetString)
        xui.MsgboxAsync(j.GetString,"Envio de archivo")
    Else
        xui.MsgboxAsync(j.GetString,"Error al enviar el archivo") '<----------------------------
    End If

You cannot call j.GetString if the job failed. You can get the error message with j.ErrorMessage.
It Works! thank you Erel!!!!
If some one uses this as reference, also is necessary to disable "firewall for web applications" in the VPS side.
 
Upvote 0
Top