Uploading a file in struts1

北城余情 提交于 2019-12-08 07:33:52

问题


I want to upload a file in struts1 application.

Currently the implementation is using File, like this:

<html:file property="upload"/>

But this does not allow to upload file if app is accessed from remote machine as this widget passes only the name of the file instead the whole file.


回答1:


using only <html:file property="upload" /> will not make your application to upload a file.

to support upload functionality,your form must have enctype="multipart/form-data"

<html:form action="fileUploadAction" method="post" enctype="multipart/form-data">
File : <html:file property="upload" /> 
<br/`>

<html:submit />
</html:form`> 

and in action get file from your form bean and manipulate it as follows

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/uploading-a-file-in-struts1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!