用java打war包的程序

angues1980 2005-08-11 02:20:45
修改了别人的程序做了个swing界面,回去打个jar包就可以当个小工具用了 ^_^

package com.angus.util;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.jar.*;

public class MakeWAR extends JFrame implements ActionListener {

public static void main(String[] args) {
new MakeWAR();
}

public MakeWAR() {
super("WAR Maker");
warTextField = new JTextField(20);
warFileField = new JTextField(20);
JPanel mainPanel = new JPanel();
mainPanel.setBorder(new EmptyBorder(3, 3, 3, 3));
mainPanel.setLayout(new BoxLayout(mainPanel, 1));
JPanel dirPanel = new JPanel();
JPanel targetPanel = new JPanel();
dirPanel.setBorder(BorderFactory
.createTitledBorder("Enter a Directory to Make Into a WAR"));
targetPanel.setBorder(BorderFactory
.createTitledBorder("Enter a WAR File Name"));
dirPanel.add(warTextField);
targetPanel.add(warFileField);
JPanel panel3 = new JPanel();
panel3.setBorder(new EmptyBorder(5, 5, 5, 5));
panel3.setLayout(new BorderLayout());
JButton startButton = new JButton("Create WAR");
startButton.addActionListener(this);
panel3.add(startButton, "East");
mainPanel.add(dirPanel);
mainPanel.add(targetPanel);
mainPanel.add(panel3);
getContentPane().add(mainPanel);
pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = getSize();
setLocation((screen.width - size.width) / 2,
(screen.height - size.height) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

}

public static void makeZip(File directory, File zipFile)
throws IOException, FileNotFoundException {
jos = new JarOutputStream(new FileOutputStream(zipFile), new Manifest());
String fileNames[] = directory.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++)
recurseFiles(new File(directory, fileNames[i]), "");

}
jos.close();
}

private static void recurseFiles(File file, String pathName)
throws IOException, FileNotFoundException {
if (file.isDirectory()) {
pathName = pathName + file.getName() + "/";
jos.putNextEntry(new JarEntry(pathName));
String fileNames[] = file.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++)
recurseFiles(new File(file, fileNames[i]), pathName);

}
} else {
JarEntry jarEntry = new JarEntry(pathName + file.getName());
System.out.println(pathName + " " + file.getName());
FileInputStream fin = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(fin);
jos.putNextEntry(jarEntry);
while ((len = in.read(buf)) >= 0)
jos.write(buf, 0, len);
in.close();
jos.closeEntry();
doneFileCount++;
progressBar.setValue(doneFileCount);
}

}

public void actionPerformed(ActionEvent e) {
final File warDir = new File(warTextField.getText());
String wfStr1 = new String(warFileField.getText());
String wfStr2;
int temp = wfStr1.trim().lastIndexOf(".");
if (temp != -1) {
wfStr2 = wfStr1.substring(0, temp);
} else {
wfStr2 = wfStr1;
}
final File warFile = new File(wfStr2 + ".war");
if (!(new File(warDir, "WEB-INF")).exists()) {
String msg = "The directory you chose does not appear to be a valid\nweb application directory. Please choose another.";
JOptionPane.showMessageDialog(MakeWAR.this, msg, "Error!", 0);
} else if (wfStr2.trim().equals("")) {
String msg = "The WAR file name should be entered";
JOptionPane.showMessageDialog(MakeWAR.this, msg, "Error", 0);
} else {
countFiles(warDir);
progressBar = new JProgressBar(0, totalFileCount);
progressBar.setValue(0);
progressBar.setStringPainted(true);
setVisible(false);
getContentPane().removeAll();
JPanel panel = new JPanel();
panel.add(progressBar);
getContentPane().add(panel);
pack();
setVisible(true);

Thread t = new Thread() {

public void run() {
try {
MakeWAR.makeZip(warDir, warFile);
String msg = "The WAR file was successfully written to: "
+ warFile.getCanonicalPath();
JOptionPane.showMessageDialog(MakeWAR.this, msg,
"WAR Completed", -1);
} catch (Exception e) {
System.err.println(e);
}
System.exit(0);
}
};
t.start();
}
}

private static void countFiles(File file) {
if (file.isDirectory()) {
String fileNames[] = file.list();
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++)
countFiles(new File(file, fileNames[i]));

}
} else {
totalFileCount++;
}
}

private static JarOutputStream jos;
private static byte buf[] = new byte[1024];
private static int len;
private JTextField warTextField;
private JTextField warFileField;
private static int totalFileCount;
private static int doneFileCount;
private static JProgressBar progressBar;
}
...全文
370 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
angues1980 2005-08-15
  • 打赏
  • 举报
回复
这个实用价值不高,不过研究价值比较高嘛 ^_^
liuquanyi 2005-08-15
  • 打赏
  • 举报
回复
同上
superslash 2005-08-15
  • 打赏
  • 举报
回复
看看先
xmadan 2005-08-14
  • 打赏
  • 举报
回复
不是只要
jar cvf aaa.war *.*
就搞定了吗?(应该没写错吧)
这么麻烦做什么
ccm1980 2005-08-13
  • 打赏
  • 举报
回复
good,mark
xdy3008 2005-08-13
  • 打赏
  • 举报
回复
up 学习啊
ocean放飞 2005-08-12
  • 打赏
  • 举报
回复
谢谢啦!!!!!
angues1980 2005-08-12
  • 打赏
  • 举报
回复
大部分用来写界面了啊
zhouqi724 2005-08-12
  • 打赏
  • 举报
回复
写的冗余啊,大哥
zrla 2005-08-12
  • 打赏
  • 举报
回复
jf吧
interhanchi 2005-08-11
  • 打赏
  • 举报
回复
jf
watcher_shen 2005-08-11
  • 打赏
  • 举报
回复
mark
xiaohuasz 2005-08-11
  • 打赏
  • 举报
回复
收藏
luotitan 2005-08-11
  • 打赏
  • 举报
回复
不错,学习
sunhw2002 2005-08-11
  • 打赏
  • 举报
回复
学习
收藏了
edward0716 2005-08-11
  • 打赏
  • 举报
回复
谢谢
heason 2005-08-11
  • 打赏
  • 举报
回复
太牛了
洪泉 2005-08-11
  • 打赏
  • 举报
回复
不错,顶!
fanqingfeng 2005-08-11
  • 打赏
  • 举报
回复
收藏
www.fqf.com.cn
全粘架构师 2005-08-11
  • 打赏
  • 举报
回复
看了,不错,不过习惯用myeclipse打包部署了。
支持原创!
加载更多回复(1)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧