使用Struts2简单的基本文件上传下载实现
2015-11-26 16:46
826 查看
1.首先创建一个web项目
2.使用myeclipse自带的功能配置成struts项目
代码开始
首先是下载页面代码
下载的Action
上传的action
struts.xml代码
配置struts.properties 代码
2.使用myeclipse自带的功能配置成struts项目
代码开始
首先是下载页面代码
<%@ 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 'download.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="downloadAction?fileName=F:/TEMP/AJAX.txt">下载文件</a> <br> </body> </html>上传页面代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% 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 'UpLoad.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> <br> <s:form action="uploadAcion" method="post" enctype="multipart/form-data"> <s:file name="file" label="请选择要上传的问题件"></s:file> <s:submit></s:submit> </s:form> </body> </html>
下载的Action
package action; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport{ private String fileName; //固定参数名,用于得到文件内容类型 private String contentType; //输入流,输入到内存 private InputStream inputStream; //将页面上要下载的文件类型通过后缀名转换为服务器能识别的文件类型 static Map<String,String> contentTypes = new HashMap<String,String>(); static{ contentTypes.put("doc", "application/msword"); contentTypes.put("ppt", "application/vnd.ms-powerpoint"); contentTypes.put("xsl", "application/vnd.ms-excel"); contentTypes.put("pdf", "application/pdf"); contentTypes.put("html", "text/html"); contentTypes.put("htm", "application/html"); contentTypes.put("gif","image/gif"); contentTypes.put("jpeg","image/jpeg"); contentTypes.put("mpeg","video/mpeg"); } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getContentType(String fileName) { for(String ext:contentTypes.keySet()){ if(fileName.toLowerCase().endsWith(ext)){ return contentTypes.get(ext); } } //代表二进制传输文件 return "application/octet-stream"; } public void setContentType(String contentType) { this.contentType = contentType; } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } @Override public String execute() { File file = new File(fileName); try{ inputStream = new FileInputStream(file); }catch(Exception e){ e.printStackTrace(); } fileName = file.getName(); contentType = this.getContentType(fileName); return SUCCESS; } }
上传的action
package action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { //file控件的名字 private String file; //文件类型,后缀是固定的 private String fileContentType; //文件的名字 private String fileFileName; public String getFile() { return file; } public void setFile(String file) { this.file = file; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } @Override public String execute(){ try { InputStream is = new FileInputStream(file); //一次传输的大小 byte[] b = new byte[1024]; int length = is.read(b); //目标文件夹 String path ="F:/TEMP"; File f = new File(path); //如果不存在就创建 if(!f.exists()){ f.mkdir(); } //输出流,从内存中输出到内存外 OutputStream os = new FileOutputStream(new File(path+"/"+fileFileName)); while(length>0){ os.write(b); length = is.read(); } is.close(); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } }
struts.xml代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="pk" extends="struts-default" namespace="/"> <action name="uploadAcion" class="action.UploadAction"> <result>/upload.jsp</result> <result name="input">/upload.jsp</result> </action> <action name="downloadAction" class="action.DownloadAction"> <result type="stream"> <param name="contentType">${contentType}</param> <param name="contentDisposition">attachment;filename=${fileName }</param> <param name="inputName">inputStream</param> <param name="bufferSize">1024</param> </result> </action> </package> </struts>
配置struts.properties 代码
struts.multipart.parser=jakarta struts.multipart.saveDir=C\:/TEMP struts.multipart.maxSize=102400
相关文章推荐
- struts2 jquery 打造无限层次的树
- struts2入门Demo示例
- 通过Ajax两种方式讲解Struts2接收数组表单的方法
- ajax交互Struts2的action(客户端/服务器端)
- struts2 spring整合fieldError问题
- Struts2的s:radio标签使用及用jquery添加change事件
- struts2 中文乱码的解决办法分享
- Struts2访问servlet分享
- struts2单个文件上传的两种实现方式
- struts2+jsp实现文件上传的方法
- struts2+jquery实现ajax登陆实例详解
- struts2中一个表单中提交多个请求的例子(多个提交按钮)
- JQuery+Ajax+Struts2+Hibernate框架整合实现完整的登录注册
- Java(基于Struts2) 分页实现代码
- 从搭建Struts2 开发环境说起
- JAVA+Struts2获取服务器地址的方法
- 使用maven+eclipse搭建struts2开发环境
- 关于国际化、OGNL表达式语言
- 在Struts2中如何将父类属性序列化为JSON格式的解决方法
- 在Struts2中的结果集类型