- 浏览: 327070 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
di1984HIT:
谢谢,写的不错。
使用hector操作Cassandra -
mr_von:
非常感谢!
paoding的分词使用 -
howgoo:
http://www.dhtmlx.com/docs/prod ...
dhtmlxGrid分页与排序 -
青春的、脚步:
谢谢
spring-data-mongodb的MongoTemplate 使用小例子 -
青春的、脚步:
xiaofancn 写道青春的、脚步 写道这个能查询都个值在某 ...
spring-data-mongodb的MongoTemplate 使用小例子
新建一个web项目
从官方网站上下载的 struts-2.2.1.1例子(app)struts2-blank解压。
上传文件主要的jar:commons-fileupload-1.2.1.jar
将struts2-blank项目中的lib ,web.xml,struts.xml复制到自己的新建的项目中
修改struts.xml
<!-- 使用缺省的struts的配置文件,struts2的支持jar包: struts2-core-2.0.11.2.jar中 -->
<include file="struts-default.xml"/>
<!-- Struts2常量配置
struts.devMode:开发模式,修改配置信息可以不必重新部署服务器
struts.i18n.encoding:国际化编码
struts.ognl.allowStaticMethodAccess:允许ognl访问静态方法 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="example" namespace="/" extends="struts-default">
<action name="fileupload" class="FileUpload">
<result>/index.jsp</result>
</action>
</package>
- public class FileUpload{
- private File image; //与jsp表单中的名称对应
- private String imageFileName; //FileName为固定格式
- private String imageContentType ; //ContentType为固定格式
- public String execute() throws Exception{
- System.out.println("imageFileName = " +imageFileName);
- System.out.println("imageContentType = " +imageContentType);
- //获取服务器的根路径realpath
- String realpath = ServletActionContext.getServletContext().getRealPath("/images" );
- System.out.println(realpath);
- if (image!= null ){
- File savefile = new File( new File(realpath), imageFileName);
- if (!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
- FileUtils.copyFile(image, savefile);
- ActionContext.getContext().put("message" , "上传成功" );
- }else {
- ActionContext.getContext().put("message" , "上传失败" );
- }
- return "success" ;
- }
- public String getImageContentType() {
- return imageContentType;
- }
- public void setImageContentType(String imageContentType) {
- this .imageContentType = imageContentType;
- }
- public String getImageFileName() {
- return imageFileName;
- }
- public void setImageFileName(String imageFileName) {
- this .imageFileName = imageFileName;
- }
- public File getImage() {
- return image;
- }
- public void setImage(File image) {
- this .image = image;
- }
- }
- <form action= "${pageContext.request.contextPath}/ fileupload.action" enctype= "multipart/form-data" method= "post" >
- 文件:<input type="file" name= "image" >
- <input type="submit" value= "上传" />
- </form>
在上面的基础上略加改动
- public class FileUpload{
- private File[] image;
- private String[] imageFileName;
- private String[] imageContentType ;
- public String execute() throws Exception{
- String realpath = ServletActionContext.getServletContext().getRealPath("/images" );
- System.out.println(realpath);
- if (image!= null ){
- File savedir = new File(realpath);
- if (!savedir.exists())
- {
- savedir.mkdirs();
- }
- System.out.println("image.length = " +image.length);
- for ( int i = 0 ; i<image.length ; i++){
- System.out.println("imageContentType[" +i+ "] = " +imageContentType[i]);
- File savefile = new File(savedir, imageFileName[i]);
- FileUtils.copyFile(image[i], savefile);
- }
- ActionContext.getContext().put("message" , "上传成功" );
- }
- return "success" ;
- }
- public File[] getImage() {
- return image;
- }
- public void setImage(File[] image) {
- this.image = image;
- }
- public String[] getImageFileName() {
- return imageFileName;
- }
- public void setImageFileName(String[] imageFileName) {
- this.imageFileName = imageFileName;
- }
- public String[] getImageContentType() {
- return imageContentType;
- }
- public void setImageContentType(String[] imageContentType) {
- this.imageContentType = imageContentType;
- }
- }
- <form action= "${pageContext.request.contextPath}/fileupload.action" enctype= "multipart/form-data" method= "post" >
- 文件1 :<input type= "file" name= "image" ><br/>
- 文件2 :<input type= "file" name= "image" ><br/>
- 文件3 :<input type= "file" name= "image" ><br/>
- <input type="submit" value= "上传" />
-
</form>
发表评论
-
spring cloud gateway 全局过滤器。
2019-03-17 23:11 498spring cloud gateway 网关 ... -
elasticsearch 5.4搜索
2017-05-19 08:37 148@Test public void t ... -
netty简单的操作memcached
2017-04-19 18:59 950pom文件 <dependency> ... -
juc系列-Executor框架
2017-03-20 13:34 672转自 juc系列-Executor框架 什 ... -
ReentrantLock 类
2017-03-19 21:11 11.1 什么是reentrantlock java.u ... -
juc之CountDownLatch、CyclicBarrier和Semaphore例子
2017-03-19 17:23 802import java.util.concurren ... -
netty客户端与服务端例子
2017-03-12 20:07 2774package com.snailteam.nett ... -
freemaker集成spring
2017-02-26 19:00 536<bean id="freeMark ... -
spring集成Hessian插件配置改成自动注入。
2017-02-10 15:39 809原来的配置 <bean name="/ ... -
web开发模版
2017-02-09 14:42 0# Rules reminder: # DEBUG &l ... -
Java多线程
2017-02-04 14:39 495线程状态图 新建状态(New): ... -
hash冲突解决
2017-01-23 18:30 4311、开放地址法有一个公式: m是hash表长度,di ... -
mybatis +spring 分库
2015-04-30 17:56 353<context:component-scan b ... -
Java并发任务处理例子
2014-02-12 16:06 1106public void init() { su ... -
JAVA动态代理模式
2012-10-03 01:01 1159这是我要实现的效果 public static ... -
paoding的分词使用
2012-06-13 21:30 4849paoding下载 http://code.googl ... -
使用spring-hadoop操作hadoop
2012-05-20 02:12 32参考文章 http://blog.springsource. ... -
solr3.5集成paoding和位置搜索及对solrj的使用。
2012-04-28 02:12 1926http://xiaofancn.iteye.com/blog ... -
日期处理DateUtils
2012-03-24 09:47 4760import java.text.SimpleDateF ... -
Cassandra的数据分布情况测试
2012-03-16 15:09 20071、规划种子节点ip和Token值的对应 4个种子 ...
相关推荐
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
struts2文件上传实例,程序员宝典......
里面包括的是实现struts2文件上传所需要的全部jar包
Struts2文件上传
Struts2 文件上传
Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码
struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!
Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...
http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接