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

struts2的配置过程

(1)在项目中加入jar包

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

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">

struts2

org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter

struts2

/*

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

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">

struts2

org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter

struts2

/*

(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页面的编写

文件名:

上传:

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

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

希望与广大网友互动??

点此进行留言吧!

java struts2上传文件_java Struts2框架下实现文件上传功能相关推荐

  1. SSM框架下实现验证码图片验证功能(源码)

    SSM框架下实现验证码图片验证功能 背景图片资源路径 https://download.csdn.net/download/hero_qhz/10322064 一.首先,在pom里面加上需要用的资源j ...

  2. linux删除test文件夹,Linux删除目录下的文件的几种方法

    Linux删除目录下的文件的几种方法 删除当前目录下的文件 1.rm -f * #最经典的方法,删除当前目录下的所有类型的文件 2.find . -type f -delete或find . -typ ...

  3. java 文件上传ssm_ssm框架下实现文件上传

    1.由于ssm框架是使用Maven进行管理的,文件上传所需要的jar包利用pom.xml进行添加,如下所示: 1.3.1 2.4 commons-io commons-io ${commons-io. ...

  4. java form 上传文件_java通过表单进行文件上传的几种方法

    上传文件的分类: 无论什么方式上传文件,都要用post提交 方式一: 前端:表单方式上传文件 后端: 使用上传技术是apache中的Commons-fileupload.jar commons-io. ...

  5. ssm框架下的文件上传和下载

    ssm下的文件上传和下载 1. 文件上传 1.1 文件上传需要的依赖 文件上传需要使用到 commons-fileupload 和 commons-io 两个 jar 包. <dependenc ...

  6. java实现qq空间模块_Java实现模拟QQ空间图片上传

    Java实现模拟QQ空间图片上传 首先看效果: 首先编写我们的上传jsp代码,如下: pageEncoding="UTF-8"%> html PUBLIC "-// ...

  7. java通知图库更新视频_Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能...

    视频网站中提供的在线视频播放功能,播放的都是FLV格式的文件,它是Flash动画文件,可通过Flash制作的播放器来播放该文件.项目中用制作的player.swf播放器. 多媒体视频处理工具FFmpe ...

  8. java安全框架下载文件_java安全框架之Permission学习笔记

    一.permission和security policy java.security.Permission代表一个访问系统资源的系统资源的权限,perm = new java.io.FilePermi ...

  9. java 解压文件_java实现解压zip文件,(亲测可用)!!!!!!

    项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...

最新文章

  1. java display html_css html布局之display属性_动力节点Java学院整理
  2. 2018人工智能行业创新情报白皮书
  3. Laravel5.2之Filesystem源码解析(下)
  4. ios系统web(微信公众号)开发遇到的问题及解决方案
  5. golang设置运行CPU数量及sync.Mutex全局互斥锁的使用示例
  6. jdbc连接Oracle/MySQL数据库进行批量导入操作,如何提高效率???
  7. 好用到爆!轻松获取PNG透明图片!
  8. 求解偏微分方程开源有限元软件deal.II学习--Step 9
  9. @Autowired与@Resource
  10. mysql内连接和左连接的区别_MySQL连接查询 内连接和外连接的区别
  11. 传奇客户端wil和pak文件的目录与作用
  12. 怎样和求职者聊天_我如何学会欣赏求职者
  13. 寒山是一位高僧,拾得是一位和尚。
  14. 机器学习常见模型英文介绍
  15. Linux王冠-RHCE认证概况及费用一览
  16. option样式美化 css,CSS select样式优化
  17. licenses.licx报-1错误
  18. kafka多线程消费
  19. [Go实战]CGO 入门系列-手把手教程4 调用 libevent (c语言类库)为案例
  20. GeneXus开发平台 拖拽低代码 JNPF源码,java开源快速开发平台

热门文章

  1. typeorm mysql_从零搭建项目 (10) --- 后端: 使用 TypeORM 和 MySQL
  2. 微信小程序 App()方法与getApp()方法
  3. 留言板JavaScript实现
  4. springboot+sockjs进行消息推送(群发)
  5. 三维重建9:点云图像的滤波方法小结
  6. 安卓使用ImageView显示OpenCV-Mat
  7. Boost锁~临界区保护和临界资源共享
  8. QT qml---- loader使用方法
  9. LAMP架构(七)配置防盗链,访问控制
  10. spring boot框架学习学前掌握之重要注解(2)-通过java的配置方式进行配置spring