【发布时间】:2015-04-30 11:39:53
【问题描述】:
我正在尝试使用 php 在服务器上上传图像,但在特定文件夹中。
这是 PHP 脚本:
<?php
$subfolder = $_POST['subfolder'];
mkdir($subfolder, 0755, true);
if(@move_uploaded_file($_FILES["filUpload"]["tmp_name"],"upload/".$subfolder.$_FILES["filUpload"]["name"]))
{
$arr["StatusID"] = "1";
$arr["Error"] = "";
}
else
{
$arr["StatusID"] = "0";
$arr["Error"] = "Cannot upload file.";
}
echo json_encode($arr);
?>
这就是我发送图像的方式:
//Upload
public void startUpload() {
Runnable runnable = new Runnable() {
public void run() {
handler.post(new Runnable() {
public void run() {
new UploadFileAsync().execute();
}
});
}
};
new Thread(runnable).start();
}
// Async Upload
public class UploadFileAsync extends AsyncTask<String, Void, Void> {
String resServer;
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
int resCode = 0;
String resMessage = "";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String strSDPath = selImgPath;
// Upload to PHP Script
String strUrlServer = "http://localhost/zon/uploadFile.php";
try {
/** Check file on SD Card ***/
File file = new File(strSDPath);
if(!file.exists())
{
resServer = "{\"StatusID\":\"0\",\"Error\":\"Please check path on SD Card\"}";
return null;
}
FileInputStream fileInputStream = new FileInputStream(new File(strSDPath));
URL url = new URL(strUrlServer);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("");
outputStream.writeBytes("Content-Disposition: form-data; name=\"filUpload\";filename=\"" + file.getName().toString() + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Response Code and Message
resCode = conn.getResponseCode();
if(resCode == HttpURLConnection.HTTP_OK)
{
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int read = 0;
while ((read = is.read()) != -1) {
bos.write(read);
}
byte[] result = bos.toByteArray();
bos.close();
resMessage = new String(result);
}
fileInputStream.close();
outputStream.flush();
outputStream.close();
resServer = resMessage.toString();
System.out.println("RES MESSAGE = " + resMessage.toString());
} catch (Exception ex) {
return null;
}
return null;
}
protected void onPostExecute(Void unused) {
// statusWhenFinish(position,resServer);
}
}
我不明白的部分是如何发送为在服务器上创建子文件夹而保留的参数。
【问题讨论】:
-
您不能在该行中传递额外的参数。但是,您可以根据需要发布任意数量的 key=value 参数。使用它们将子文件夹告诉 php 脚本。
-
我不知道该怎么做?这是我第一次使用这种方法。你有这方面的例子吗?
-
上传图片时,记得将表单的 ENCTYPE 设置为 `multipart/form-data´。
-
new UploadFileAsync().execute();这应该是startUpload.中唯一的语句删除所有其他代码。您不需要该线程,因为 AsyncTask 已经是一个线程。
标签: php android file-upload