- 浏览: 225052 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (118)
- SQL Server (2)
- Oracle (6)
- J2SE (8)
- J2EE (4)
- Struts2 (9)
- Hibernate (4)
- Spring (5)
- JS框架 (8)
- DWR (2)
- JQuery (4)
- JSP (1)
- log4j (1)
- MySql (3)
- JBPM (2)
- EJB (1)
- JBoss (1)
- maven (2)
- IT杂谈 (1)
- eclipse (1)
- VMware (1)
- apache (3)
- JavaScript (5)
- HTML5 (1)
- SVN (2)
- Linux (2)
- tools (1)
- HttpClient (1)
- FTP (1)
- FTP、定时器 (0)
- FTP上传(java) (1)
- 工具类 (2)
- 排序算法 (2)
- 设计模式 (1)
- 面试题 (9)
- java设计模式 (1)
- ZooKeeper (1)
- 并发 (1)
最新评论
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.multipart.maxSize" value="4194304"></constant> <package name="upLoad" namespace="/upLoad" extends="struts-default"> <action name="*-*" class="com.wepull.action.{1}Action" method="{2}"> <interceptor-ref name="fileUpload"> <param name="maximumSize">204800</param><!-- 单个上传文件不能超过200K --> <param name="allowedTypes">image/jpeg,image/gif</param> <!-- 允许上传的类型 --> </interceptor-ref> <interceptor-ref name="defaultStack" /> <result name="success">/success.jsp</result> <result name="input">/failure.jsp</result><!-- 上传失败返回input --> </action> </package> <package name="down" namespace="/down" extends="struts-default"> <action name="*-*" class="com.wepull.action.{1}Action" method="{2}"> <param name="path">down/王.jpg</param> <result name="down-success" type="stream"> <param name="contentType">image/jpeg,image/gif</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename=${fileName}</param> <param name="bufferSize">4096</param> </result> </action> </package> </struts>
下载java代码:
package com.wepull.action; import java.io.InputStream; import java.io.UnsupportedEncodingException; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownAction extends ActionSupport { private String fileName; private String path; public String down() { fileName = path.substring(path.lastIndexOf("/") + 1);// 截取文件名字 // fileName= "王.jpg"; System.out.println(fileName); return "down-success"; } public String getFileName() { // 转换成西欧字符集 try { fileName = new String(fileName.getBytes(), "ISO-8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return fileName; } public InputStream getInputStream() { InputStream is = ServletActionContext.getServletContext() .getResourceAsStream(path); return is; } public void setPath(String path) { this.path = path; } }
下载前台页面:
<%@ 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 'index.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> <a href="down/Down-down">点击下载玉照</a> <br> </body> </html>
上传代码:上传单个文件
package com.wepull.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; public class UpLoadAction extends ActionSupport { private File fileUp;//一定要和form表单中 上传框的name的属性值保持一致 private String fileUpFileName;// 上传文件的名字 必须以FileName结尾 private String fileUpContentType;//上传文件的名字类型 必须以ContentType结尾 public String upLoad(){ String path = ServletActionContext.getServletContext().getRealPath("/"); System.out.println("发布工程的路径:"+path);//D:\Program Files\apache-tomcat-6.0.24\webapps\1102_Struts_upLoad File saveDir = new File(path+File.separator+"image"); // File saveDir = new File(path,"image"); if(!saveDir.exists())saveDir.mkdirs(); File saveFile = new File(saveDir,fileUpFileName);//要保存的文件 try { FileUtils.copyFile(fileUp,saveFile );//拷贝文件 } catch (IOException e) { e.printStackTrace(); } return SUCCESS; } public File getFileUp() { return fileUp; } public void setFileUp(File fileUp) { this.fileUp = fileUp; } public String getFileUpFileName() { return fileUpFileName; } public void setFileUpFileName(String fileUpFileName) { this.fileUpFileName = fileUpFileName; } public String getFileUpContentType() { return fileUpContentType; } public void setFileUpContentType(String fileUpContentType) { this.fileUpContentType = fileUpContentType; } }
上传代码:上传单个文件 对应前台代码
<%@ 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 'index.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> <form action="upLoad/UpLoad-upLoad" enctype="multipart/form-data" method="post"> <input type="file" name="fileUp"><br> <input type="submit" value="上传"> <input type="reset" value="重置"> </form> </body> </html>
上传代码2:上传多个文件
package com.wepull.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; public class UpLoad2Action extends ActionSupport { private File[] fileUp;//一定要和form表单中 上传框的name的属性值保持一致 private String[] fileUpFileName;// 上传文件的名字 必须以FileName结尾 private String[] fileUpContentType;//上传文件的名字类型 必须以ContentType结尾 public String upLoad(){ String path = ServletActionContext.getServletContext().getRealPath("/"); System.out.println("发布工程的路径:"+path);//D:\Program Files\apache-tomcat-6.0.24\webapps\1102_Struts_upLoad File saveDir = new File(path+File.separator+"image"); // File saveDir = new File(path,"image"); if(!saveDir.exists())saveDir.mkdirs(); for (int i = 0; i < fileUp.length; i++) { File saveFile = new File(saveDir,fileUpFileName[i]);//要保存的文件 try { FileUtils.copyFile(fileUp[i],saveFile );//拷贝文件 } catch (IOException e) { e.printStackTrace(); } } return SUCCESS; } public File[] getFileUp() { return fileUp; } public void setFileUp(File[] fileUp) { this.fileUp = fileUp; } public String[] getFileUpFileName() { return fileUpFileName; } public void setFileUpFileName(String[] fileUpFileName) { this.fileUpFileName = fileUpFileName; } public String[] getFileUpContentType() { return fileUpContentType; } public void setFileUpContentType(String[] fileUpContentType) { this.fileUpContentType = fileUpContentType; } }
上传代码2:上传多个文件 对应前台代码
<%@ 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 'index.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> <form action="upLoad/UpLoad2-upLoad" enctype="multipart/form-data" method="post"> <input type="file" name="fileUp"><br> <input type="file" name="fileUp"><br> <input type="file" name="fileUp"><br> <input type="submit" value="上传"> <input type="reset" value="重置"> </form> </body> </html>
发表评论
-
struts2 三种传值方式
2011-08-14 19:31 124181. * 第一种传值 struct.xml ... -
Action名称的搜索顺序
2011-06-14 23:10 756获得请求路径的URI,例如url是:http://server ... -
Struts2中参数传递
2011-06-14 23:07 10561.最简单的参数传递 使用OGNL的最基本的功能, ... -
Struts2整合Jfreechart
2011-06-08 16:43 1421准备:需要的jar jfreechar的jarStruts的 ... -
Struts简单数据验证
2011-06-08 16:27 862<?xml version="1.0" ... -
Struts2标签
2011-06-03 17:04 1157<%@ page language="java ... -
Struts2的OGNL
2011-06-03 17:03 1095<%@ page language="java ... -
struts2使用convention-plugin实现零配置
2011-05-30 15:30 1255从struts2.1开始,struts2不 ...
相关推荐
struts2上传下载struts2上传下载
struts2上传下载项目,代码详细,注释清晰,简单易懂
Struts2 上传下载
struts2上传下载实例包含了很多种不同的方法
struts2 上传 下载 项目 mysql dao mvc
简单的struts2上传下载文件功能,希望可以帮助到大家
struts2上传下载jar包
struts2 学习经典案例,包含struts2标签,上传下载,拦截器等。可以帮助了解struts2框架,并进行扩展
完整的基于Struts2的上传下载功能实现,开发平台为MyEclipse
Struts2 上传下载 Demo