根據以前的帖子的建議,我試圖使用Android: Uploading image on server with php但是我得到一個文件未找到異常。Android上傳文件到服務器
這是我在上面的帖子中描述的功能。我對這些輸入是:
畫廊:uploadFile:Source File not exist :content://media/external/images/media/342
照片:uploadFile:Source File not exist: file:///storage/emulated/0/MyDir/blah
的是從lanched到catputre /選擇他們的意圖得出這些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);
}
}
我也說你最後的答案可能在我的頭上。 – jcaruso
@jcaruso:除非你特別解釋*你不明白的東西,否則沒有人能夠提供澄清。 – CommonsWare
我不僅僅是要讓你先解釋它,而不是先閱讀。雖然我理解並做了你所問的,但顯然我錯了。我討厭那些不做研究並期望會變得艱難的人。然而,回到上面的改變它似乎已經工作(我有一個文件至少上傳到我的服務器),但它沒有擴展名(JPG/PNG),雖然它確實呈現當我去的URL – jcaruso