欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java Struts2框架下实现文件上传功能

程序员文章站 2024-03-12 20:58:32
本文实例为大家分享了struts2框架实现文件上传的方法,供大家参考,具体内容如下 struts2的配置过程 (1)在项目中加入jar包    ...

本文实例为大家分享了struts2框架实现文件上传的方法,供大家参考,具体内容如下

struts2的配置过程

(1)在项目中加入jar包

 java Struts2框架下实现文件上传功能

 

(2)web.xml中filter(过滤器)的配置

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name></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>
</web-app>
 

(3)struts.xml配置文件的编写

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></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>
</web-app>

(4)action类的编写

package com.xmgc.sc.action;
 
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
 
 
import org.apache.struts2.servletactioncontext;
 
public class myuploadaction {
 
  private string title;
  private file upload;//与form表单中的file文件类型的名字是一样的
  private string uploadcontenttype;//前缀必须要是上面的upload
  private string uploadfilename;
   
 
  public string gettitle() {
    return title;
  }
 
  public void settitle(string title) {
    this.title = title;
  }
 
  public file getupload() {
    return upload;
  }
 
  public void setupload(file upload) {
    this.upload = upload;
  }
 
  public string getuploadcontenttype() {
    return uploadcontenttype;
  }
 
  public void setuploadcontenttype(string uploadcontenttype) {
    this.uploadcontenttype = uploadcontenttype;
  }
 
  public string getuploadfilename() {
    return uploadfilename;
  }
 
  public void setuploadfilename(string uploadfilename) {
    this.uploadfilename = uploadfilename;
  }
 
/* public string getsavepath() {
    //servletcontext cxt=servletactioncontext.getservletcontext();
    //string path=cxt.getrealpath("/");
    //这个获取的path为:http://localhost:8080/sc   
    //上传以后变为e:\software\apache-tomcat-6.0.45\webapps\sc
     
     
    return savepath;
     
  }
 
  public void setsavepath(string savepath) {
    //e:\software\apache-tomcat-6.0.45\webapps\sc/myupload
    this.savepath = servletactioncontext.getservletcontext().getrealpath("/myupload");
  }*/
   
   
  public string execute() throws ioexception{
     
    system.out.println(title);//标题
    system.out.println(uploadcontenttype);//准备上传的文件的文件类型
    system.out.println(uploadfilename);//准备上传的文件的文件名,记住还有扩展名
    system.out.println(upload);
     
    string realpath=servletactioncontext.getservletcontext().getrealpath("/");
     
    string path=realpath+"myupload/"+uploadfilename;
     
    system.out.println(realpath);
    system.out.println(path);
     
    fileinputstream fis=new fileinputstream(upload);
    fileoutputstream fos=new fileoutputstream(path);
     
    byte[] bytes=new byte[1024];//定义一个1024大小的字节数组
    int len=-1;//用来做标志位
     
     
    while((len=fis.read(bytes))>0){
      fos.write(bytes, 0, len);
    }
     
    return null;
  }
}

 (5)jsp页面的编写

<%@ page contenttype="text/html;charset=utf-8"%>
 
<!-- enctype="multipart/form-data",这个是最重要的配置 -->
<form action="myupload.action" enctype="multipart/form-data" method="post">
   文件名:<input type="text" name="title"/><br/>
     上传:<input type="file" name="upload"/><br/>
    <input type="submit" value="上传"/>
</form>
  

经过这次总结,感觉struts2框架下的单文件的上传还是非常简单的,只要获取要存储在什么地方的地址,再通过输入输出流,写到这个地址中去就行了。绝大部分工作,struts2已经帮我们做好了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。