上传jsp
---------------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
function addMore()
{
var td = document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
input.type="file";
input.name="file";
button.type="button";
button.value="Remove";
button.onclick = function(){
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</script>
</head>
<body>
<form action="upload/save.do" enctype="multipart/form-data" method="post">
<table align="center" width="40%">
<tr>
<td>file:</td>
<td id="more"><!-- 定义一个id,方便javascript调用 -->
<input type="button" value="Add More..." onclick="addMore()">
</td>
</tr>
<tr>
<td><input type="submit" value="提交"> </td>
</tr>
</table>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------
http://yitong.xiaodoutao.com/
上传Action
-----------------------
public class UploadAction extends ActionSupport {
private List<File> file;
//下面两个变量是文件名与文件类型,Struts2会自动为以下两变量赋值
private List<String> fileFileName;
private List<String> fileContentType;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public String save() {
String root = ServletActionContext.getRequest().getRealPath("upload");
File dir = new File(root);
if (dir.exists()==false){
dir.mkdir();
}
for(int i = 0;i < file.size(); i++){
InputStream is = new FileInputStream(file.get(i));
File destFile = new File(root,this.getFileFileName().get(i));
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int length = 0;
while((length= is.read(buffer)) > 0){
os.write(buffer, 0, length);
}
is.close();
os.close();
}
return SUCCESS;
}
}
------------------------------------------------------------------------------------------------------------
上传struts.xml
----------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="authority">
<action name="upload/*" method="{2}" class="{1}Action">
<interceptor-ref name="upload">
配置允许上传的文件类型,多个用","分隔
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg
</param>
配置允许上传的文件大小,单位字节
<param name="maximumSize">102400</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success">/success.jsp</result>
</struts>
------------------------------------------------------------------------------------------------------------
下载jsp
---------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'down.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
测试检验test <br>
<a href="downloadCenter/execute.do?fileName=${fileName}" >下载中心</a>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------------
下载action
----------------------------------------------------------------------------
public class DownloadCenterAction extends ActionSupport {
String inputName = "inputStream";
String fileName;
public String getFileName() {
//从页面获取文件名
fileName = getRequest().getParameter("fileName");
String downFileName =fileName ;
try {
//中文文件名也是需要转码为 ISO8859-1,否则乱码
downFileName = new String(fileName.getBytes("ISO8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}
public InputStream getDownloadFile(){
try {
InputStream in = ServletActionContext.getServletContext().getResourceAsStream("upload/"+fileName );
System.err.println(in);
return in;
} catch (Exception e) {
return null;
}
}
public String execute() throws Exception {
//调用相关业务逻辑方法,动态设置相关下载信息
//contentType = "application/octet-stream;charset=ISO8859-1";
return SUCCESS;
}
}
------------------------------------------------------------------------------------------------------------
下载struts.xml
--------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--下载中心 -->
<package name="download_center" extends="authority">
<action name="*/*" method="{2}" class="{1}Action">
<result name="success" type="stream">
<!-- 设置内容类型 -->
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<!-- 设置下载文件的名字 attachment:作为附件,filename=:指定下载文件名-->
<param name="contentDisposition">attachment;filename="${downFileName}"</param>
<!-- 设置下载文件的输入流对应的方法 downloadFile对应DownloadAction中的getDownloadFile()-->
<!--对应的getDownloadFile类中的getDownloadFile方法-->
<param name="inputName">downloadFile</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>
------------------------------------------------------------------------------------------------------------
http://yitong.xiaodoutao.com/
分享到:
相关推荐
自己做的一个struts2 实现文件批量上传功能,
Struts2文件批量上传,里面有很多的web包,每一个都是一个完整的项目,每个web包中都有详细的说明文档
Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip
NULL 博文链接:https://lovexuwenhao.iteye.com/blog/459064
struts2 批量上传 图片+ 文件 显示进度条
很受网络上庞杂的Struts2上传下载的误导,很多没有解决中文乱码问题,本例实现了Struts2批量上传与下载,中文乱码问题完美解决,供大家参考!
Struts批量上传文件.doc Struts批量上传文件.doc Struts批量上传文件.docStruts批量上传文件.doc
struts1实现的文件批量上传,java实现
本案例是分为两种模式,单文件上传和多文件批量上传(可自动添加和减少上传文件的个数哦!) 欢迎交流吐槽。
本demo实现了struts2批量上传文件,利用html5进行批量选择,然后利用struts2进行后台数据的上传并保存到数据库,适合商品图片的上传,需要的同学可以下载。
struts2 ajax 实现 批量上传文件(按ctr键 多选,并非多个文件选择输入框的),jar包太大删掉了 仅用了struts2 的jar包 加进去即可
基于当前最新版本Struts2.3的多文件批量上传与下载,直接上传到服务器路径下,未涉及数据库操作。对上传的文件类型进行过滤
struts2+ajax+jquery异步批量上传超大文件.zip struts2+ajax+jquery异步批量上传超大文件.zip struts2+ajax+jquery异步批量上传超大文件.zip struts2+ajax+jquery异步批量上传超大文件.zip struts2+ajax+jquery异步...
基于Struts2和Spring的网络硬盘系统,批量上传文件和在线解压,优秀源代码! 基于Struts2和Spring的网络硬盘系统,批量上传文件和在线解压,优秀源代码! 基于Struts2和Spring的网络硬盘系统,批量上传文件和在线...
3. 在struts2的Action通过2种不同的实现方法返回json格式的字符串。 4. 针对商品实现简单无刷新上传与下载 4. 批量导入数据采用的是导入test文件夹下的测试压缩包upload.rar上传到服务器的临时目录,然后利用WinRar....
struts2+ajax+jquery异步批量上传超大文件,单文件最大支持2G,一次文件数量,文件大小都可设置。简单明了。绝对可用,稍微懂点的就可以修改为自己所用。你值得拥有! 我会分享给大家更多更好的东西,希望支持!
最近想在Struts2中实现批量上传(使用html的file标签时,一次只能选一个文件,我是想一下可以选多个文件),结果找了半天,大都是swf+ajax的,用起来太复杂了,代码也很繁多,实在没耐心了。 就想起FlashFileUpload...
Struts2+hibernate+spring实现多文件的上传与下载功能,主要包括:上传、下载、删除 1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定) 2.Struts2进行下载处理,能对上传的所有文件进行...
Struts2漏洞检查工具,可快速检测struts命令执行漏洞,支持批量导入验证,存在可利用的漏洞后,可远程执行命令及文件上传等操作。方便管理人员了解其危害。
一个简单的Struts2上传文件源码,支持批量上传。适合初学者的学习