基于前一篇文章的建议,我尝试使用Android:使用php在服务器上上传图像,但是我发现文件未找到异常。
这是我在上面的帖子中描述的功能。我的输入是:
图库:uploadFile:Source File not exist :content://media/external/images/media/342
照片:uploadFile:Source File not exist: file:///storage/emulated/0/MyDir/blah
这些uri源自意图倾斜/选择它们的意图。有什么想法让我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);
}
}
我收到文件找不到异常
那是因为这些都不是文件的路径。您可以通过查看它们来说明这一点。您也没有按照我之前的回答进行操作。
更换:
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
与:
InputStream contentInputStream = getContentResolver().openInputStream(Uri.parse(exsistingFileName));
(并在其余方法fileInputStream
中用替换出现contentInputStream
)
注意:
假设您doFileUpload()
是在继承自的某个类Context
(例如anActivity
或a)上实现的Service
。您将需要安排获得ContentResolver
对doFileUpload()
通过其他方式,如果doFileUpload()
没有访问getContentResolver()
。
您可以通过将Uri
收到的传入doFileUpload()
而不是先将其转换为aString
然后再转换为a来简化事务Uri
。
您将需要为Content-Disposition:
标题创建自己的文件名,因为您不会从中获取文件名Uri
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句