gpt4 book ai didi

php - 无法通过 PHP (Android) 上传名称中有空格的文件

转载 作者:行者123 更新时间:2023-11-29 15:21:57 25 4
gpt4 key购买 nike

我正在使用以下 Android/PHP 代码将文件上传到服务器。除非原始文件名中有空格,否则一切正常,此时它会失败。

安卓:(有问题的空间在 String path 变量中)

public static void uploadFile(String path,String group,String folder) {



HttpURLConnection conn = null;


String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
// ------------------ CLIENT REQUEST



FileInputStream fileInputStream = new FileInputStream(new File(
path));





// open a URL connection to the Servlet

URL url = new URL("http://uploadsite.php" +
"?group_name=" + group + "&folder=" + folder );

// Open a HTTP connection to the URL

conn = (HttpURLConnection) url.openConnection();

// Allow Inputs
conn.setDoInput(true);

// Allow Outputs
conn.setDoOutput(true);

// Don't use a cached copy.
conn.setUseCaches(false);

// Use a post method.
conn.setRequestMethod("POST");

conn.setRequestProperty("Connection", "Keep-Alive");

conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);



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

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
+ path + "" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size



int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1*1024*1024;

byte[] buffer = new byte[bytesAvailable];

// read file and write it into form...

int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

while (bytesRead > 0) {
dos.write(buffer, 0, bytesAvailable);
bytesAvailable = fileInputStream.available();
bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
}

dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// close streams

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

} catch (MalformedURLException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}

catch (IOException ioe) {
Log.e(TAG, "error: " + ioe.getMessage(), ioe);
}

try {
BufferedReader rd = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
Log.e(TAG, "Message: " + line);
}
rd.close();

} catch (IOException ioex) {
Log.e(TAG, "error: " + ioex.getMessage(), ioex);
}
return;
}

PHP:

<?php

$group = $_GET["group_name"];
$folder = $_GET["folder"];
$base_path = "./db/";

$path = $base_path . $group;



$target_path = $path . "/" . $folder . "/" . basename( $_FILES['uploadedfile']['name']);

$group = ( $_FILES['group_name']['name']);



$file = 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 ($path.basename( $_FILES['uploadedfile']['name']), 0777);
} else{
echo "There was an error uploading the file, please try again!(".basename( $_FILES['uploadedfile']['name']).")";
}
?>

关于如何在不更改 android 端文件名的情况下上传此文件有什么建议吗?

谢谢乔希

最佳答案

某些字符在 URL 中使用时具有特殊含义。这就是为什么你总是必须正确编码你的字符串才能成为 url 的一部分。使用 Java,您可以使用 URLEncoder 类来完成这项工作。所以你的

dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
+ path + "" + lineEnd);

应该看看

dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
+ URLEncoder.encode(path, "UTF-8") + lineEnd);

编辑

如果您需要将字符串解码回其原始形式,那么在 PHP 端您可以使用 urldecode()

关于php - 无法通过 PHP (Android) 上传名称中有空格的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17513917/

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