使用urlconnection上传文件

时间:2013-07-06 12:00:45

标签: java android ftp urlconnection uploading

你好,我正在开发一个Android应用程序,我试图上传一个文件到我的ftp服务器与Url连接连接成功,但它不上传文件到我的服务器和logcat也没有显示任何错误这是我的代码任何帮助将提前appriciated thanx。 {仅限主要代码}

代码:

       new Thread(new Runnable() {

            @Override
            public void run() {


                    String host = "ethicstrain.tk";
                    String user = "myuername";
                    String pass = "mypass";
                    String filePath = Environment.getExternalStorageDirectory()+"/index2.html";
                    String uploadPath = "public_html/"+str+"/index.html";
                    String filename = "index2.html";





                    StringBuffer sb = new StringBuffer( "http://" );


                       sb.append( user ); 
                       sb.append( ':' );
                       sb.append( pass);
                       sb.append( '@' );

                    sb.append( host );
                    sb.append( '/' );
                    sb.append( str ); // str is the text input from EditText.
                    sb.append( '/');
                    sb.append(filename);


                    sb.append( ";type=i" ); 

                    BufferedInputStream bis = null;
                    BufferedOutputStream bos = null;
                    try
                    {
                       URL url = new URL(sb.toString());
                       URLConnection urlc = url.openConnection();

                       if(true){


                       Log.v ("connecting to server", filename);
                       }
                       urlc.setDoOutput(true);

                       bos = new BufferedOutputStream( urlc.getOutputStream() );
                       bis = new BufferedInputStream( new FileInputStream(filePath ) );

                       int i;

                       while ((i = bis.read()) != -1)
                       {
                          bos.write( i );
                       }



                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    finally
                    {
                       if (bis != null)
                          try
                          {

                             Log.v("","file uploaded to server");
                             bis.close();
                          }
                          catch (IOException ioe)
                          {
                             ioe.printStackTrace();
                          }
                       if (bos != null)
                          try
                          {
                             bos.close();
                          }
                          catch (IOException ioe)
                          {
                             ioe.printStackTrace();
                          }
                    }
                 }


        /

        }).start();

现在我正在使用apache ftpclient,这不显示任何错误,但文件未上传 这里是代码

                                try{    
                                FTPClient client = new FTPClient ();
                        client.connect(server);
                        client.enterLocalPassiveMode();
                        client.login(user, pass);
                        client.makeDirectory("/public_html/"+str);
                        client.changeWorkingDirectory(str);
                        @SuppressWarnings("unused")
                        String ac = "30073761354";
                        client.setFileType(FTP.BINARY_FILE_TYPE);
                        client.setFileTransferMode(FTP.BINARY_FILE_TYPE );


                         FileInputStream is = null;

                        is = new FileInputStream(Environment.getExternalStorageDirectory()+"/index.html");
                        Boolean isStored = client.storeFile("/public_html/"+str+"/", is);
                        is.close();








                        client.logout();
                        client.disconnect();

}

0 个答案:

没有答案