以下是主要代码如下
private void setMapButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int showConfirmDialog = JOptionPane.showConfirmDialog(MapPanel.this, "确定要更换地图?", "友情提示", JOptionPane.YES_NO_OPTION);
if (showConfirmDialog == 0) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
String fileName = file.getName().toUpperCase();
if (fileName.endsWith(".JPG") || fileName.endsWith(".JPEG") || fileName.endsWith(".GIF")) {
return true;
} else {
return false;
}
}
}
@Override
public String getDescription() {
return "地图格式(.JPG;.JPEG;.GIF)";
}
});
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
//String mapPath = this.getClass().getResource("img/map").getPath();
String mapPath = MapProcessor.class.getResource("/img/map").getPath();
new File(mapPath + "/" + dao.getMapName()).delete();
File selectedMapFile = fileChooser.getSelectedFile();
File upLoadFile = new File(mapPath + "/map" + selectedMapFile.getName().substring(selectedMapFile.getName().lastIndexOf(".")));
dao.setMapName(upLoadFile.getName());
try {
InputStream inStream = new FileInputStream(selectedMapFile);
OutputStream outStream = new FileOutputStream(upLoadFile);
int readBytes = 0; // 读取字节数
byte[] buffer = new byte[1024]; // 定义缓存数组
while ((readBytes = inStream.read(buffer, 0, 1024)) != -1) {// 从输入流读取数据到缓存数组中
outStream.write(buffer, 0, readBytes); // 将缓存数组中的数据输出到输出流
}
outStream.close();// 关闭输出流对象
inStream.close();// 关闭输入流对象
} catch (Exception ex) {
Logger.getLogger(MapPanel.class.getName()).log(Level.SEVERE, null, ex);
}
MapPanel.this.mapProcessor.replaceMap("/img/map/" + upLoadFile.getName());
slider.setValue(slider.getValue() + 1);
smallMapLabel.refreshScale();
smallMapLabel.setIcon(new ImageIcon(mapProcessor.zoom(200, 200)));
}
}
}
以下是在控制台出现的异常
D:\>java -jar MapModule.jar
五月 02, 2012 5:07:50 下午 com.mwq.map.MapPanel setMapButtonActionPerformed
严重: null
java.io.FileNotFoundException: file:\D:\MapModule.jar!\img\map\map.jpg (文件名、
目录名或卷标语法不正确。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.mwq.map.MapPanel.setMapButtonActionPerformed(MapPanel.java:387)
at com.mwq.map.MapPanel.access$20(MapPanel.java:352)
at com.mwq.map.MapPanel$21.actionPerformed(MapPanel.java:322)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
谢谢啊,分析的恨透侧。我确实是要往jar包里面写数据,那就我当前的目录结构能写进去吗?那要是能写的话,如何获取他的路径呢,或者说怎样构造目录才能写进去呢,在eclipse中调试的获取的路径,打包之后都会出异常。如果像你
“URL url=new URL("jar:file:\D:\MapModule.jar!\img\map\map.jpg");
InputStream is=url.openStream() “
构造的话,那要是目录改了的话也重新要改代码,那在eclipse中肯定就没法运行了
首先谢谢啊,嗯是的,387行就是这段代码里面的,应该是这个mapPath路径的问题,我是通过String mapPath = MapProcessor.class.getResource("/img/map").getPath();获得的路径,在eclipse中获得的路径是/D:/Administrator/workspace/MapModule/bin/img/map
不过打包成.jar文件之后就变成了\D:\MapModule.jar!\img\map\,我看到网上有人说是打包成jar包之后,只能读不能写,也不知道怎么回事
是不是因为你使用的是绝对路径所以找不到,
你用相对路径试试。然后,
jar包只是一种压缩格式,应该不会吧。
ps:我也是菜鸟,看来我帮不了你了。
你在自己折腾吧,祝你好运了。
不太明白,那这个到地该如何获取路径呢,找了各种方法获取路径打成jar包之后总会有异常,总是那几句话有问题