怎么上传文件到php服务器?
php接受文件代码:<?php
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"];
}
move_uploaded_file($_FILES["file"]["tmp_name"],
"./" . $_FILES["file"]["name"]);//将上传的文件存储到服务器
//echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
?>
客户端安卓上传文件代码:
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try
{
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K
// 允许输入输出流
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
// 使用POST方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(
httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+ "002"
+ "\""
+ end);
dos.writeBytes(end);
String str = filePath.substring(filePath.lastIndexOf("/") + 1);
Log.i("GameTransformActivity",
"filePath.substring(filePath.lastIndexOf()="+str);
Log.i("GameTransformActivity",
"filePath="+filePath);
FileInputStream fis = new FileInputStream(filePath);
byte[] buffer = new byte[256]; // 8k
int count = 0;
// 读取文件
while ((count = fis.read(buffer)) != -1)
{
dos.write(buffer, 0, count);
Log.i("GameTransformActivity","count="+count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
Log.i("GameTransformActivity", "result="+result);
// Toast.makeText(GameTransformActivity.this, result, Toast.LENGTH_LONG).show();
dos.close();
is.close();
但是log的结果result = "<br />",这是为什么啊,我不懂php的啊 php 服务器 安卓文件上传 --------------------编程问答-------------------- .收听开机广播
intent-filter设置如下即可
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
补充:移动开发 , Android