gpt4 book ai didi

Android上传文件到服务器

转载 作者:行者123 更新时间:2023-11-29 00:23:02 24 4
gpt4 key购买 nike

根据上一篇文章的建议,我正在尝试使用 Android: Uploading image on server with php但是我得到一个文件未找到的异常。

这是我在上面的帖子中描述的功能。我对这些的输入是:

图库:上传文件:源文件不存在:content://media/external/images/media/342照片:uploadFile:源文件不存在:file:///storage/emulated/0/MyDir/blah

这些 uri 派生自 Intent a lanched to catputre/select them。知道为什么我会收到 File Not Found 异常吗?

private void doFileUpload(String exsistingFileName){
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;

//String exsistingFileName = "/sdcard/six.3gp";
// Is this the place are you doing something wrong.

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = "http://192.168.1.5/upload.php";
try
{
Log.e("MediaPlayer","Inside second Method");
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
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);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e("MediaPlayer","Headers are written");
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);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
String LogString = "";

while ((inputLine = in.readLine()) != null) {
LogString= LogString + inputLine;
}

Log.i(Utils.TAG, LogString);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
}

//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
Log.e("MediaPlayer","Server Response"+str);
}
/*while((str = inStream.readLine()) !=null ){

}*/
inStream.close();
}
catch (IOException ioex){
Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
}
}

最佳答案

i get a file not found exception

那是因为它们都不是文件路径。你可以通过观察它们来判断。您也没有按照 my previous answer 中的说明进行操作.

替换:

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

与:

InputStream contentInputStream = getContentResolver().openInputStream(Uri.parse(exsistingFileName));

(并将出现的 fileInputStream 替换为 contentInputStream 用于您的方法的其余部分)

注意:

  • 这假设您的 doFileUpload()在继承自 Context 的某个类上实现,例如 ActivityService .您需要安排获得 ContentResolverdoFileUpload()通过其他方式如果doFileUpload()无权访问 getContentResolver() .

  • 您可以通过传入 Uri 来简化事情你收到了doFileUpload() , 而不是将其转换为 String然后回到Uri .

  • 您需要为 Content-Disposition: 创建自己的文件名 header ,因为您没有从 Uri 中获取文件名.

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

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