我想在struts1应用程序中上传一个文件。
目前的实现是使用File,如下所示:
<html:file property="upload"/>但是如果app是从远程机器访问的,这不允许上传文件,因为这个小部件只传递文件的名称,而不是整个文件。
发布于 2011-12-01 10:19:00
仅使用<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();
}
}https://stackoverflow.com/questions/8255244
复制相似问题