Struts2 单个文件上传/多文件上传
1导入struts2-blank.war所有jar包:\struts-2.3.4\apps\struts2-blank.war
单个文件上传
upload.jsp
<s:form action="upload2.action" method="post" theme="simple" enctype="multipart/form-data">
<tr>
<td id="more">
选择上传文件:<s:file name="file"></s:file><br>
<s:submit type="button" value="submit"/>
</td>
</tr>
</s:form>
struts.xml
<package name="struts2" extends="struts-default">
<action name="upload2"class="com.hloytax.wg.upload.UploadAction1">
<result name="success">/success.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">409600</param> //上传文件大小设置
// <!--allowedTypes (可选) - 以逗号分割的contentType类型列表(例如text/html), <param name="contentType"> application/txt; </param>
<param name="allowedTypes">
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
UploadAction1.action
public class UploadAction1 extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private File file;
//文件名称
private String fileFileName;
//文件类型
private String fileContentType;
//注意:文件名称和文件类型的名称前缀必须相同,
省略get set 方法
@Override
public String execute() throws Exception{
//获取需要上传文件的文件路径
File uploadFile=new File(ServletActionContext.getServletContext().getRealPath("uploadFile"));
//判断文件是否上传,如果上传的话将会创建该目录
if(!uploadFile.exists()){
uploadFile.mkdir(); //创建该目录
}
/*//第一种文件上传的方法
//声明文件输入流,为输入流指定文件路径
FileInputStream input=new FileInputStream(file);
//获取输出流,获取文件的文件地址及名称
FileOutputStream out=new FileOutputStream(uploadFile + "\\" +fileFileName);
try{
byte[] b=new byte[1024];//每次写入的大小
int i=0;
while((i=input.read(b))>0){
out.write(b,0,i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
input.close();
out.close();
}
*/
//第二种文件上传的方法
//FileUtils.copyFile(file,new File(uploadFile+"\\"+fileFileName));
// FileUtils.copyFile(file,new File(uploadFile,fileFileName));
// System.out.println(uploadFile);
//第三种方法
BufferedReader bReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
BufferedWriter bWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uploadFile+"\\"+fileFileName)));
System.out.println(uploadFile);
try{
char[] str=new char[1024];
int i=0;
while((i=bReader.read(str))>0){
bWriter.write(str,0,i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
bReader.close();
bWriter.close();
uploadFile.delete();
}
return SUCCESS;
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<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>
多文件上传:
private List<File> file;
private List<String> fileContentType;
private List<String> fileFileName;
private String savePath;
省略get set 方法 上传方法 参照单文件上传
@Override
public String execute() throws Exception {
List<File> files= getFile();
if (files !=null) {
for (int i = 0; i < files.size(); i++) {
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName().get(i));
//建立上传文件的输入流
System.out.println(getSavePath());
FileInputStream fis = new FileInputStream(files.get(i)); byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
}
return SUCCESS;
} /**
* 返回上传文件保存的位置
*
* @return
* @throws Exception
*/
public String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
Struts2 单个文件上传/多文件上传的更多相关文章
- struts2实现文件上传(多文件上传)及下载
一.要实现文件上传,需在项目中添加两个jar文件 二.上传准备的页面 注:必须植入enctype="multipart/form-data"属性,以及提交方式要设置成post &l ...
- struts2 s:file标签使用及文件上传例子
<s:form action="uploadaction" method="post" enctype="multipart/form-da ...
- Struts2文件上传--多文件上传(插件uploadify)
公司需要把以前的Struts2自带的图片上传替换掉,因为不能一个file选择多个文件,本人直接百度搜索图片插件, 貌似就它(uploadify3.2.1)在最前面,也找过很多案例, 其中有不少问题, ...
- Struts2之文件上传(单文件/多文件)
<一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...
- ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案
摘要: ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案 在struts2应用中使用ueditor富文本编辑器上传图片或者附件时,即使配置 ...
- struts2文件上传,文件类型 allowedTypes
struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...
- struts2.1.6教程九、文件上传下载(了解)
首先建立struts2UpDownLoad项目,搭建好struts2基本的开发环境. 上传实例 步骤一:upload.jsp代码如下: <s:form action="upload&q ...
- struts2 上传下载文件,异常处理,数据类型转换
一,web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...
- struts2文件上传时获取上传文件的大小
利用struts2框架上传文件时,如果想要获取上传文件的大小可以利用下面的方式进行: FileInputStream ins = new FileInputStream(file); if (ins. ...
随机推荐
- restful风格,restcontroller与controller
restful风格,restcontroller与controller 初步接触springmvc的时候,被要求使用restful风格,彼时一头雾水,不懂何谓restful,参阅了很多资料,慢慢的接触 ...
- css的定位机制
牛腩新闻发不系统中遇到了CSS(Cascading style sheets),第一次接触,比较陌生还!因为CSS很多关于元素定位的问题,并且很多情况下元素的位置以像素精度计.一个不小心就很头疼,为此 ...
- MyEclipse Spring被删之后,如何在myeclipse里面重新导入
1.找到项目文件 2.用记事本打开 .project 把里面涉及到spring的全部删除就可以了.
- 初识DirectX和COM
一.COM 1.什么是COM对象 一个COM对象事实上是一个或一套实现了大量接口的C++类 2.COM的优点 不用重新编译你的程序就能升级COM模块 3.COM概貌 4.COM对象的接口 QueryI ...
- LCD显示的一些基本概念以及DSI的一些clock解释
数字视频的基本概念源自于模拟视频.对于模拟视频我们可以这样理解:视频可以分解为若干个基本视点(像素),每个像素都有独立的色彩信息,在屏幕上依次将 这些点用电子枪按照行和列打出来,就形成了一幅完整画面 ...
- 《算法问题实战策略》-chaper15-计算几何-线段相交
这篇文章着力来讨论线段相交这一个问题. 给出两条线段,如何判断这两条线段相交? 如果这两条线段相交,如何求其交点? 线段相交问题通常由于其繁杂的情况种类而让人避而远之,在这里希望通过笔者的简化讨论希望 ...
- SRM 409(1-250pt, 1-500pt)
DIV1 250pt 题意:称string s是vector<string> words的ordered superstring,如果它满足:存在一个数列{x0, x1, x2...xm} ...
- C#添加资源的两种方式
1.粘贴到项目Properties中的Resources.resx中 base.m_bitmap = Properties.Resources.MeasuredisTool; 2.添加已有资源中的bm ...
- 还原dede数据后系统基本参数空白栏目无显示的解决方法
有时dedecms开发的网站在更换空间还原数据后,出现"系统基本参数"空白,而且可以看到tag也没有了. 大家不妨看看后台"数据库备份/还原"处,已经还原后的表 ...
- Android RecyclerView使用(一)
RecyclerView一种可以替换掉listview gridview 瀑布流等控件的新视图控件,控制视图回收和复用 ==优化内存可以替换listview 与listview的不同之处是:listv ...