【发布时间】:2012-08-15 01:50:22
【问题描述】:
在我的客户端/服务器应用程序中,我需要将一些文件(.txt、.doc 等)从客户端发送到服务器。当我在 Eclipse 中运行我的代码时它可以工作,但是当我导出 Applet 的签名 JAR 时它不会。它抛出一个FileNotFoundException。我尝试以多种方式保存文件,但均未成功。
public static boolean saveFile(File sourceFile) throws IOException {
DirectoryChooserDialog dialog = new DirectoryChooserDialog();
filePath = dialog.getDestinationFolder();
if (filePath != null) {
InputStream inputFile = ClassLoader.getSystemResourceAsStream(""+sourceFile);
filePath += File.separator + sourceFile.getName();
FileOutputStream outputFile = new FileOutputStream(filePath);
int byteLetti = 0;
while ((byteLetti = inputFile.read(buffer)) >= 0) {
outputFile.write(buffer, 0, byteLetti);
outputFile.flush();
}
inputFile.close();
outputFile.close();
return true;
} else
return false;
}
使用的替代代码:
FileInputStream inputFile = new FileInputStream(sourceFile);
或者
InputStream inputFile = ClassLoader.class.getResourceAsStream(""+sourceFile);
或者
InputStream inputFile = FileSaving.class.getResourceAsStream(""+sourceFile);
原始代码和所有替代代码都可以在 Eclipse 中工作,并在导出时停止工作。
【问题讨论】:
-
如果您的应用程序动态创建数据,您应该使用系统特定的存储来存储应用程序数据。某种
java.io.tmpdir或user.home。不要试图在 .jar 中找到它们,因为它没有用。
标签: java jar applet inputstream filenotfoundexception