从JSP上载文件时java.io.FileNotFoundException
我在应用程序中编写了一个AJAX文件上传功能。在我的笔记本电脑上运行时,它工作得非常好。当我使用相同的应用程序尝试完全相同的文件,但部署在jBoss服务器上时,我得到以下异常:从JSP上载文件时java.io.FileNotFoundException,java,ajax,file,upload,Java,Ajax,File,Upload,我在应用程序中编写了一个AJAX文件上传功能。在我的笔记本电脑上运行时,它工作得非常好。当我使用相同的应用程序尝试完全相同的文件,但部署在jBoss服务器上时,我得到以下异常: 2013-02-18 11:30:02,796 ERROR [STDERR] java.io.FileNotFoundException: C:\Users\MyUser\Desktop\TestFile.pdf (The system cannot find the file specified). getFileD
2013-02-18 11:30:02,796 ERROR [STDERR] java.io.FileNotFoundException: C:\Users\MyUser\Desktop\TestFile.pdf (The system cannot find the file specified).
getFileData方法:
private byte[] getFileData(File file) {
FileInputStream fileInputStream = null;
byte[] bytFileData = null;
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
if (fileInputStream != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytBuffer = new byte[1024];
try {
for (int readNum; (readNum = fileInputStream.read(bytBuffer)) != -1;) {
byteArrayOutputStream.write(bytBuffer, 0, readNum);
}
} catch (IOException e) {
e.printStackTrace();
}
bytFileData = byteArrayOutputStream.toByteArray();
}
return bytFileData;
}
获取变量中的文件内容(通过上述方法):
制作文件:
private boolean makeFile(File folderToMake, File fileToMake, byte[] bytFileData) {
Boolean booSuccess = false;
FileOutputStream fileOutputStream = null;
try {
if (!folderToMake.exists()) {
folderToMake.mkdirs();
}
if (!fileToMake.exists()) {
if (fileToMake.createNewFile() == true) {
booSuccess = true;
fileOutputStream = new FileOutputStream(fileToMake);
fileOutputStream.write(bytFileData);
fileOutputStream.flush();
fileOutputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
booSuccess = false;
}
return booSuccess;
}
有什么想法吗
多谢各位
Charles您似乎只是将文件路径作为请求的一部分传递给服务器,而不是实际上载文件,然后尝试使用该文件路径访问文件 这将在您的笔记本电脑上运行,因为代码在本地运行时,可以访问您的文件系统并能够找到文件。它部署在服务器上无法工作,因为它是一台完全独立的机器,因此无法访问您的文件系统 您需要修改客户端(AJAX)代码以实际上载文件,然后修改服务器端代码以使用上载的文件。请注意,AJAX文件上传通常是不可能的——有一些框架插件,如jQuery,使用变通方法提供此功能 我不是100%,但我认为使用HTML5功能可以正确上传AJAX文件,但浏览器对这方面的支持现在可能会非常差
private boolean makeFile(File folderToMake, File fileToMake, byte[] bytFileData) {
Boolean booSuccess = false;
FileOutputStream fileOutputStream = null;
try {
if (!folderToMake.exists()) {
folderToMake.mkdirs();
}
if (!fileToMake.exists()) {
if (fileToMake.createNewFile() == true) {
booSuccess = true;
fileOutputStream = new FileOutputStream(fileToMake);
fileOutputStream.write(bytFileData);
fileOutputStream.flush();
fileOutputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
booSuccess = false;
}
return booSuccess;
}