我想在struts1应用程序中上传文件。
目前的实现是使用文件,如下所示:
<html:file property="upload"/>
但是,如果从远程计算机访问应用程序,则不允许上传文件,因为此小部件仅传递文件名而不是整个文件。
最佳答案
仅使用 <html:file property="upload" /
> 不会让您的应用程序上传文件。
要支持上传功能,您的表单必须具有 enctype="multipart/form-data"
<html:form action="fileUploadAction" method="post" enctype="multipart/form-data">
File : <html:file property="upload" />
<br/`>
<html:submit />
</html:form`>
并实际从表单 bean 获取文件并按如下方式操作它
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();
}
}
关于java - 在struts1中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255244/