2013-01-23 78 views
0

我想給我的文件:///mnt/sdcard/APK/Video.apk文件到插座,併發送PDF和文本文件。什麼是最好的方式來發送這種類型的文件編程?如何在本地服務器上添加pdf文件或文本文件?

+2

你是什麼意味着通過添加到本地服務器! –

+0

意味着我要通過wifi共享PDF或文本文件...我正在開發無線上網的文件傳輸application.I做了通過mediastore發送媒體文件,但我怎麼能發送DOC文件,其在SD卡?和本地服務器意味着我的設備上的IP地址瀏覽器.... –

回答

0

我用了我的回答我的問題我自己:-D所以這裏是誰了同樣的問題解決....首先,我才知道,在服務器端插座URI不工作「的文件這個文件路徑:/ //mnt/sdcard/APK/Video.apk「或其他類型的文件uri。只有「/mnt/sdcard/APK/Video.apk」在socket上工作。因此,從文件URI字符串其中有來自內容提供商得到的,我創造了我的服務器套接字文件。然後創建一個文件之後...我已經得到了我也文件大小....

private void addSDFileEntity(final Uri uri, HttpResponse response) 
      throws IOException { 
     if (mTransferStartedListener != null) { 
      mTransferStartedListener.started(uri); 
     } 

     Cursor c = mContext.getContentResolver().query(uri, null, null, null, 
       null); 
     c.moveToFirst(); 
     int nameIndex = c.getColumnIndexOrThrow(NewSDCardContentProvider.SDFiles.Columns.DISPLAY_NAME); 

String name = c.getString(nameIndex); 
     int dataIndex = c.getColumnIndexOrThrow(NewSDCardContentProvider.SDFiles.Columns._DATA); 
     String dataname = c.getString(dataIndex); 
     Uri data = Uri.parse(dataname); 




     if (name.endsWith(".pdf")) { 
      String contentTypepdf = "application/pdf"; 
      File myFile = new File(dataname); 

      int reqLen = (int) myFile.length(); 

      Log.d("apk len===>", "" + reqLen); 

      InputStream fis = new FileInputStream(myFile); 

      response.addHeader("Content-Type", contentTypepdf); 
      response.addHeader("Content-Length", "" + reqLen); 
      response.setEntity(new InputStreamEntity(fis, reqLen)); 

      if (socket == null) { 
       socket.close(); 

      } 

     } 
     if (name.endsWith(".apk")) { 
      String contentTypeapk = "application/vnd.android.package-archive"; 
      File myFile = new File(dataname); 

      int reqLen = (int) myFile.length(); 

      Log.d("apk len===>", "" + reqLen); 

      InputStream fis = new FileInputStream(myFile); 

      response.addHeader("Content-Type", contentTypeapk); 
      response.addHeader("Content-Length", "" + reqLen); 
      response.setEntity(new InputStreamEntity(fis, reqLen)); 

     } 

現在我在服務器套接字發送我的文件內容....

private void sendSDFileContent(
      DefaultHttpServerConnection serverConnection, 
      RequestLine requestLine) throws IOException, HttpException { 
     HttpResponse response = new BasicHttpResponse(new HttpVersion(1, 1), 
       200, "OK"); 
     String SDfileId = getSDFileId(requestLine.getUri()); 

     Log.d("selected URI from server", SDfileId); 
     addSDFileEntity(Uri.withAppendedPath(
       NewSDCardContentProvider.SDFiles.CONTENT_URI, SDfileId), 
       response); 
     serverConnection.sendResponseHeader(response); 
     serverConnection.sendResponseEntity(response); 
    } 
相關問題