将文件从真实设备上传到服务器时出现错误 [英] Getting error while uploading file from real device to server

查看:95
本文介绍了将文件从真实设备上传到服务器时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从真实设备将文件上传到服务器时出现错误。

I am getting an error while file uploading to the server from the real device. All are working well in the emulator as I switch to real device get an error.

这里是代码,

从设备中选择文件的功能

Function to choose a file from the device

private void showFileChooser(int index) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                index);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(getActivity(), "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }
}

OnResultActivityCode:

OnResultActivityCode :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedFileURI = data.getData();
            File file = new File(selectedFileURI.getPath().toString());

            Log.i("", "File : " + file.getName());
            uploadedFileName = file.getName().toString();
            tokens = new StringTokenizer(uploadedFileName, ":");
            first = tokens.nextToken();
            file_1 = tokens.nextToken().trim();
        }
    }
}

上传文件的代码:

@Override
    protected String doInBackground(String... strings) {
        try {

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("URL");


            if (file_1 != null && !file_1.equalsIgnoreCase("")) {
                file1 = new File(Environment.getExternalStorageDirectory()
                        .getAbsolutePath(), file_1);
                fileBody1 = new FileBody(file1);
            }

            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);



            if (file_1 != null && !file_1.equalsIgnoreCase(""))
                reqEntity.addPart("file", fileBody1);

            httpPost.setEntity(reqEntity);

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();

            if (resEntity != null) {
                final String responseStr = EntityUtils.toString(resEntity)
                        .trim();
                Log.v(TAG, "Response: " + responseStr);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            parseData(responseStr);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });

            }

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

这是我在尝试使用真实设备时遇到的错误。

Here is error that I got when try to do with real device.


12-26 12:47:32.919:W / System.err(15529):java.io.FileNotFoundException:/ storage / emulated / 0 / Woodenstreet Doc.doc:打开失败:ENOENT(没有此类文件或目录)
12-26 12:47:32.920:W / System.err(15529):位于libcore.io.IoBridge.open(IoBridge。 java:491)
12-26 12:47:32.920:W / System.err(15529):位于java.io.FileInputStream。(FileInputStream.java:76)
12-26 12:47 :32.920:W / System.err(15529):在org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:92)
12-26 12:47:32.920:W / System.err(15529):位于org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:206)
12-26 12:47:32.920:W / System.err(15529):在org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:224)
12-26 12:47:32.920:W / System.err(15529):在org.apache.http。实体.mime.Multip artEntity.writeTo(MultipartEntity.java:183)
12-26 12:47:32.920:W / System.err(15529):在org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java :97)
12-26 12:47:32.920:W / System.err(15529):在org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:162)
12- 26 12:47:32.920:W / System.err(15529):位于org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:272)
12-26 12:47:32.920: W / System.err(15529):位于org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:242)
12-26 12:47:32.920:W / System.err(15529):在org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
12-26 12:47:32.920:W / System.err(15529):在org.apache.http.impl。 client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
12-26 12:47:32.920:W / System.err(15529) :在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:592)
12-26 12:47:32.920:W / System.err(15529):在org.apache.http .impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:512)
12-26 12:47:32.920:W / System.err(15529):位于org.apache.http.impl.client.AbstractHttpClient。 execute(AbstractHttpClient.java:490)
12-26 12:47:32.921:W / System.err(15529):在com.cognus.gha.fragments.Fragment_Chat $ PostDataAsyncTask.doInBackground(Fragment_Chat.java:498 )
12-26 12:47:32.921:W / System.err(15529):在com.cognus.gha.fragments.Fragment_Chat $ PostDataAsyncTask.doInBackground(Fragment_Chat.java:1)
12- 26 12:47:32.921:W / System.err(15529):在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
12-26 12:47:32.921:W / System.err (15529):位于java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-26 12:47:32.921:W / System.err(15529):位于android.os.AsyncTask $ SerialExecutor $ 1.r un(AsyncTask.java:231)
12-26 12:47:32.921:W / System.err(15529):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-26 12:47:32.921:W / System.err(15529):位于java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)
12-26 12:47:32.921 :W / System.err(15529):在java.lang.Thread.run(Thread.java:818)
12-26 12:47:32.921:W / System.err(15529):原因: android.system.ErrnoException:打开失败:ENOENT(没有此类文件或目录)
12-26 12:47:32.921:W / System.err(15529):位于libcore.io.Posix.open(本机方法) )
12-26 12:47:32.921:W / System.err(15529):在libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
12-26 12:47:32.921 :W / System.err(15529):位于libcore.io.IoBridge.open(IoBridge.java:477)
12-26 12:47:32.921:W / System.err(15529):...另外22个

12-26 12:47:32.919: W/System.err(15529): java.io.FileNotFoundException: /storage/emulated/0/Woodenstreet Doc.doc: open failed: ENOENT (No such file or directory) 12-26 12:47:32.920: W/System.err(15529): at libcore.io.IoBridge.open(IoBridge.java:491) 12-26 12:47:32.920: W/System.err(15529): at java.io.FileInputStream.(FileInputStream.java:76) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:92) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:206) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:224) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:183) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:162) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:272) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:242) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:592) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:512) 12-26 12:47:32.920: W/System.err(15529): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:490) 12-26 12:47:32.921: W/System.err(15529): at com.cognus.gha.fragments.Fragment_Chat$PostDataAsyncTask.doInBackground(Fragment_Chat.java:498) 12-26 12:47:32.921: W/System.err(15529): at com.cognus.gha.fragments.Fragment_Chat$PostDataAsyncTask.doInBackground(Fragment_Chat.java:1) 12-26 12:47:32.921: W/System.err(15529): at android.os.AsyncTask$2.call(AsyncTask.java:288) 12-26 12:47:32.921: W/System.err(15529): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 12-26 12:47:32.921: W/System.err(15529): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 12-26 12:47:32.921: W/System.err(15529): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 12-26 12:47:32.921: W/System.err(15529): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 12-26 12:47:32.921: W/System.err(15529): at java.lang.Thread.run(Thread.java:818) 12-26 12:47:32.921: W/System.err(15529): Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) 12-26 12:47:32.921: W/System.err(15529): at libcore.io.Posix.open(Native Method) 12-26 12:47:32.921: W/System.err(15529): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 12-26 12:47:32.921: W/System.err(15529): at libcore.io.IoBridge.open(IoBridge.java:477) 12-26 12:47:32.921: W/System.err(15529): ... 22 more

错误-


12 -2 6 12:47:32.919:W / System.err(15529):java.io.FileNotFoundException:/ storage / emulated / 0 / Woodenstreet Doc.doc:打开失败:ENOENT(没有这样的文件或目录)

12-26 12:47:32.919: W/System.err(15529): java.io.FileNotFoundException: /storage/emulated/0/Woodenstreet Doc.doc: open failed: ENOENT (No such file or directory)

我不知道这是什么问题。请帮忙。

I am not able to whats the problem is that. Please help.

推荐答案

似乎文件路径错误。尝试使用此代码来获取文件路径,然后更改代码即可。

It seems you getting wrong file path. Try this code to get file path and then change code and it works.

使用此代码获取文件路径。

Use this code for get path of file.

public static String getRealPathFromUri(Context ctx, Uri uri) {
    String[] filePathColumn = { MediaStore.Files.FileColumns.DATA };

    Cursor cursor = ctx.getContentResolver().query(uri, filePathColumn,
            null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    picturePath = cursor.getString(columnIndex);
    Log.e("", "picturePath : " + picturePath);
    cursor.close();
    return picturePath;
}

以及onActivityResult()代码中的更改如下:

And the change in your onActivityResult() code like this :

    getRealPathFromUri(getActivity(), selectedFileURI);

最后在doInBackgraound()方法中使用它

And finally use this in doInBackgraound() method

if (file_1 != null && !file_1.equalsIgnoreCase("")) {
                file1 = new File(picturePath);
                fileBody1 = new FileBody(file1);
            }

希望这对您有帮助。

这篇关于将文件从真实设备上传到服务器时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆