繁体   English   中英

Android多个文件上传到服务器

[英]Android multiple file upload to server

我在将图像上传到服务器以更好地向您解释我的问题时遇到了问题,我准备了一对PHP脚本来模拟我想发生的事情。

PHP upload file ///////////////////////////////////////////////////////////////////// 
<form  enctype="multipart/form-data"  action=destination.php method=post>
<input  type=file name=file1 />
<input  type=file name=file2 />
<input  type=submit name=submit />
</form>


    PHP destination file ////////////////////////////////////////////////////////////////

<?php

$files = array('file1', 'file2', 'file3');
$path = 'elp/pendingimages/';

foreach ($files as $file) {
    if ($_FILES[$file]['error'] > 0) {
        echo 'Error: '. $_FILES[$file]['error'] .'<br />';
    }
    else {
        echo 'Upload: '. $_FILES[$file]['name'] .'<br />';
        echo 'Type: '. $_FILES[$file]['type'] .'<br />';
        echo 'Size: '. ($_FILES[$file]['size'] / 1024) .' Kb<br />';
        echo 'Stored in: '. $_FILES[$file]['tmp_name'] .'<br />';
    }
}

?>

///////////////////////////////////////////////////// //////////////////////////////////////

让我们将目标文件保留为原样,因为我将无法控制服务器,而我想拥有与我的JAVA代码中的上传文件相同的功能。 我已经有了这个,但是我的问题是服务器中只插入了一行

FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("image[]", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"image[]\";filename=\"" + fileName + "\"" + ";user_id=\"157\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();

我已经在我的博客上写了有关它的教程 不过在印尼语中。 在您的问题中,这是因为您仅上传了一个文件。 您将需要重复添加上传字段的过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM