如何使用java应用程序将local.png发送到服务器上的.php文件?

如何使用java应用程序将local.png发送到服务器上的.php文件?,java,image,post,png,Java,Image,Post,Png,我有一个本地的.png文件,我想使用POST数据将其发送到一个.php脚本,该脚本将数据保存到服务器上的一个.png文件中。我该怎么做?我需要编码吗?我只有一个文件和一种发布数据的方法 以下是我发送.png的方式: public static byte[] imageToByte(File file) throws FileNotFoundException { FileInputStream fis = new FileInputStream(file); ByteArrayO

我有一个本地的.png文件,我想使用POST数据将其发送到一个.php脚本,该脚本将数据保存到服务器上的一个.png文件中。我该怎么做?我需要编码吗?我只有一个文件和一种发布数据的方法

以下是我发送.png的方式:

public static byte[] imageToByte(File file) throws FileNotFoundException {
    FileInputStream fis = new FileInputStream(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    try {
        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
    } catch (IOException ex) {
    }
    byte[] bytes = bos.toByteArray();
    return bytes;
}

public static void sendPostData(String url, HashMap<String, String> data)
        throws Exception {
    URL siteUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setDoInput(true);

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());

    Set keys = data.keySet();
    Iterator keyIter = keys.iterator();
    String content = "";
    for (int i = 0; keyIter.hasNext(); i++) {
        Object key = keyIter.next();
        if (i != 0) {
            content += "&";
        }
        content += key + "=" + URLEncoder.encode(data.get(key), "UTF-8");
    }
    System.out.println(content);
    out.writeBytes(content);
    out.flush();
    out.close();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            conn.getInputStream()));
    String line = "";
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    in.close();
}
PHP脚本:

<? 
// Config
$uploadBase = "../screenshots/";
$uploadFilename = $_GET['user'] . ".png";
$uploadPath = $uploadBase . $uploadFilename;

// Upload directory
if(!is_dir($uploadBase))
mkdir($uploadBase);

// Grab the data
$incomingData = $_POST['img'];

// Valid data?
if(!$incomingData || !isset($_POST['img']))
die("No input data");

// Write to disk
$fh = fopen($uploadPath, 'w') or die("Error opening file");
fwrite($fh, $incomingData) or die("Error writing to file");
fclose($fh) or die("Error closing file");

echo "Success";
 ?>

我的简约代码可以工作:

$body = file_get_contents('php://input');
$fh = fopen('file.txt', 'w') or die("Error opening fil
e");
fwrite($fh, $body) or die("Error writing to file");
fclose($fh)

curl --upload-file download.txt http://example.com/upload.php

但是,请将方法设置为PUT。

我必须承认,我很惊讶您几乎得到了正确的文件。实际上,当您使用浏览器发送文件时,表单标记定义了一种编码:enctype=multipart/form data。我不知道它是如何工作的,它是在中定义的,但它包括对文件进行编码,例如,在Base64中。无论如何,如果您使用http客户端库(如

中的库),那么您可以忽略内部内容。当我尝试上载时,图像已损坏。当我用记事本打开.png时,我看到所有内容都与本地文件相同,只是代码中有“\0”而不是“NUL”。你是在写一篇简单的文章还是一篇表单编码的文章?我编辑了主要的文章和方法。PHP代码在哪里?这可能就是问题所在。看看。我刚刚编辑了PHP代码。我查看了该链接,并尝试使用文件“获取内容”php://input但是java作为post数据发送的内容与实际的.png中的内容不匹配。它必须被编码或解码吗?最后一行是做什么的?我认为问题在于java或php编码/解码,因为:最后一行将文件发送到php脚本。您的Java和PHP代码已经彻底崩溃了。