In my client/server app I need to send some file (.txt
, .doc
, etc.) from the client to the server. When I run my code in Eclipse it works, but when I export the signed JAR of the Applet it doesn't. It throws a FileNotFoundException
. I tried saving file in several ways without success.
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;
}
Alternative code used:
FileInputStream inputFile = new FileInputStream(sourceFile);
Or
InputStream inputFile = ClassLoader.class.getResourceAsStream(""+sourceFile);
Or
InputStream inputFile = FileSaving.class.getResourceAsStream(""+sourceFile);
Original code and every alternative work in Eclipse and stop working when exported.
java.io.tmpdir
oruser.home
. Not try to find them inside .jar, because it's useless. – Tumble