我想在struts1應用程序中上傳文件。在struts1中上傳文件
目前的實施是使用文件,像這樣:
<html:file property="upload"/>
但作爲這個小工具傳遞文件,而不是整個文件的唯一名稱這不允許上傳文件,如果應用程序從遠程計算機訪問。
我想在struts1應用程序中上傳文件。在struts1中上傳文件
目前的實施是使用文件,像這樣:
<html:file property="upload"/>
但作爲這個小工具傳遞文件,而不是整個文件的唯一名稱這不允許上傳文件,如果應用程序從遠程計算機訪問。
只使用<html:file property="upload" /
>不會讓您的應用程序上傳文件。
支持上傳功能,您的表格必須由您的form bean中有ENCTYPE = 「的multipart/form-data的」
<html:form action="fileUploadAction" method="post" enctype="multipart/form-data">
File : <html:file property="upload" />
<br/`>
<html:submit />
</html:form`>
,並在行動中獲取文件,並對其進行操作如下
YourForm uploadForm = (YourForm) form;
FileOutputStream outputStream = null;
FormFile file = null;
try {
file = uploadForm.getFile();
String path = getServlet().getServletContext().getRealPath("")+"/"+file.getFileName();
outputStream = new FileOutputStream(new File(path));
outputStream.write(file.getFileData());
}
finally {
if (outputStream != null) {
outputStream.close();
}
}
請不要將上傳的文件保存在展開的WAR文件夾中。即使在重新啓動服務器時(因爲它們不包含在原始WAR文件中),重新部署WAR或某些服務器時,它們都會丟失。更甚者,當服務器配置爲在內存中而不是在磁盤上擴展WAR時,getRealPath()將返回null,並且所有內容都將中斷。只是不要那樣做。將它們存儲在固定磁盤文件系統位置中,或者存儲在數據庫中的最高位置。 – BalusC
文件:html:file property =「upload」 - current impl – user762421
您不應該對文件名感興趣,而應該對文件內容感興趣。另請參閱http://stackoverflow.com/questions/81180/how-to-get-the-file-path-from-html-input-form-in-firefox-3/3374408#3374408使其成爲'multipart/form數據「表單。 – BalusC
我不完全確定你的意思;該文件作爲'ActionForm'中的'FormFile'元素可用。你能提供你認爲不起作用的代碼嗎? –