struts2之文件上传
一、单文件上传
实例:
表单应该注意三个点 form中的method="post"、enctype="multipart/form-data"、input中的type="file"
fileForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>File Operation</title>
</head>
<body>
<form action="file/fileOperation_upload" method="post"
enctype="multipart/form-data">
<label for="myFile">Upload your file</label><br />
<input type="file" name="myFile">
<input type="submit" value="上传">
</form>
</body>
</html>
struts.xml中配置文件
<package name="file" namespace="/file" extends="struts-default">
<action name="fileOperation_*" class="com.lz.web.action.FileOperation" method="{1}" >
<result name="upload">/WEB-INF/pages/file.jsp</result>
<result name="error">/WEB-INF/pages/error.jsp</result>
</action>
</package>
FileOperationAction.java
package com.lz.web.action; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial")
public class FileOperation extends ActionSupport{ private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath; public File getMyFile() {
return myFile;
} public void setMyFile(File myFile) {
this.myFile = myFile;
} public String getMyFileContentType() {
return myFileContentType;
} public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
} public String getMyFileFileName() {
return myFileFileName;
} public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
} public String upload(){ destPath= ServletActionContext.getServletContext().getRealPath("/files");//文件保存的路径
System.out.println("realpath: "+destPath);
System.out.println("Src filename: "+myFile);
System.out.println("Dest filename: "+myFileFileName);
try {
File destFile = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);//拷贝文件
} catch (IOException e) {
e.printStackTrace();
return "error";
} return "upload";
}
public String prefile(){ return "prefile";
}
}
file.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
your have successfully uploaded <s:property value="myFileFileName"/>
</body>
</html>
二、多文件上传
fileForm2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>File Operation</title>
</head>
<body>
<form action="file/multiFileUploadAction_upload" method="post"
enctype="multipart/form-data">
文件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>
</body>
</html>
struts.xml中配置
<package name="mfile" namespace="/file" extends="struts-default">
<action name="multiFileUploadAction_*" class="com.lz.web.action.MultiFileUploadAction" method="{1}">
<result name="load">/WEB-INF/pages/fileForm2.jsp</result>
<result name="upload2">/WEB-INF/pages/file2.jsp</result>
</action>
</package>
MultiFileUploadAction.java
package com.lz.web.action; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial")
public class MultiFileUploadAction extends ActionSupport{ private File[] image;
private String[] imageFileName;
private String[] imageContentType; 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;
}
public String load(){
return "load";
}
public String upload(){ String realPath = ServletActionContext.getServletContext().getRealPath("/files");//指定文件保存的路径
System.out.println("realpath: "+realPath);
try {
if(image!=null){
File savedir = new File(realPath);
if(!savedir.getParentFile().exists())//判断文件目录是否存在
savedir.getParentFile().mkdirs();
for(int i=0; i<image.length; i++){
File saveFile = new File(realPath, imageFileName[i]);
FileUtils.copyFile(image[i], saveFile);//文件复制
}
}
} catch (IOException e) {
e.printStackTrace();
}
return "upload2";
} }
file2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
上传成功<br/>
<s:iterator value="imageFileName" var="ifn">
<s:property value="ifn"/>
</s:iterator>
</body>
</html>
struts2之文件上传的更多相关文章
- struts2的文件上传
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...
- jsp\struts1.2\struts2 中文件上传(转)
jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...
- Struts2+Uploadify文件上传使用详解
Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例是php版本的,本文将详细介绍Uploadify在java中的使用,您也可以点击下面的链接进行演示或下 ...
- Struts2 多文件上传
Struts2多文件上传只需要将 单文件上传中的File变成File[] 即可,上篇文章:单文件上传 <form action="${pageContext.request.cont ...
- Struts2图片文件上传,判断图片格式和图片大小
1. 配置Struts2能够上传的最大文件大小 使用Struts2进行文件上传的时候,Struts2默认文件大小最大为2MB,如果要传大一点的文件,就需要修改struts.xml配置文件,重新设置能够 ...
- Struts2实现文件上传下载功能(批量上传)
今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...
- Struts2实现文件上传(四)
Struts2实现文件上传 配置文件struts.xml <!-- /* * $Id: struts.xml 1364077 2012-07-21 12:57:02Z lukaszlenart ...
- Struts2实现文件上传(三)
Struts2实现文件上传 配置文件web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...
- Struts2实现文件上传(二)
Struts2实现文件上传 文件上传页面 file.jsp: <%@ page language="java" import="java.util.*" ...
- Struts2实现文件上传(一)
Struts2实现文件上传 文件上传成功后结果页面 result.jsp: <%@ page language="java" import="java.util.* ...
随机推荐
- Git学习第一天--安装Git和创建版本库
Windows上安装Git msysgit是Windows版的Git,从https://git-for-windows.github.io下载(备份:百度网盘),然后按默认选项安装即可. 安装完成后, ...
- Nginx+php+mysql+wordpress搭建自己的博客站点
服务器环境要求Centos 6 或以上版本(由于我们的目标是半小时内搭建好,那就选简单yum安装)MySQL 5或更新版本Nginx 1或更新版本PHP 5 或更新版本 php-fpm 5或更新版本 ...
- kubernetes基础架构及原理
kubernetes简称“k8s” 其中“8”代表的是“k”和“s”中间的8个字母. k8s是Google公司开发的Borg项目中独立出来的容器编排工具,然后将其捐献给CNCF这个组织,然后发扬光大. ...
- scrapy--json(喜马拉雅Fm)
已经开始听喜马拉雅Fm电台有2个月,听里面的故事,感觉能听到自己,特别是蕊希电台,始于声音,陷于故事,忠于总结.感谢喜马拉雅Fm陪我度过了这2个月,应该是太爱了,然后就开始对Fm下手了.QAQ 该博客 ...
- C语言字符篇(三)字符串比较函数
#include <string.h> int strcmp(const char *s1, const char *s2); 比较字符串s1和s2 int strncmp(const ...
- C++封装的全部总结
类 类是对现实生活中一类具有共同特征的事物的抽象 类是面向对象程序设计实现信息封装的基础. 类是一种用户定义类型,也称类类型. 类的实例称为对象. 类的实质是一种数据类型 面向对象原则 以对象为中心, ...
- perl连接mysql数据库
首先需要安装 ppm install DBD::mysql use strict; use DBI; my $host = "localhost"; # 主机地址 my $driv ...
- Android 创建 SO 文件
创建工程,新建一个类,该类需要有一个static初始化块中调用System.loadLibrary("${soName}"),还需要有用native修饰的方法声明(无需实现),一个 ...
- Android 自定义圆形图片 CircleImageView
1.效果预览 1.1.布局中写自定义圆形图片的路径即可 1.2.然后看一看图片效果 1.3.原图是这样的 @mipmap/ic_launcher 2.使用过程 2.1.CircleImageView源 ...
- 1911: [Apio2010]特别行动队(斜率优化)
链接 思路 斜率优化dp. 代码 #include<cstdio> #include<algorithm> #include<cstring> #include&l ...