1

尝试使用 android 应用程序将文件上传到我的服务器,效果很好,但我想将内容上传到特定文件夹(用户名文件夹,如果不存在则创建)。如何使用 multiform/form-data 添加该功能?

PHP:

<?php
$target_path  = "/home/myfolder/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ".  basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>

和(片段)当前的android代码:

    connection = (HttpURLConnection) url.openConnection();

    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

    outStream = new DataOutputStream( connection.getOutputStream() );
    outStream.writeBytes(hyph + boundary + end);
    outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fileToUpload +"\"" + end);
    outStream.writeBytes(end);
4

0 回答 0