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.* ...
随机推荐
- java高并发之CountDownLatch,CyclicBarrier和join
晚上打车回家,在车上看到一篇文章<22岁大学生获谷歌天价Offer,年薪千万!>,讲的是印度一个22岁大学生多次参加ACM大赛,开源多个项目,以非常牛逼的履历通过了谷歌的AI测试,斩获谷歌 ...
- ethereum(以太坊)(十二)--应用(二)__投票(基础总和)
编写应用合约之前,先弄清它的逻辑,有助于我们更好的部署合约 pragma solidity ^0.4.21; pragma experimental ABIEncoderV2; contract vo ...
- scrapy--selenium(二)
今天学习了很多,还是想给大家讲一讲正题:scrapy的动态加载AJax的网页爬取:selenium.让我们开始 三: 针对大型电商网站:京东网,因为比较有代表性,爬出来有点小成就.先给大家看下效果图. ...
- 为什么C++编译器不能支持对模板的分离式编译
首先,一个编译单元(translation unit)是指一个.cpp文件以及它所#include的所有.h文件,.h文件里的代码将会被扩展到包含它的.cpp文件里,然后编译器编译该.cpp文件为一个 ...
- POJ:2449-Remmarguts' Date(单源第K短路)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33081 Accepted: 8993 Des ...
- 3-1 练习 HTML 总结
1.段落 #段落 <div class="ui segment"> </div> #翻转 <div class="ui inverted s ...
- 解决Android Studio报错:DefaultAndroidProject : Unsupported major.minor version 52.0
解决办法是你需要将工程根目录build.gradle中的 classpath 'com.android.tools.build:gradle:2.2.0' 更改成 classpath 'com.and ...
- 剑指Offer - 九度1384 - 二维数组中的查找
剑指Offer - 九度1384 - 二维数组中的查找2013-11-23 23:23 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个 ...
- 基于Python的selenuim自动化测试尝试
工作这么多年了,终于狠下心好好开始学学自动化测试相关知识,揭开这层神秘的面纱. 困难重重,障碍很多,但好在每天都多少有点小收获. 很感谢一个QQ好友推荐的虫师,也非常感谢在这个契机读到了虫师编著的&l ...
- Python IO关于mode参数的问题
关于open()的mode参数: 'r':读 'w':写 'a':追加 'r+' == r+w(可读可写,文件若不存在就报错(IOError)) 'w+' == w+r(可读可写,文件若不存在就创建) ...