Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

Upload and POST file to PHP page with Java

I need a way to upload a file and POST it into php page...

My php page is:

<?php 
$maxsize = 10485760;
$array_estensioni_ammesse=array('.tmp');
$uploaddir = 'uploads/';
if (is_uploaded_file($_FILES['file']['tmp_name']))
{
    if($_FILES['file']['size'] <= $maxsize)
    {
        $estensione = strtolower(substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], "."), strlen($_FILES['file']['name'])-strrpos($_FILES['file']['name'], ".")));
        if(!in_array($estensione, $array_estensioni_ammesse))
        {
            echo "File is not valid!
";
        }
        else
        {
            $uploadfile = $uploaddir . basename($_FILES['file']['name']); 
            echo "File ". $_FILES['file']['name'] ." uploaded successfully.
"; 
            if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
            {
                echo "File is valid, and was successfully moved.
";
            } 
            else 
                print_r($_FILES); 
        }
    }
    else
        echo "File is not valid!
";
}
else
{ 
    echo "Upload Failed!!!"; 
    print_r($_FILES);
} 
?>

and i use this java code in my desktop application:

HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL("http://www.mypage.org/upload.php").openConnection();
        httpUrlConnection.setDoOutput(true);
        httpUrlConnection.setRequestMethod("POST");
        OutputStream os = httpUrlConnection.getOutputStream();
        Thread.sleep(1000);
        BufferedInputStream fis = new BufferedInputStream(new FileInputStream("tmpfile.tmp"));

        long totalByte = fis.available();
        long byteTrasferred = 0;
        for (int i = 0; i < totalByte; i++) {
            os.write(fis.read());
            byteTrasferred = i + 1;
        }

        os.close();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                httpUrlConnection.getInputStream()));

        String s = null;
        while ((s = in.readLine()) != null) {
            System.out.println(s);
        }
        in.close();
        fis.close();

But I receive always the "Upload Failed!!!" message.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Even though the thread is very old, there may still be someone around looking for a more easy way to solve this problem (like me :))

After some research I found a way to uplaod a file without changing the original poster's Java-Code. You just have to use the following PHP-code:

<?php
  $filename="abc.xyz";
  $fileData=file_get_contents('php://input');
  $fhandle=fopen($filename, 'wb');
  fwrite($fhandle, $fileData);
  fclose($fhandle);
  echo("Done uploading");
?>

This code is just fetching the raw data sent by the java-application and writing it into a file. There is, however one problem: You dont get the original filename, so you have to transmit it somehow else.

I solved this problem by using a GET-Parameter, which makes a little change in the Java-code necessary:

HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL("http://www.mypage.org/upload.php").openConnection();

changes to

HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL("http://www.mypage.org/upload.php?filename=abc.def").openConnection();

In your PHP-script you change the line

$filename="abc.xyz";

to

$filename=$_GET['filename'];

This solution doesn't use any external librarys and seems to me much more simple than some of the other posted ones...

Hope I could help anyone:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...