0

我想将我的 file:///mnt/sdcard/APK/Video.apk 文件发送到套接字并发送 pdf 和文本文件。以编程方式发送此类文件的最佳方法是什么????

4

1 回答 1

0

我自己得到了我的问题的答案:-D所以这里是谁有同样问题的解决方案....首先我知道在服务器端套接字这个文件路径uri不起作用“file:///mnt/ sdcard/APK/Video.apk" 或其他类型的文件 uri。只有“/mnt/sdcard/APK/Video.apk”在套接字上工作。因此,从内容提供程序获得的文件 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);
    }
于 2013-01-30T07:08:00.683 回答