2012-07-28 102 views
1

我需要從我的android模擬器目錄('data \ data \ org.mypackage \ file.dat')上傳一個文件到遠程服務器,但是當我嘗試訪問文件時,它會給出一個錯誤如'錯誤:權限被拒絕',我將我的sqlite數據庫存儲在'data \ data \ org.mypackage \ databases \'文件夾中,在應用程序啓動過程中,如果該文件夾中沒有sqlite數據庫,我將其從我的資產文件夾複製到該目錄並從那裏訪問它,它工作完美,但在這種情況下上傳任務要求許可爲什麼會發生這種情況?以下是我的uploadFile方法Android模擬器文件讀取權限

private void uploadFile(){ 

    HttpURLConnection conn = null; 
    DataOutputStream dos = null; 
    DataInputStream inStream = null; 
    String existingFileName="/data/data/org.mypackage/file.dat"; 
    GeneralFunctions.comment(existingFileName); 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1*1024*1024; 
    String responseFromServer = ""; 
    String urlString = "http://117.231.150.213:8080/upload.jsp"; 
    try 
    { 
    //------------------ CLIENT REQUEST 
    FileInputStream fileInputStream = new FileInputStream(new File(existingFileName)); 
    // open a URL connection to the Servlet 
    URL url = new URL(urlString); 
    // 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); 
    dos = new DataOutputStream(conn.getOutputStream()); 
    dos.writeBytes(twoHyphens + boundary + lineEnd); 
    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd); 
    dos.writeBytes(lineEnd); 
    // create a buffer of maximum size 
    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    buffer = new byte[bufferSize]; 
    // read file and write it into form... 
    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); 
    } 
    // send multipart form data necesssary after file data... 
    dos.writeBytes(lineEnd); 
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
    // close streams 
    GF.showToast("File is written"); 
    fileInputStream.close(); 
    dos.flush(); 
    dos.close(); 
    } 
    catch (Exception ex) 
    { 
     GeneralFunctions.exception("error: " + ex.getMessage()); 
    } 

    //------------------ read the SERVER RESPONSE 
    try { 
      inStream = new DataInputStream (conn.getInputStream()); 
      String str; 

      while ((str = inStream.readLine()) != null) 
      { 
       GeneralFunctions.comment("Server Response "+str); 
      } 
      inStream.close(); 

    } 
    catch (Exception ioex){ 
      GeneralFunctions.exception("error: " + ioex.getMessage()); 
    } 
    } 

回答

2

INTERNET的權限添加到您的清單。

如果您可以提供logcat,那麼它將會提供更多幫助。