使用 Java 上传和 POST 文件到 PHP 页面 [英] Upload and POST file to PHP page with Java

查看:30
本文介绍了使用 Java 上传和 POST 文件到 PHP 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种上传文件并将其发布到 php 页面的方法...

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

我的php页面是:

<?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);
} 
?>

我在我的桌面应用程序中使用这个 java 代码:

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.

推荐答案

尽管线程很老,但可能仍然有人在寻找更简单的方法来解决这个问题(像我一样:))

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

经过一番研究,我找到了一种无需更改原始发布者的 Java 代码即可上传文件的方法.您只需要使用以下 PHP 代码:

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

这段代码只是获取java应用程序发送的原始数据并将其写入文件.但是有一个问题:您没有获得原始文件名,因此您必须以其他方式传输它.

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.

我通过使用 GET 参数解决了这个问题,这需要对 Java 代码进行一些更改:

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();

更改为

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

在您的 PHP 脚本中更改行

In your PHP-script you change the line

$filename="abc.xyz";

$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:)

这篇关于使用 Java 上传和 POST 文件到 PHP 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆