使用Java ME通过FTP上传文件

时间:2010-06-27 22:50:52

标签: file-io java-me ftp

如何修改以下java代码,使其适用于J2RE(Java ME),因为Java ME中没有java.io.files :(

public static void main(String[] args) {
    // TODO code application logic here

    try
    {
        FTPClient client = new FTPClient();
        client.connect("serveraddy");
        client.login("user", "pass");
        client.upload(new java.io.File("C://text.txt"));
    } catch(Exception e) {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:2)

如果您想要打开并阅读文件,请查看这两个链接

http://developers.sun.com/mobility/apis/articles/fileconnection/index.html
http://developers.sun.com/mobility/midp/articles/genericframework/index.html

以下是打印文件内容的示例...

public void showFile(String fileName) {
   try {
      FileConnection fc = (FileConnection)
         Connector.open("file:///CFCard/" + fileName);
      if(!fc.exists()) {
         throw new IOException("File does not exist");
      }
      InputStream is = fc.openInputStream();
      byte b[] = new byte[1024];
      int length = is.read(b, 0, 1024);
      System.out.println
         ("Content of "+fileName + ": "+ new String(b, 0, length));
   } catch (Exception e) {
   }
}