将文件上传到http

时间:2012-02-13 05:56:39

标签: android

我正在上传.rar到我的http服务器...如果SD卡上没有文件,我怎么能取消上传...这是我的代码......

 private class SessionTask extends AsyncTask<String, Integer, Integer> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);
        Vibrator v2 = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
        v2.vibrate(1500);
        dialog = new ProgressDialog(TestUI.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setTitle("UploadFile");
        dialog.setMessage("Uploading file...");
        dialog.setCancelable(false);
        dialog.setProgress(0);
        dialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {

            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            MultipartEntity multipart = new MultipartEntity();
            File file = new File("/mnt/sdcard/321.rar");  // File with some location (filepath)
            Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
            FileBody fileB = new FileBody(file); // Create a new FileBody with the above mentioned file
            multipart.addPart("data", fileB); // Add the part to my MultipartEntity. "data" is parameter name for the file

            StringBody stringB;  // Now lets add some extra information in a StringBody
            try {
                stringB = new StringBody("I am the caption of the file",chars);  // Adding the content to the StringBody and setting up the encoding
                multipart.addPart("caption", stringB); // Add the part to my MultipartEntity
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            HttpPost post = new HttpPost("http://192.168.1.1:9999/folder/0"); // Setting up a HTTP Post method with the target url
            post.setEntity(multipart); // Setting the multipart Entity to the post method
            HttpResponse resp = httpclient.execute(post);  // Using some HttpClient (I'm using DefaultHttpClient) to execute the post method and receive the response
        } catch(MalformedURLException e) {
            Log.e(TestUI.TAG, "E: Malformed URL! " + e.getLocalizedMessage());

            return 1;
        } catch(IOException e) {
            Log.e(TestUI.TAG, "E: I/O error! " + e.getLocalizedMessage());
        return 2;
        }
        return 0;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        dialog.setMax(values[1]);
        dialog.setProgress(values[0]);

    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        dialog.dismiss();
        switch (result) {
        case 0:
            Toast.makeText(TestUI.this, "Uploading finished", Toast.LENGTH_LONG).show();
            new DownloadTask().execute(new String[] {TestUI.LINK_DOWN, TestUI.FILE_DOWN});
            break;
        case 1:
            Toast.makeText(TestUI.this, "E: Malformed URL!", Toast.LENGTH_LONG).show();
            break;
        case 2:
            Toast.makeText(TestUI.this, "E: I/O error! Connection was dismissed!!!", Toast.LENGTH_LONG).show();
            try {
                Thread.sleep(timesleep);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            new UploadTask().execute();
            break;
        default:
            break;
        }
    }
} 

当没有要上传的文件时 - 发生此类错误...
        01-05 22:12:08.887:D / dalvikvm(6626):GC_FOR_MALLOC在108ms内释放了1334个对象/ 250744个字节 01-05 22:12:13.230:D / dalvikvm(6626):GC_FOR_MALLOC在115ms内释放了839个对象/ 586800个字节 01-05 22:12:15.312:E / GF_UI_T(6626):E:I / O错误! /mnt/sdcard/321.rar(没有这样的文件或目录)

0 个答案:

没有答案