struts框架解决多文件的上传问题(首次更博……)

来源:互联网 发布:淘宝ulzzang短发女模特 编辑:程序博客网 时间:2024/05/16 09:18

用了CSDN好长时间了,一直都是在看人家的博客,人家的经验,现在第一次在这里写属于自己的博客,最近学习struts2.0框架,,正好学到使用struts框架解决多文件的上传。
好了,,话不多说,,开干!
下面是struts中action中的代码

package com.ruide.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;public class UpLoadAction {    //实现struts框架上传的action必须要有这三个变量    //文件数组    private File []photo;    //文件类型    private String []photoContentType;    //文件名    private String []photoFileName;    //对应的get和set 属性    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public File[] getPhoto() {        return photo;    }    public void setPhoto(File[] photo) {        this.photo = photo;    }    public String[] getPhotoContentType() {        return photoContentType;    }    public void setPhotoContentType(String[] photoContentType) {        this.photoContentType = photoContentType;    }    public String[] getPhotoFileName() {        return photoFileName;    }    public void setPhotoFileName(String[] photoFileName) {        this.photoFileName = photoFileName;    }    //action中execute方法    public String execute()throws Exception{        for (int i = 0; i < photo.length; i++) {            //System.out.println(photo.length);            this.writeFile(photo[i], photoFileName[i]);        }         return null;    }    //writeFile方法,,实现对文件的读写    public void writeFile(File f,String fname)throws Exception{       //获取request对象       HttpServletRequest request=ServletActionContext.getRequest();        //得到"files"的路径         String realpath=request.getRealPath("");        realpath+="\\files\\";        //使用输入流将文件读取出来        InputStream in=new FileInputStream(f);        //创建输出流,然后针对输入流的读取的内容写入文件        OutputStream out=new FileOutputStream(realpath+"\\"+fname);        int a=-1;        while ((a=in.read())!=-1) {            out.write(a);        }        //关闭输入输出流        out.close();        in.close();    } }

然后就是前台的界面了,,当然了,,咱们做后端的也不是很注重前台的一些东西,,所以我就直接写了一个简单的多元化表单,,贴代码……
—-这是upload.jsp中的代码,,就是一个简单的多元化form表单。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'upload.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    <form enctype="multipart/form-data" action="upload.action" method="post">        照片1:<input name="photo" type="file" /><br />        照片2:<input name="photo" type="file" /><br />        照片3:<input name="photo" type="file" /><br />        <input name="photo" type="submit" /><br />    </form>  </body></html>

这些就是整个struts框架解决上传问题的核心代码,,但是呢,,要将前台jsp获取到的数据传递到后端action中进行处理,还是需要一些配置文件的。

下面就是struts的配置文件struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="default" namespace="/" extends="struts-default">        <action name="hello" class="com.ruide.action.HelloAction">            <result name="success" type="redirect">/index.jsp</result>        </action>        <action name="doregister" class="com.ruide.action.DoRegisterAction">        </action>        <action name="up" class="com.ruide.action.UpAction"></action>    </package></struts>

这样呢就可以实现action与jsp之间的联系,能够把jsp接受到的文件交给action处理。
但是呢,我们用的是struts框架,,不要忘了这一点,,必要的配置还是要有的,,比如说struts框架需要的基础性的jar包,,还有就是上传下载所用的jar包,在下面我已经列出来了。。。

  • asm-3.3.jar
  • asm-commons-3.3.jar
  • asm-tree-3.3.jar
  • commons-fileupload-1.3.jar
  • commons-io-2.2.jar
  • commons-lang3-3.1.jar
  • commons-logging-1.1.3.jar
  • freemarker-2.3.19.jar
  • javassist-3.11.0.GA.jar
  • log4j-1.2.17.jar
  • ognl-3.0.6.jar
  • struts2-core-2.3.16.jar
  • xwork-core-2.3.16.jar
    还有就是要在web工程里配置struts框架:
    web.xml配置文件里配置:
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <display-name>Struts Blank</display-name>    <filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <welcome-file-list>        <welcome-file>index.html</welcome-file>    </welcome-file-list></web-app>

对于web.xml的配置,,只要是引入了struts框架,那么这个配置就是一定的,,写死的。

好了,,,至此为之,,这些就是struts框架实现文件的上传下载。

原创粉丝点击