Struts2文件上传

传统的文件上传:

A) 需要将form的enctype设置为multipart/form-data

此时浏览器将会以二进制的形式处理表单数据,因此不能用request.getParameter获取。

B) 需要启动一个文件上传的组件( SmartUpoload、Common-FileUpload)

C) Servlet通过文件上传组件来获取请求参数来获取上传文件,得到上传文件后以IO流的形式写入磁盘。

 

Servlet3.0以后

         只要增加一个@MultipartConfig修饰Servlet
    接下来就可用request.getParameter来获取请求参数
                   用request.getPart()来获取上传文件。

Servlet通过文件上传组件来获取请求参数来获取上传文件,得到上传文件后以IO流的形式写入磁盘。

 

Struts2以后:

a)  Struts默认使用Jakarta的Common-FileUpload文件上传框架,因此再使用Struts2的上传功能时应在web应用中增加commons-io-xxx.jar与commons-fileupload-xxx.jar。

b)  应该把需要文件上传的表单域所在的表单的enctype属性设置为”multipart/form-data”,让浏览器以二进制流的方式处理表单数据。

……./index.jsp页面

<form action="new/hello.action"  enctype="multipart/form-data"method="post">

 

        文件标题:<input type="text"name="title"/>

        选择文件:<input type="file"name="upload"/>

        <input type="submit"value="上传"/>

</form>

 

c)  处理文件上传的Action类中需要用3个属性来封装文件域的信息:xxxFileName封装该文件域对应的文件名、xxxContentType封装该文件域对应文件的类型、类型为File的xxx属性封装该文件域对应文件的内容。

 

package com.jluzh.up.action;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.UUID;

 

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class HelloWorldAction extendsActionSupport {

         //上传文件名

         privateString title;

         //上传文件

         privateFile upload;

         //上传文件类型

         privateString uploadContentType;

         //上传文件名字

         privateString uploadFileName;

         //上传文件保存路径,使用依赖注入在struts.xml中配置该值

         privateString savePath;

        

        

         publicString getTitle() {

                   returntitle;

         }

 

 

         publicvoid setTitle(String title) {

                   this.title= title;

         }

 

 

         publicFile getUpload() {

                   returnupload;

         }

 

 

         publicvoid setUpload(File upload) {

                   this.upload= upload;

         }

 

 

         publicString getUploadContentType() {

                   returnuploadContentType;

         }

 

 

         publicvoid setUploadContentType(String uploadContentType) {

                   this.uploadContentType= uploadContentType;

         }

 

 

         publicString getUploadFileName() {

                   returnuploadFileName;

         }

 

 

         publicvoid setUploadFileName(String uploadFileName) {

                   this.uploadFileName= uploadFileName;

         }

 

 

         publicString getSavePath() {

                   returnsavePath;

         }

 

 

         publicvoid setSavePath(String savePath) {

                   this.savePath= savePath;

         }

 

 

         @Override

         publicString execute() throws Exception {

                   //TODO Auto-generated method stub

//               Stringfile=ServletActionContext.getServletContext()

//                                    .getRealPath("\\");

                  

                  

                   StringnewName=UUID.randomUUID()+uploadFileName.substring(uploadFileName.lastIndexOf("."));

                   FileInputStreamfis=new FileInputStream(getUpload());

                   FileOutputStreamfos=new FileOutputStream(getSavePath()+"\\"+newName);

                  

                   byte[]buffer=new byte[1024];

                   intlen=0;

                   while((len=fis.read(buffer))>0){

                            fos.write(buffer,0,len);

                           

                   }

                   setUploadFileName(newName);

                   return"success";

         }

        

 

}

 

配置文件上传的Action

struts>

    <constant name="struts.enable.DynamicMethodInvocation"value="false" />

    <constant name="struts.devMode"value="false" />

 

    <package name="default"namespace="/new" extends="struts-default">

   

        <action name="hello"class="com.jluzh.up.action.HelloWorldAction">

<!—使用依赖注入,为Action中的savePath属性赋值-->

        <param name="savePath">F:\\up</param>

        <result name="success">/WEB-INF/context/HelloWorld.jsp</result>

        </action>

    

     <action name="*">

             <result>/WEB-INF/context/{1}.jsp</result>

         </action>

    </package>

 

    <!-- Add packages here -->

 

</struts>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值