Struts文件上传功能的实现

来源:互联网 发布:ubuntu怎么卸载jdk 编辑:程序博客网 时间:2024/04/28 03:51

工具:myeclipse 6.5

1.
新建项目StrutsFileUpload

导入struts1.x的属性包:

struts-config.xml里面新建一个actionformfileupload

struts-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTDStruts Configuration 1.3//EN""http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
<form-beans >
    <form-bean name="fileuploadForm"type="com.shiryu.form.FileuploadForm" />
</form-beans>


<global-exceptions />
<global-forwards />
<action-mappings >
    <action
      attribute="fileuploadForm"
      input="/fileupload.jsp"
      name="fileuploadForm"
      path="/fileupload"
      scope="request"
      type="com.shiryu.action.FileuploadAction">
      <forward name="index"path="/index.jsp" />
    </action>

  
</action-mappings>

<message-resources parameter="com.shiryu.ApplicationResources"/>
</struts-config>

2.com.shiryu.action
包下的FileuploadAction.java

package com.shiryu.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.shiryu.form.FileuploadForm;

public class FileuploadAction extends Action {
   
    public ActionForward execute(ActionMapping mapping,ActionForm form,
            HttpServletRequestrequest, HttpServletResponse response) {

        FileuploadForm fileuploadForm =(FileuploadForm) form;

        FormFileupfile=fileuploadForm.getFilecontext();

        //
文件上传功能实现
        try {

            byte[]buf=upfile.getFileData();

               OutputStream out=new FileOutputStream(newFile("d://"+fileuploadForm.getFilecontext().toString()));

               out.write(buf);

               out.close();
               
        } catch (FileNotFoundException e) {
               e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        returnmapping.findForward("index");
    }
}

3.com.shiryu.form
包下的FileuploadForm.java

package com.shiryu.form;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;


public class FileuploadForm extends ActionForm {
   
    private static final long serialVersionUID =5572854553277148095L;

    
//类型是FormFile
    private FormFile filecontext;

    private String filename;

    public FormFile getFilecontext() {
        return filecontext;
    }

    public String getFilename() {
        return filename;
    }


    public void setFilename(String filename) {
        this.filename = filename;
    }

    public void setFilecontext(FormFile filecontext) {
        this.filecontext = filecontext;
    }
}


fileUpload.jsp

<%@ pagelanguage="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean"prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html"prefix="html"%>

<html>
    <head>
        <title>JSP forFileuploadForm</title>
    </head>
    <body>

       
<html:form action="/fileupload"enctype="multipart/form-data">

            filename :<html:text property="filename"/><html:errorsproperty="filename"/><br/>

            filecontext :<html:file property="filecontext"/><html:errorsproperty="filecontext"/><br/>

           <html:submit/><html:cancel/>

        </html:form>

    </body>
</html>