无法将某些文件上传到服务器

时间:2017-04-13 14:29:00

标签: android file upload server okhttp3

抱歉我的语言不好。我是android的初学者,我有兴趣上传创建表格截屏的文件。这是我的代码

    private void takeScreenCapture() {
    Date now = new Date();
    try {
        String extr = Environment.getExternalStorageDirectory().toString();
        File mFolder = new File(extr + "/chatlistview_file");
        if (!mFolder.exists()) {
            mFolder.mkdir();
        }
        String s = now+"tmp.jpg";
        File f = new File(mFolder.getAbsolutePath(), s.replace(" ",""));

        String mPath = f.getAbsolutePath();
        MediaScannerConnection.scanFile(this, new String[] { f.getPath() }, new String[] { "image/jpeg" }, null);
        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView().findViewById(R.id.touchMe);
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);
        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();
        //use sctatch as image tumbnail
        asAttach(mPath);
        //openScreenshot(imageFile);
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

我可以创建图像文件,但我无法将此文件上传到服务器。我使用OkHttp3库上传一些文件,除非我的捕获文件都成功。 这是我的上传代码

    private void uploadImages(final Integer xid, final String xemail, Integer xidserver, final String xcat, final Integer xchatpos, final String pathFile) {

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            Log.d("File:",""+pathFile);

            File f = new File(pathFile);
            String content_type = getMimeType(f.getPath());
            if(content_type == null) {
                content_type = "image/*";
            }
            Log.d("File type:",""+content_type);
            String file_path = f.getAbsolutePath();
            OkHttpClient client = new OkHttpClient();
            RequestBody file_body = RequestBody.create(MediaType.parse(content_type), f);

            RequestBody request_body = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("type", content_type)
                    //.addFormDataPart("uploaded_file", file_path.substring(file_path.lastIndexOf("/") + 1), file_body)
                    .addFormDataPart("uploaded_file",xemail+file_path.substring(file_path.lastIndexOf("/") + 1), file_body)
                    .build();

            Request request = new Request.Builder()
                    .url(Config.URL_UPLOAD)
                    .post(request_body)
                    .build();

            try {
                Response response = client.newCall(request).execute();

                if(response.isSuccessful()){  // [START_EXCLUDE]
                    broadcastUploadFinished(1, xchatpos);
                    updateChatDb(xid, 1, xchatpos);
                    taskCompleted();
                    Log.d(TAG,""+xemail+file_path.substring(file_path.lastIndexOf("/") + 1));
                }else if (!response.isSuccessful()) {
                    Log.d("Respon Fail ",""+response);
                    throw new IOException("Error : " + response);
                }

                //progress.dismiss();

            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    });

    t.start();
}

上传此捕获的图像文件时没有失败报告。但我无法在服务器上找到我捕获的图像失败。 任何帮助表示赞赏....

0 个答案:

没有答案