------------->>转载于;http://blog.csdn.net/zjw10wei321/article/details/9031417
对于struts2一直都是看过,很少去动手写里面的功能,今天花了一点时间算整理整理。
整体的项目大致如下:
1:文件上传
uploadAction.java
- package action;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- 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 static final long serialVersionUID = -8920466592471253212L;
- private String title;
- private File file; // 上传的文件
- private String fileFileName; // 文件名称
- private String fileContentType; // 文件类型
- private String savePath;
- private String contents = "";
- public String execute() throws Exception {
- // String url = getSavePath() + File.separator + getFileFileName();
- String url = getSavePath();
- System.out.println("url==" + url + ",,," + getFileContentType());
- FileOutputStream fos = null;
- FileInputStream fis = null;
- try {
- // fos = new FileOutputStream(url);
- File uploadFile = new File(url, this.getFileFileName());
- fos = new FileOutputStream(uploadFile);
- fis = new FileInputStream(getFile());
- byte[] buffer = new byte[1024 * 1024];
- int len = 0;
- while ((len = fis.read(buffer)) > 0) {
- fos.write(buffer, 0, len);
- setContents(getContents() + new String(buffer));
- }
- fos.flush();
- } catch (Exception e) {
- e.printStackTrace();
- return INPUT;
- } finally {
- close(fos, fis);
- }
- return SUCCESS;
- }
- public File getFile() {
- return file;
- }
- public void setFile(File file) {
- this.file = file;
- }
- public String getFileFileName() {
- return fileFileName;
- }
- public void setFileFileName(String fileFileName) {
- this.fileFileName = fileFileName;
- }
- public String getFileContentType() {
- return fileContentType;
- }
- public void setFileContentType(String fileContentType) {
- this.fileContentType = fileContentType;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getSavePath() {
- String result = ServletActionContext.getServletContext().getRealPath(
- savePath);
- return result;
- }
- public void setSavePath(String savePath) {
- this.savePath = savePath;
- }
- public String getContents() {
- return contents;
- }
- public void setContents(String contents) {
- this.contents = contents;
- }
- private void close(FileOutputStream fos, FileInputStream fis) {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- System.out.println("FileInputStream关闭失败");
- e.printStackTrace();
- }
- }
- if (fos != null) {
- try {
- fos.close();
- } catch (IOException e) {
- System.out.println("FileOutputStream关闭失败");
- e.printStackTrace();
- }
- }
- }
- }
上传页面upload.jsp:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib prefix="s" uri="/struts-tags" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- >
- <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">
- head>
- <body>
- <s:form action="uploads" method="post" enctype="multipart/form-data">
- <s:textfield id="title" name="title" label="文件标题"/>
- <s:file name="file" label="上传文件"/>
- <s:submit value="提交"/>
- s:form>
- body>
- html>
上传成功uploadSuccess.jsp:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@taglib prefix="s" uri="/struts-tags" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- >
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting pagetitle>
- <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">
- head>
- <body>
- 上传文件:<s:property value="title" /><br/>
- 文件内容:
- <s:if test="%{fileContentType.indexOf('image')>=0}">
- <img src="<s:property value="'upload/'+fileFileName"/>"/>
- s:if>
- <s:else>
- <s:property value="contents"/>
- s:else>
- body>
- html>
主要的struts.xml:
- xml version="1.0" encoding="UTF-8" ?>
- >
- <struts>
- <constant name="struts.devMode" value="true" />
- <constant name="struts.i18n.encoding" value="UTF-8" />
- <package name="myDemo" extends="struts-default" namespace="/">
- <action name="uploads" class="action.UploadAction" >
- <param name="savePath">/uploadparam>
- <result name="input">/upload.jspresult>
- <result name="success">/uploadSuccess.jspresult>
- action>
- <action name="downloads" class="action.DownloadAction">
- <param name="inputPath">uploadparam>
- <result name="input">/downloadSuccess.jspresult>
- <result name="success" type="stream">
- <param name="contentType">application/octet-stream;charset=ISO8859-1 param>
- <param name="inputName">downloadFileparam>
- <param name="contentDisposition">attachment;filename="${downloadFileName}"param>
- <param name="bufferSize">4096param>
- result>
- action>
- <action name="download" class="action.DownloadAction" method="download">
- <param name="inputPath">uploadparam>
- <result name="success" >/download.jsp
- result>
- action>