首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

下传文件

2012-09-17 
上传文件文件上传采用struts上传文件* 页面的配置,如:form actionupload.do methodpost enctypem

上传文件
文件上传

采用struts上传文件

       * 页面的配置,如:

      <form action="upload.do" method="post" enctype="multipart/form-data">

             标题:<input type="text" name="title"><br>

             文件:<input type="file" name="myfile"><br>

             <input type="submit" value="提交">

      </form>

      * ActionForm中使用FormFile来接收上传的文件

       * 在Action中调用FormFile取得上传文件数据,采用流输出,即完成上传

       * 在struts-config.xml中采用<controller/>标签配置上传参数,在<action-mappings>标签后边,如:<controller maxFileSize="10M"/>

创建

Index.jsp

<%@ page language="java"  pageEncoding="GB18030"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>UploadFile</title>

  </head>

  <body>

    <h1>文件上传</h1>

    <hr>

    <form action="uploadfile.do" method="post" enctype="multipart/form-data">

    Title:<input type="text" name="title"><br>

    文件:<input type="file" name="myfile"><br>

    <input type="submit" value="提交">

    </form>

  </body>

</html>



Upload_success.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"

    pageEncoding="GB18030"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GB18030">

<title>上传成功</title>

</head>

<body>

    上传成功!<br>

    Title:${uploadfile.title }<br>

    文件名:${uploadfile.myfile.fileName }<br>

    文件大小:${uploadfile.myfile.fileSize }

</body>

</html>

UploadFileActionForm.jsp

package com.libo.struts;



import org.apache.struts.action.ActionForm;

import org.apache.struts.upload.FormFile;



public class UploadFileActionForm extends ActionForm {



    private String title;

   

    private FormFile myfile;



    public String getTitle() {

       return title;

    }



    public void setTitle(String title) {

       this.title = title;

    }



    public FormFile getMyfile() {

       return myfile;

    }



    public void setMyfile(FormFile myfile) {

       this.myfile = myfile;

    }

}

UploadAction.jsp

package com.libo.struts;



import java.io.FileOutputStream;



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;



public class UploadAction extends Action {



    @Override

    public ActionForward execute(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)

           throws Exception {

       UploadFileActionForm ufa = (UploadFileActionForm)form;

       System.out.println(ufa.getTitle());

       System.out.println(ufa.getMyfile().getFileName());

       FileOutputStream fos = new FileOutputStream("c:\"+ufa.getMyfile().getFileName());

       fos.write(ufa.getMyfile().getFileData());

       fos.flush();

       fos.close();

       return mapping.findForward("success");

    }

}

Struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>



<!DOCTYPE struts-config PUBLIC

          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">





<struts-config>

    <form-beans>

       <form-bean name="uploadfile" type="com.libo.struts.UploadFileActionForm" />

    </form-beans>

    <action-mappings>

       <action path="/uploadfile"

              type="com.libo.struts.UploadAction"

              name="uploadfile"

              scope="request"

       >

           <forward name="success" path="/upload_success.jsp"/>

       </action>

    </action-mappings>

</struts-config>

热点排行