0
ファイルアップロード:FileNotFoundException
ファイルのアップロードを行うプログラムを作っているのですが、たまに、FileNotExceptionエラーが出ることがあります。
(※できる場合もあります。
どんな時に発生するのは、把握できません・・・。実行するたび違うので。)
対処方法がわかる方がいましたら、
教えてください。
エラー内容は、
----------------
java.io.FileNotFoundException: XXXXXX (指定されたパスが見つかりません。)
06/12/05 10:20:32 at java.io.FileOutputStream.open(Native Method)
06/12/05 10:20:32 at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
06/12/05 10:20:32 at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
-----
public synchronized void executeMultiFile(
ActionMapping mapping,
HttpServletRequest request,
HttpServletResponse response,
String outFilename,
String outDir,
FormFile file
) throws FileUploadException {
if (file == null) {
throw new FileUploadException(0);
}
String fileName = file.getFileName();
this.setFileSize(file.getFileSize());
String data = null;
if (!fileName.equals("")) {
try {
InputStream stream = file.getInputStream();
File directory = new File(outDir);
if (!directory.exists()) {
// ディレクトリがない場合、新規作成
directory.mkdirs();
}
File attachFile = new File(directory, outFilename);
OutputStream bos = new FileOutputStream(new File(attachFile
.getAbsolutePath()));
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data = "The file has been written to \""
+ attachFile.getAbsolutePath() + "\"";
System.out.println(data);
//close the stream
stream.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
this.setFileSize(-1);
System.out.println("File Not Found Exception, " + "fileName = "
+ fileName + ";");
throw new FileUploadException(1);
} catch (IOException ioe) {
this.setFileSize(-1);
System.out.println("IO Exception");
throw new FileUploadException(2);
}
} else {
this.setFileSize(-1);
throw new FileUploadException(1);
}
//destroy the temporary file created
file.destroy();
}