JAVA使用FTP上传下载文件的问题


为了实现 FTP上传下载,大概试了两个方法

sun.net.ftp.FtpClient

org.apache.commons.net

 

一开始使用sun.net.ftp.FtpClient,结果发现唯一的问题是它不可以创建目录,

随后试了下org.apache.commons.net,创建目录倒是没有问题,不过用FTPClient 的storeFile方法存储文件的时候发现文本文件正常,但是存储word 或者是压缩包等等的文件就会损坏,由于时间紧迫,网上也没有找到有效的解决办法,结果最后还是采用sun.net.ftp.FtpClient方法

sun.net.ftp.FtpClient中的sendserver方法可以发送FTP服务器的命令,从而可以通过它发送XMDK命令来创建目录

值得注意的是使用sendserver发送命令后,还应该要解析发回来的返回信息。并不是一个命令发送完后就可以发送下一个命令。有时候要等待到就绪状态才行。

使用ftpclient.readServerResponse来接收服务器发回来的执行结果

 

 

附代码:

sun.net.ftp.FtpClient:

    // 连接ftp服务器
    private void connectServer(String server, String user, String password, String path) throws IOException {
            ftpClient_sun = new FtpClient();
            ftpClient_sun = new FtpClient();
            ftpClient_sun.openServer(server,22);
            ftpClient_sun.login(user, password);

            // path是FTP服务下主目录的子目录
            if (path.length() != 0)
                ftpClient_sun.cd(path);
            ftpClient_sun.sendServer("XMKD test\r\n");
            ftpClient_sun.readServerResponse();
            ftpClient_sun.cd("test");
            // 用2进制上传
            ftpClient_sun.binary();
            System.out.println("登录成功");
       
    }

    // 上传文件;并返回上传文件的信息
    private String upload(FormFile formFile) throws Exception {
        TelnetOutputStream os = null;
        InputStream is = null;
        try {
            os = ftpClient_sun.put("upftp"+getName()+"." + getExtName(formFile.getFileName()));
            is = formFile.getInputStream();
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
        } finally {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
        }
        return "上传文件成功!";
    }
   

    /**
     * 下载文件
     *
     * @param fileName
     *            FormFile对象
     * @param HttpServletResponse
     *            HTTP响应
     * @throws IOException
     */
    private void download(String fileName, HttpServletResponse response) throws IOException {
        TelnetInputStream ftpIn = ftpClient_sun.get(fileName);
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

        OutputStream out = null;
        try {
            out = response.getOutputStream();
            IOUtils.copy(ftpIn, out);
        } finally {
            if (ftpIn != null) {
                ftpIn.close();
            }
        }
    }

    /**
     * 展示图片     *
     * @param fileName
     *            FormFile对象
     * @param HttpServletResponse
     *            HTTP响应
     * @throws IOException
     */
    private void show(String fileName, HttpServletResponse response) throws IOException {
        TelnetInputStream ftpIn = ftpClient_sun.get(fileName);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            IOUtils.copy(ftpIn, out);
        } finally {
            if (ftpIn != null) {
                ftpIn.close();
            }
        }
    }

org.apache.commons.net

    // 连接ftp服务器
    private void connectServer(String server, String user, String password, String path) throws IOException {
        try {
            ftpClient = new FTPClient();
            ftpClient.connect(server);
            ftpClient.login(user, password);
            System.out.println(" login success !!! ");
            if (path.length() != 0) {
              boolean flag = ftpClient.changeWorkingDirectory(path);
              if (flag) {
                System.out.println(" set working directory successful !!! ");
              }else
              {
                  ftpClient.makeDirectory(path);
                  ftpClient.changeWorkingDirectory(path);
              }
            }
          }
          catch (IOException e) {
              System.out.println(" not login !!! ");
              System.out.println(e.getMessage());
          }
    }

    // 上传文件;并返回上传文件的信息
    private String upload(FormFile formFile) throws Exception {
        InputStream fis = formFile.getInputStream();
        // 上传本地文件到服务器上(文件名以'temp_'开头,当上传完毕后,名字改为正式名)
        boolean flag = ftpClient.storeFile("upftp"+getName()+"."+getExtName(formFile.getFileName()), fis);
        if (flag) {
            System.out.println(" upload success !!! ");
        }
        // 关闭文件流
        fis.close();
        return "上传文件成功!";
    }

你可能感兴趣的:(java,exception,String,FTP服务器,null,Path)