applet实现大文件ftp上传(二)

package yp;

import java.applet.Applet;

import java.io.File;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

 

public class FileFtpApplet extends Applet  {

 

 

 public void destroy() {

  // Put your code here

 }

 

 public String getAppletInfo() {

  return "This is my default applet created by Eclipse";

 }

 

 public void init() {

  // Put your code here

 }

 

 public void start() {

  // Put your code here

 }

 

 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,搞定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值