且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

"缺少文件"错误在使用HTTP上传后的文件服务器

更新时间:2022-04-16 09:07:40

很多的奋斗,我终于解决了我的问题。答案是的文件上传的第二种方式,我用Apache库的修改。这里是code为我工作:

After lot of struggle, I finally solved my problem. The answer is a modification of the second way of file upload where I was using apache libraries. Here is the code that worked for me:

InputStream inputStream;
try{
    inputStream = new FileInputStream(new File("/sdcard/Pictures/","wallpaper.png"));
    byte[] data;
    try{
        data = IOUtils.toByteArray(inputStream);
        Log.d("File size", ""+ data.toString());
        MultipartEntityBuilder entityBuilder =   entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data),"wallpaper.png")
        entityBuilder.addPart("File",inputStreamBody);
        HttpClient httpClient = new DefaultHttpClient();
        String url="my url";
        HttpPost httpPost = new HttpPost(url);
        HttpEntity entity = entityBuilder.build();
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        BufferedReader bufferreader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        String msg="";
        String line = "";
        while ((line = bufferreader.readLine()) != null) {
            msg += line;
        }
        Log.i("msg=",""+msg);

        // Handle response back from script.
        if(httpResponse != null) {
            showToast("Upload Completed");

        } else { // Error, no response.
            showToast("Server Error");
        }
        } catch (IOException e) {
                e.printStackTrace();
        }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

我在我的问题code获得唯一的例外是

The exception I was getting in my question code was

04-09 16:45:32.876    7694-7750/com.tutsplus.nfcdemo W/System.err﹕ java.io.FileNotFoundException: /sdcard/Pictures: open failed: EISDIR (Is a directory)
04-09 16:45:32.877    7694-7750/com.tutsplus.nfcdemo W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:456)

我解决它通过简单的有

I solved it by simply having

inputStream = new FileInputStream(new File("/sdcard/Pictures/","wallpaper.png"));

而不是只此

inputStream = new FileInputStream(new File("/sdcard/Pictures/"));

还有一个原因,我的应用程序崩溃是因为我在异步任务有吐司()语句。要解决,我只是把这个功能。

One more reason why my app crashed was because I had Toast() statement in my async task. To resolve that I simply put this function

    public void showToast(final String toast)
    {
        runOnUiThread(new Runnable() {
            public void run()
            {
                    Toast.makeText(FileUpload.this, toast, Toast.LENGTH_SHORT).show();
            }
        });
    }

和所谓的它无论我需要面包。例如:

and called it wherever I needed to Toast. For example:

showToast("Upload Completed");

我还是一直无法弄清楚,为什么我的第一个code没有工作,为什么却显示出文件丢失的错误。任何有关更多的答案仍然是AP preciated。

I still haven't been able to figure out why my first code didn't work and why was it showing "missing file" error. Any more answer regarding that will still be appreciated.