gpt4 book ai didi

java - 文件未上传到 Android 服务器上

转载 作者:行者123 更新时间:2023-12-01 17:13:23 26 4
gpt4 key购买 nike

我尝试了多种在服务器上加载图像的方法,但没有一种方法显示任何错误。但没有任何帮助我。图片没有上传到服务器。图像的文件路径正确。我还尝试更改服务器上的文件路径,它也没有帮助我并添加了访问文件的权限,这没有帮助。

加载文件:

public class UploadFiles {
private String sourceFileUri = "";

public UploadFiles(final String sourceFileUri){
this.sourceFileUri = sourceFileUri;
new UploadFileAsync().execute("");
}

private class UploadFileAsync extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
try {
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);

if (sourceFile.isFile()) {

try {
String upLoadServerUri = "http://myserver/gpstracker/api/v1/users/uploadfile.php";

// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(
sourceFile);
URL url = new URL(upLoadServerUri);

// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
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("bill", sourceFileUri);

dos = new DataOutputStream(conn.getOutputStream());

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
+ sourceFileUri + "\"" + lineEnd);

dos.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0) {

dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math
.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0,
bufferSize);

}

// send multipart form data necesssary after file
// data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);

int serverResponseCode = conn.getResponseCode();

String serverResponseMessage = conn
.getResponseMessage();
System.out.println("SERVER: " + serverResponseMessage);
if (serverResponseCode == 200) {
}

fileInputStream.close();
dos.flush();
dos.close();

} catch (Exception e) {

e.printStackTrace();

}
} // End else block


} catch (Exception ex) {
ex.printStackTrace();
}
return "Executed";
}
}

}

php文件

<?php

if (is_uploaded_file($_FILES['bill']['tmp_name'])) {
$uploads_dir = '/var/www/html/gpstracker/api/v1/users/images';
$tmp_name = $_FILES['bill']['tmp_name'];
$pic_name = $_FILES['bill']['name'];
move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
}
else{
echo "File not uploaded successfully.";
}
?>

服务器响应:

System.out: SERVER: OK

最佳答案

服务器回复200并不代表上传成功。尝试输出 php echo。请参阅Get PHP Echo in Android .

编辑:您对该目录有权限吗?请参阅PHP move_uploaded_file() error?

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

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