上传一个文件在struts1 [英] Uploading a file in struts1

查看:102
本文介绍了上传一个文件在struts1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在struts1应用程序中上传一个文件。



目前的实现是使用File,如下所示:

 < ; html:file property =upload/> 

但是,如果从远程机器访问应用程序,则不允许上传文件,文件的名称,而不是整个文件。 使用< html:file property = 上传/ >不会让你的应用程序上传文件。



为了支持上传功能,你的表单必须有enctype =multipart / form-data

 < html:form action =fileUploadActionmethod =postenctype =multipart / form-数据> 
档案:< html:file property =upload/>
< br /`>

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

在动作中从你的表单bean获取文件,并按照如下操作:

  YourForm uploadForm =(YourForm)form; 
FileOutputStream outputStream = null;
FormFile file = null;
尝试{
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();
}
}


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.

解决方案

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();
  }
}

这篇关于上传一个文件在struts1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆