您的位置:首页 > 编程语言 > Java开发

Struts2文件上传

2016-05-26 12:24 357 查看
传统的文件上传:

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Struts2