久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Java 將文件上傳和 POST 到 PHP 頁面

Upload and POST file to PHP page with Java(使用 Java 將文件上傳和 POST 到 PHP 頁面)
本文介紹了使用 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 頁面的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 中文字幕视频在线 | 狠狠亚洲| 91亚洲一区| 国产精品久久免费观看 | 欧美激情视频一区二区三区在线播放 | 欧美激情综合色综合啪啪五月 | 欧美三级电影在线播放 | 中文字幕在线观看国产 | 免费看一区二区三区 | 日韩免费av一区二区 | 国产精品免费在线 | 久久精品91久久久久久再现 | 91视频久久| 日韩在线播放av | 日韩视频精品在线 | 日韩精品在线观看视频 | 日韩电影一区二区三区 | 99国产精品久久久久 | 成人精品高清 | 久热国产精品视频 | 99re视频在线观看 | 久久久久久久久久久久久9999 | 亚洲国产精品99久久久久久久久 | 欧美日韩国产在线观看 | 国产视频一区二区三区四区五区 | 一区二区三区四区av | 亚洲看片 | www.精品国产| 日本亚洲一区 | 亚洲视频一区二区三区 | 日韩精品| 成人片在线看 | 日韩中文字幕 | 亚洲视频中文字幕 | 亚洲欧洲日韩精品 中文字幕 | 精品1区2区3区 | 波多野结衣先锋影音 | 亚洲综合视频 | 色网在线看 | 久久综合伊人一区二区三 | www.4567|