Struts2.1笔记(九):上传和下载文件

8.1 上传文件完成前的两个必要操作
1.表单的enctype属性设置为multipart/form-data
2.method="post"
8.2上传框架
常用的上传框架有:Common-FileUpload和COS
8.2.1下载Common-FileUpload框架
Common-FileUpload
Common-IO
8.3 Struts2的文件上传
步骤:
a.编写文件的上传页面
b.实现文件上传的Action
c.配置文件上传的Action
8.3.1 拦截器实现文件过滤
配置fileUpload拦截器 两个重要参数
allowedTypes:允许上传的文件类型。
maximumSize:允许上传文件的大小。
8.4使用Struts2控制文件下载
步骤:
a.编写下载页面
b.实现文件下载的Action
c.配置文件下载的Action
配置时的3个重要属性
contentType:下载文件的文件类型
inputName:下载文件的入口输入流
contentDisposition:下载的文件名

下面是一个文件上传和下载的实例代码

1.可以自定义实现上传数量的实例
upload.JSP代码

<%@ page language="java" contentType="text/html; charset=gb2312"
 pageEncoding="gb2312"%>
<%@ 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=gb2312">
<title>Insert title here</title>
<script type="text/javascript">
function addMore(){
 var td = document.getElementById("td");
 
 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>
<s:form action="upload" enctype="multipart/form-data" theme="simple">

 <table align="center" width="50%" border="1">
  <tr>
   <td>Title</td>
   <td><s:textfield name="title" label="title" id="titleId"></s:textfield>
   </td>
  </tr>


  <tr>
   <td>upload</td>

   <td id="td"><s:file name="file" label="file"></s:file><input type="button" value="AddMore..." onclick="addMore()"></td>
  </tr>

  <tr>
   <td><s:submit value=" submit "></s:submit></td>

   <td><s:reset value=" reset "></s:reset></td>
  </tr>
 </table>
</s:form>
</body>
</html>

UploadAction代码

package com.test.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
 private List<String> title;
 private List<File> file;
 private List<String> fileFileName;
 private List<String> fileContentType;

 public String execute() throws Exception {
  for (int i = 0; i < file.size(); i++) {
   InputStream is = new FileInputStream(file.get(i));

   File f = new File("D:", this.getFileFileName().get(i));
   OutputStream os = new FileOutputStream(f);
   byte[] buffer = new byte[400];
   int length = 0;
   while ((length = is.read(buffer)) > 0) {
    os.write(buffer, 0, length);
   }
   is.close();
   os.close();

  }
  return SUCCESS;
 }

 public List<String> getTitle() {
  return title;
 }

 public void setTitle(List<String> title) {
  this.title = title;
 }

 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;
 }
}


struts.xml

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 "http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- struts是Struts 2配置文件的根元素 -->
<struts>
 <constant name="struts.custom.i18n.resources" value="message"></constant>
 <constant name="struts.i18n.encoding" value="gbk"></constant>
 <constant name="struts.multipart.saveDir" value="D:/"></constant>
 <package name="Struts2" extends="struts-default">
<action name="upload" class="com.test.action.UploadAction">
   <result name="success">/Result2.jsp</result>
   <result name="input">/upload2.jsp</result>
   <interceptor-ref name="fileUpload">
    <param name="allowedTypesSet">
     application/vnd.ms-word
    </param>
   </interceptor-ref>
   <interceptor-ref name="defaultStack"></interceptor-ref>
  </action>
  </package>
</struts>

---------------------------------------------------------------

2.限制只能下载ppt格式的文件

download.JSP代码

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
    <%@ 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=gb2312">
<title>Insert title here</title>
</head>
<body>
<s:a href="/Struts2/download.action">down</s:a>
</body>
</html>

DownLoadAction.java代码


package com.test.action;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownLoadAction extends ActionSupport {

 public InputStream getDownFile() {
  return ServletActionContext.getServletContext().getResourceAsStream(
    "/upload/Struts2.ppt");

 }

 @Override
 public String execute() throws Exception {

  return SUCCESS;
 }

}


struts.xml配置

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 "http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- struts是Struts 2配置文件的根元素 -->
<struts>
 <constant name="struts.custom.i18n.resources" value="message"></constant>
 <constant name="struts.i18n.encoding" value="gbk"></constant>
 <constant name="struts.multipart.saveDir" value="D:/"></constant>
 <package name="Struts2" extends="struts-default">
<action name="download"
   class="com.test.action.DownLoadAction">
   <result name="success" type="stream">
    <param name="contentType">
     application/vnd.ms-powerpoint
    </param>
    <param name="contentDispostion">
     filename="Struts2.ppt"
    </param>
    <param name="inputName">downFile</param>
   </result>
  </action>
 </package>
</struts>

 

你可能感兴趣的:(exception,struts,File,input,action,button)