1. 文件上传的原理:
表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:
1) application/x-www-form-urlencoded:这是默认编码方式,它只处理表单域里的value属性值,采用这种编码方式的表单会将表单域的值处理成URL编码方式。
2) multipart/form-data:这种编码方式的表单会以二进制流的方式来处理表单数据,这种编码方式会把文件域指定文件的内容也封装到请求参数里。
3) text/plain:这种方式主要适用于直接通过表单发送邮件的方式。
文件上传下載是web application常常用到的。原理是,通过为表单元素设置enctype=”multipart/form-data”属性,让表单提交的数据以二进制编码的方式提交,在接收此request的后臺程序中用二進制流來獲取內容.就可以取得上传文件的内容,从而实现文件的上传。
文件上传项目有:一个是Common-FileUpload组件,另一个是Oreilly组织的COS框架。,利用这兩个框架都能很方便的实现文件的上传。
2.
Struts2默认使用的是Jakarta的Common-FileUpload框架来上传文件,需要两个Jar文件:commons-fileupload-1.2.jar和commons-io-1.4.jar。
如果要改成其它的文件上传框架,可以修改struts.multipart.parser常量的值为cos/pell,默认值是jakata。
代码:upload.jsp,可上传一个或多个附件
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ 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=UTF-8">
- <title>Insert title here</title>
- <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>
- <table align="center" width="50%">
- <tr>
- <td>
- <s:fielderror cssStyle="color:red" />
- </td>
- </tr>
- </table>
- <s:form action="upload" theme="simple" enctype="multipart/form-data">
- <table align="center" width="50%" border="1">
- <tr>
- <td>
- username
- </td>
- <td>
- <s:textfield name="username"></s:textfield>
- </td>
- </tr>
- <tr>
- <td>
- password
- </td>
- <td>
- <s:password name="password"></s:password>
- </td>
- </tr>
- <tr>
- <td>
- file
- </td>
- <td id="more">
- <s:file name="file"></s:file><input type="button" value="Add More.." οnclick="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>
- 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 org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class UploadAction extends ActionSupport
- {
- private List<File> file;
- private List<String> fileFileName;
- private List<String> fileContentType;
- //可通过依赖注入保存路径,
- //private String savePath;
- //setter,getter方法省略
- @Override
- public String execute() throws Exception
- {
- for (int i = 0; i < file.size(); ++i)
- {
- InputStream is = new FileInputStream(file.get(i));
- String root = ServletActionContext.getRequest().getRealPath(
- "/upload");
- File destFile = new File(root, this.getFileFileName().get(i));
- OutputStream os = new FileOutputStream(destFile);
- 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;
- }
- }
配置文件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>
- <constant name="struts.i18n.encoding" value="UTF-8"></constant>
- <!-- 临时文件的储存目录 -->
- <constant name="struts.multipart.saveDir" value="c:/"></constant>
- <package name="struts2" extends="struts-default">
- <action name="upload" class="com.test.action.UploadAction">
- <result name="success">/uploadResult.jsp</result>
- <result name="input">/upload.jsp</result>
- <interceptor-ref name="fileUpload">
- <param name="maximumSize">409600</param>
-
- <!-- 上傳文件允許的類型 -->
- <param name="allowedTypes">
- application/vnd.ms-powerpoint,image/gif,image/jpeg,text/html,application/pdf,
- application/vnd.ms-powerpoint,application/x-visio,application/vnd.ms-excel,
- application/zip,application/msword
- </param>
- </interceptor-ref>
- <interceptor-ref name="defaultStack"></interceptor-ref>
- <!--<param name="savePath">/upload</param>-->
- </action>
- </package>
- </struts>
Struts2文件下载
在配置文件中增加action
- <action name="download" class="com.test.action.FileDownloadAction">
- <result name="success" type="stream">
- <!-- <param name="contentType">
- application/vnd.ms-powerpoint,image/gif,image/jpeg,text/html,application/pdf,
- application/x-visio,application/vnd.ms-excel,
- application/zip,application/msword
- </param>
- <param name="contentType">${contentType}</param> -->
- <param name="inputName">inputStream</param>
- <param name="bufferSize">4096</param>
- <param name="contentDisposition">filename="${filename}"</param>
- <param name=""></param>
- </result>
- </action>
- import com.opensymphony.xwork2.ActionSupport;
- public class FileDownloadAction extends ActionSupport{
- private String inputPath;
- private String contentType;
- private String filename;
- public InputStream getInputStream() throws Exception{
- return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
- }
- @Override
- public String execute() throws Exception {
- inputPath="/upload/Struts2.ppt";//要下載的文件名稱
- filename="Struts2.ppt"; //保存文件時的名稱
- //contentType="application/vnd.ms-powerpoint";
- return SUCCESS;
- }
- //setter,getter方法省略
- }