如何将文件发送到REST服务?

时间:2018-11-10 10:32:26

标签: android rest file

我想从android获取rest服务中的文件。在我的代码中,我通过filestream从Android发送了url。如果我这样使用,我的整个应用都无法正常运行,只需说......

Logcat

  

合同“ IREST”中的操作Fileacces有一个名为   stream类型的System.IO.FileStream,但类型   System.IO.FileStream无法转换为QueryStringConverter。   UriTemplate查询值的变量必须具有可以   由QueryStringConverter

转换

我在这里发布了我的编码FYR

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == FILE_PICKER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
                Uri content_describer = data.getData();

                String src = null;
                try {
                    src = getFilePath(UploadActivity.this, content_describer);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
                Log.d("src is: ", src);
                source = new File(src);
                 fname = source.getName();

                Button text = (Button) findViewById(R.id.txt);
                text.setText(fname);


                text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            FileOpen.openFile(UploadActivity.this, source);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });


            }
        }

操作栏图标中的Onclick事件:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.mybutton) {
           new Save().execute();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

保存功能asynctask

private class Save extends AsyncTask<String,Void,String> {

        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                fis=new FileInputStream(source);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            ServiceHandler jsonParser = new ServiceHandler();
            String fileaccess = ip+"Mobile/rest.svc/Fileacces?stream="+fis+"&filename="+fname;
            Log.d("Fileaccess","fileaccess"+fileaccess);
            String result = jsonParser.makeServiceCall(fileaccess, ServiceHandler.GET);
            Log.i("fileaccess", "result: > " + result);
            return  null;
        }


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            System.out.println(s);

        }

    }

我在vb.net中的休息功能

 Public Function fileacces(ByVal stream As FileStream, ByVal filename As String) As String Implements IREST.fileacces
        Dim result = ""

        Try

            Dim path = "D:\XXX\YYY\ECM Source\Archive\EZECM\Settings\Monitor\" + filename
            Dim fileStream = New FileStream(path, FileMode.Create, FileAccess.Write)
            stream.CopyTo(fileStream)
            fileStream.Dispose()
            result = "file uploaded"


        Catch ex As Exception

        End Try
        Return result
    End Function

1 个答案:

答案 0 :(得分:0)

  

请使用base 64

喜欢

public void uploadFile(final String selectedFilePath) {
    try {


        FileInputStream inputStream = new FileInputStream(selectedFilePath);
        byte[] byteArray = IOUtils.toByteArray(inputStream);

        final String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
              //  tvFileName.setText(encoded);
                Log.e("File Base 64",encoded);
                filebas64=encoded;
            }
        });


    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.e("Error",e.toString());

    } catch (IOException e) {
        e.printStackTrace();
        Log.e("Error",e.toString());

    }

}