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. ...
随机推荐
- druid配置(转)
java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,有不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色的性能,也 ...
- android Process.killProcess 和 System.exit(0) 区别
1 Process.killProcess 和 System.exit(0) 两个都会 kill 掉当前进程. 你可以打开 DDMS 查看进程号,或 adb shell 进入 shell 然后 ps ...
- android--使用Struts2服务端与android交互
一,服务器端: web.xml文件: <?xml version="1.0" encoding="UTF-8"?> <web-app vers ...
- Android程序安装后在模拟器上不显示,并且控制台显示The launch will only sync the application package on the device!
初学安卓,今天写了一个小例子,可是eclipse控制台却提示 No Launcher activity found! The launch will only sync the application ...
- 【HDOJ】3367 Pseudoforest
并查集. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10005 #d ...
- HDU 1150 Machine Schedule
题目大意: 有两台机器A和B以及K个需要运行的任务.A机器有N种不同的模式,B机器有M种不同的模式,而每个任务都恰好在一台机器上运行. 如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B ...
- 前端程序员:月薪 5K 到 5 万,我干了啥
高贵的前端程序猿们: 如何在前端开发这种高精尖的技术领域找到心仪的工作?实现在咖啡馆喝喝咖啡敲敲代码就能升职加薪.买房买车.迎娶白富美走上人生巅峰的职业梦想?这篇<进化论:从 0 到 100,前 ...
- Am命令
Am.java中: Override public void onRun() throws Exception { mAm = ActivityManagerNative.getDefault(); ...
- Hadoop的文件读写操作流程
以下主要讲解了Hadoop的文件读写操作流程: 读文件 读文件时内部工作机制参看下图: 客户端通过调用FileSystem对象(对应于HDFS文件系统,调用DistributedFileSystem对 ...
- unity3d Human skin real time rendering plus 真实模拟人皮实时渲染 plus篇
最近逃课做游戏,逃的有几门都要停考了,呵呵呵,百忙之中不忘超炒冷饭,感觉之前的人皮效果还是不够好,又改进了一些东西 首先上图 放大看细节 显而易见的比上次的效果要好很多,此次我把模型用3dmax进行了 ...