`

struts2文件上传

阅读更多

1.struts2提供的文件上传方式是:jakarta

2.struts2前端界面:需要注意的是上传需要设置enctype=“multipart/form-data”

   <form action="file/uploadAction.action" method="post" enctype="multipart/form-data">
    <s:textfield name="title" label="标题名称:"></s:textfield>
    <s:file name="upload" label="选择文件:"></s:file>
    <s:submit value="上传"></s:submit>
  </form>

3.action在跳转的时候,struts.xml可以通过参数设置文件上传路径,如果需要修改文件上传路径,可以直接修改配置文件,改配置文件也可以在.properties文件中配置,例如:

<action name="uploadAction" class="com.xxx.action.FileUploadAction" method="fileUpload">
   <param name="savePath">F://file//upload</param>
   <result name="success">/fileUploadTest/uploadSuccess.jsp</result>
 </action>

4.在action类定义一个属性savePath用于获取在struts.xml配置的属性,添加set,get方法,为了获取前台界面传来的title,upload,同样在action中添加title,upload属性和各个的set,get方法。struts在上传文件的时候封装了获取前台界面的文件名称(uploadFileName),文件上传类型(uploadContentType);action的执行方法代码如下:

public String fileUpload() throws IOException{
  try {
   FileOutputStream fos = new FileOutputStream(savePath+"\\"+uploadFileName);
   FileInputStream fis = new FileInputStream(upload);
   byte[] bytes = new byte[1024];
   int leng = 0;
   while((leng=fis.read(bytes))>0){
    System.out.println("----------"+leng);
    fos.write(bytes, 0, leng);
   }
   System.out.println("上传的文件路径为:"+savePath);
   System.out.println("上传的自定义标题名称:"+title);
   System.out.println("文件原始名称:"+uploadFileName);
   System.out.println("文件类型:"+uploadContentType);
   System.out.println("文件上传成功");
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  return SUCCESS;
 } 

 

 

 

分享到:
评论

相关推荐

    struts2文件上传和下载

    struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载

    struts2 文件上传

    struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload

    struts2文件上传实例

    struts2文件上传实例,程序员宝典......

    struts2文件上传jar

    里面包括的是实现struts2文件上传所需要的全部jar包

    Struts2文件上传

    Struts2文件上传

    Struts2 文件上传

    Struts2 文件上传

    Struts2文件上传源码

    Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码

    struts2文件上传与下载

    struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!

    struts2 文件上传功能

    Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...

    struts2文件上传下载源代码

    http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接

Global site tag (gtag.js) - Google Analytics