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