2011-03-24 95 views
0

everyone。我試圖從手機上將圖片文件上傳到我的電腦上的Apache Web服務器,但沒有成功。這裏是我的代碼:從Android SD卡上傳文件到網絡服務器

package com.testconnectivity; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import android.app.Activity; 
import android.os.Bundle; 

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     HttpURLConnection connection = null; 
     DataOutputStream outputStream = null; 
     DataInputStream inputStream = null; 

     String pathToOurFile = "/sdcard/Confused.jpg"; 
     String urlServer = "http://190.213.29.178/connectiontest1.php"; 
     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 

     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = 1*1024*1024; 

     try 
     { 
     FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); 

     URL url = new URL(urlServer); 
     connection = (HttpURLConnection) url.openConnection(); 

     // Allow Inputs & Outputs 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     connection.setUseCaches(false); 

     // Enable POST method 
     connection.setRequestMethod("POST"); 

     connection.setRequestProperty("Connection", "Keep-Alive"); 
     connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

     outputStream = new DataOutputStream(connection.getOutputStream()); 
     outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
     outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); 
     outputStream.writeBytes(lineEnd); 

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

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

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

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

     // Responses from the server (code and message) 
     int serverResponseCode = connection.getResponseCode(); 
     String serverResponseMessage = connection.getResponseMessage(); 

     fileInputStream.close(); 
     outputStream.flush(); 
     outputStream.close(); 
     } 
     catch (Exception ex) 
     { 
     //Exception handling 
     Log.d("MainActivity", "Print " + ex.toString()); 

     } 

    } 
} 

// connectiontest1.php

<?php 
$target_path = "./"; 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
echo "The file ". basename($_FILES['uploadedfile']['name']). 
" has been uploaded"; 
} else{ 
echo "There was an error uploading the file, please try again!"; 
} 
?> 

可能有人請告訴我出了什麼問題?我是否需要Web服務或者是否有其他問題?任何幫助將非常感激。

+0

對不起,試圖上傳我的php代碼下的評論連接測試,但它沒有工作。 – 2011-03-24 19:14:44

+0

你會得到什麼錯誤(在java端)? – MByD 2011-03-24 19:41:01

+0

@MByD - 編譯器或控制檯沒有註冊錯誤。有關問題可能出在哪裏的任何建議? – 2011-03-24 20:33:33

回答

0

你有android.os.NetworkOnMainThreadException嗎?
您應該嘗試使用AsyncTask。這對我有效。
檢查:How to fix android.os.NetworkOnMainThreadException?

+1

作爲一般性建議,最好解釋爲什麼這些東西連接起來,爲什麼使用AsyncTask可以解決這個問題。還有如何確認這是問題,原始海報應該在哪個方面看這個錯誤。 – EdC 2012-09-15 02:54:59

0

下面這樣的代碼:

//啓用POST方法 connection.setRequestMethod( 「POST」);

connection.setRequestProperty("Connection", "Keep-Alive"); 
    connection.setRequestProperty("Content-Type", "multipart/form data;boundary="+boundary); 

添加下列行:

connection.setRequestProperty( 「UploadedFile的」,pathToOurFile);

相關問題