gpt4 book ai didi

java - 无法将文件从 Android 上传到 PHP 服务器

转载 作者:行者123 更新时间:2023-11-29 08:06:34 25 4
gpt4 key购买 nike

我正在制作一个应用程序,它将一个文件从SD上传PHP 服务器。但是,当我尝试执行此操作时,出现错误。

我的安卓代码如下:

package de.fileupload;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import android.os.Bundle;

@SuppressWarnings("unused")
public class FileUpload extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final TextView tmp = (TextView) findViewById(R.id.textView1);
tmp.setText("Hi! Click the button!");

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
File f = new File("mnt/sdcard/SMSBackup.txt");
try {
f.createNewFile();
Date d = new Date();
PrintWriter writer = new PrintWriter(f);
writer.println(d.toString());
writer.close();
HttpClient client = new DefaultHttpClient();
httpPostFileUpload(client, "mnt/sdcard/SMSBackup.txt", "http://10.0.2.2:8080/admin/admin/upload1.php", "uploadedfile");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void httpPostFileUpload(
HttpClient client,
String filePath,
String uploadUri,
String inputNameAttr) throws ClientProtocolException,
IOException {
HttpUriRequest request = new HttpPost(uploadUri);
MultipartEntity form = new MultipartEntity();

client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
form.addPart(inputNameAttr, new FileBody(new File(filePath)));
((HttpEntityEnclosingRequestBase) request).setEntity(form);
try {
client.execute(request);
} catch (ClientProtocolException e) {
throw e;
} catch (IOException ee) {
throw ee;
}
}
}

我的 PHP 文件如下:

上传1.php

<meta name="generator" content="Namo WebEditor(Trial)">
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br /><input
type="submit" value="Upload File" />
</form>
<?php
$to_file = "tmp/" . basename($_FILES['uploadedfile']['name']);
$from_file = $_FILES['uploadedfile']['tmp_name'];
if (move_uploaded_file($from_file, $to_file)) {
echo "Successful upload";
?>
<a href="<?php echo $to_file;?>"><?php echo $to_file;?></a>
<?php
} else {
echo "Unsuccessful upload";
}
?>

upload.php 是:

<?php
// Where the file is going to be placed
$target_path = "localhost/admin/admin/uploads/";

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$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";
chmod ("uploads/".basename( $_FILES['uploadedfile']['name']), 0644);
} else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename( $_FILES['uploadedfile']['name']);
echo "target_path: " .$target_path;
}
?>

谁能告诉我哪里错了?它总是显示文件上传失败。当我的服务器正在运行时。

最佳答案

尝试改用move_uploaded_file(),函数copy()。它们使用相同的输入参数,如下所示:

if(copy($_FILES['uploadedfile']['tmp_name'], $target_path)) { ...

很可能PHP进程没有权限在上传成功后将上传的文件移动到不同的文件夹,因为移动本质上包含复制和删除操作。

还有一件事——尽量不要将上传文件的权限更改为 0644,因为这也可以限制在 PHP 进程中,即当你在 Linux 上处理文件系统操作时(我假设你使用 Linux 机器作为你的服务器)工作进程(在你的情况下是 PHP 和 apache)设置了特定的权限,也许他们没有能力删除/移动工作文件夹之外的文件。您还应该将上传文件夹权限更改为 755 或 777。

关于java - 无法将文件从 Android 上传到 PHP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10800042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com