applet实现大文件FTP上传下载的功能

时间:2021-06-21 17:21:59
 
package yp;

import java.applet.Applet;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter; 

 /**
  * 说明:本APPLET只是测试大文件FTP上传可行性

  * 至于其他功能比如FTP下载、删除、FTP服务器文件列表可调用ContinueFTP相应功能。
  */

public class FileFtpApplet extends Applet  {

 /**
  * Constructor of the applet.
  *
  * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  * returns true.
  */
/* public FileFtpApplet() throws HeadlessException {
  super();
 }*/

 /**
  * Called by the browser or applet viewer to inform
  * this applet that it is being reclaimed and that it should destroy
  * any resources that it has allocated. The <code>stop</code> method
  * will always be called before <code>destroy</code>. <p>
  *
  * A subclass of <code>Applet</code> should override this method if
  * it has any operation that it wants to perform before it is
  * destroyed. For example, an applet with threads would use the
  * <code>init</code> method to create the threads and the
  * <code>destroy</code> method to kill them. <p>
  */
 public void destroy() {
  // Put your code here
 }

 /**
  * Returns information about this applet. An applet should override
  * this method to return a <code>String</code> containing information
  * about the author, version, and copyright of the applet. <p>
  *
  * @return  a string containing information about the author, version, and
  * copyright of the applet.
  */
 public String getAppletInfo() {
  return "This is my default applet created by Eclipse";
 }

 /**
  * Called by the browser or applet viewer to inform
  * this applet that it has been loaded into the system. It is always
  * called before the first time that the <code>start</code> method is
  * called. <p>
  *
  * A subclass of <code>Applet</code> should override this method if
  * it has initialization to perform. For example, an applet with
  * threads would use the <code>init</code> method to create the
  * threads and the <code>destroy</code> method to kill them. <p>
  */
 public void init() {
  // Put your code here
 }

 /**
  * Called by the browser or applet viewer to inform
  * this applet that it should start its execution. It is called after
  * the <code>init</code> method and each time the applet is revisited
  * in a Web page. <p>
  *
  * A subclass of <code>Applet</code> should override this method if
  * it has any operation that it wants to perform each time the Web
  * page containing it is visited. For example, an applet with
  * animation might want to use the <code>start</code> method to
  * resume animation, and the <code>stop</code> method to suspend the
  * animation. <p>
  */
 public void start() {
  // Put your code here
 }

 /**
  * Called by the browser or applet viewer to inform
  * this applet that it should stop its execution. It is called when
  * the Web page that contains this applet has been replaced by
  * another page, and also just before the applet is to be destroyed. <p>
  *
  * A subclass of <code>Applet</code> should override this method if
  * it has any operation that it wants to perform each time the Web
  * page containing it is no longer visible. For example, an applet
  * with animation might want to use the <code>start</code> method to
  * resume animation, and the <code>stop</code> method to suspend the
  * animation. <p>
  */
 public void stop() {
  // Put your code here
 }
    private static final long serialVersionUID = 1L;

    private FileFtpApplet jFrame = null;

    private JButton jFileButton = null;

    public FileFtpApplet() {
   // TODO Auto-generated constructor stub
   jFrame = this;
   this.setSize(496, 260);
   jFileButton = new JButton("打开文件");
   jFrame.add(jFileButton);
   jFileButton.addMouseListener(new java.awt.event.MouseAdapter() {
       public void mouseClicked(java.awt.event.MouseEvent e) {
     // System.out.println("mouseClicked()"); // TODO
     // Auto-generated Event stub mouseClicked()
     JFileChooser jfChooser = new JFileChooser("D:\\..\\..");
     jfChooser.setDialogTitle("打开并上传文件");
     jfChooser.setFileFilter(new FileFilter() {
         @Override
         public boolean accept(File f) {
       if (f.getName().endsWith("dat") || f.isDirectory())
           return true;
       return false;
         }
         @Override
         public String getDescription() {
       // TODO Auto-generated method stub
       return "数据文件(*.dat)";
         }
     });
     int result = jfChooser.showOpenDialog(jFrame);
     if (result == JFileChooser.APPROVE_OPTION) { // 确认打开
 
         File fileIn = jfChooser.getSelectedFile();
 
         if (fileIn.exists()) {
           //JOptionPane.showMessageDialog(jFrame, "OPEN"); // 提示框
         ContinueFTP myFtp = new ContinueFTP();  
         try {  
             long l1 = System.currentTimeMillis();
             System.out.println("begin:"+ l1);
                if (myFtp.connect("10.68.7.182", 21, "a", "a")) {
                    String  remotePath = "/";
           String remoteFile = myFtp.getRemoteFileName(fileIn.getName());
           if (remotePath == null || remotePath.trim().equals(""))
            remotePath = "/";
           //文件扩展名
           String kzNm = "";
           if (remoteFile.indexOf(".")>=0)
            kzNm = remoteFile.substring(remoteFile.indexOf("."));
           String cellCode = "yp";
           boolean isSaveFileName  = false;
           //若不保留原文件,则重新组装远程文件名
           if (!isSaveFileName)
           remoteFile= cellCode+"_"+System.currentTimeMillis()+kzNm;
           //获得远程路径最后一位
           String lastStr = remotePath.substring(remotePath.length()-1);
           if (lastStr.trim().equals("/"))
            remoteFile = remotePath + cellCode + "/" + remoteFile;
           else 
            remoteFile = remotePath + "/" +cellCode + "/" + remoteFile;
                 
                 myFtp.upload(fileIn, remoteFile);
                 myFtp.disconnect(); 
              long l2 = System.currentTimeMillis();
              System.out.println("end:"+ l2);
                 System.out.println("remaining:"+(l2-l1));
                } 
            } catch (Exception e1) {  
                System.out.println("连接FTP出错:"+e1.getMessage());  
            }
       } else {
         }
     } else if (result == JFileChooser.CANCEL_OPTION) {
         System.out.println("Cancel button is pushed.");
     } else if (result == JFileChooser.ERROR_OPTION) {
         System.err.println("Error when select file.");
     }

      }
   }
  );
  } 


    接下来问题:如何把上述JAVA类编译结果打包成jar文件,方法有很多,比如eclipse导出,java自带命令、winrar工具等。

    笔者在打包jar遇到一个问题,只打包上述java类编译结果,报错找不到yp.FileFtpApplet.class,经请教别人采取把所用的commons-net-2.0.jar、commons-net-ftp-2.0.jar解压后与java类编译结果一起打包成一个jar,搞定。


关 
本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/java/javarest/20100127/7319_4.html

2 个解决方案

#1


也不知道现在适用不。applet。导jar包没那么麻烦吧。

#2


applet,早就不接触了,应该用的不多了吧

#1


也不知道现在适用不。applet。导jar包没那么麻烦吧。

#2


applet,早就不接触了,应该用的不多了吧

相关文章