<o:p> </o:p>添加 Struts 框架,使用 Struts1.2 版本<o:p></o:p>
<o:p></o:p>
新建 一个 jsp + action 这里将使用动态的 ActionForm<o:p></o:p>
<o:p> </o:p> 在新建表单对像时使用一个文件名和一个文件对像进行提交<o:p></o:p>
<o:p> </o:p>修改动态 From 的类型为 org.apache.struts.upload.FormFile<o:p></o:p>
<o:p> </o:p>
- <form-bean name="upfileForm" type="org.apache.struts.action.DynaActionForm">
- <form-property name="filename" type="java.lang.String" />
- <form-property name="filedata" type="java.lang.String" />
- form-bean>
改为 <o:p></o:p>
- <form-bean name="upfileForm" type="org.apache.struts.action.DynaActionForm">
- <form-property name="filename" type="java.lang.String" />
- <form-property name="filedata" type="org.apache.struts.upload.FormFile" />
- form-bean>
<o:p> </o:p>
修改 upfile.jsp 文件,在中加入 enctype="multipart/form-data" ,这样才可以提交二进制类型的文件<o:p></o:p>
<o:p> </o:p>
修改文件第一行代码<o:p></o:p>
<o:p>
- <%@ page language="java"%>
改为
- <%@ page contentType="text/html;charset=UTF-8" language="java"%>
修改 upfileAction.java 文件,修改后的内容如下:<o:p></o:p>
<o:p></o:p>
<o:p>- package com.test.struts.action;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.action.DynaActionForm;
- import org.apache.struts.upload.FormFile;
- /**
- * MyEclipse Struts
- * Creation date: 07-05-2006
- *
- * XDoclet definition:
- * @struts.action path="/upfile" name="upfileForm" input="/upfile.jsp" scope="request" validate="true"
- */
- public class UpfileAction extends Action {
- // --------------------------------------------------------- Instance Variables
- // --------------------------------------------------------- Methods
- /**
- * Method execute
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward execute(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- DynaActionForm upfileForm = (DynaActionForm) form;
- // 声明并获取对像
- String filename = upfileForm.getString("filename");
- // 输出文件名
- System.out.println(filename);
- FormFile filedata = (FormFile) upfileForm.get("filedata");
- // 取当前系统路径
- String filePath = request.getRealPath("/");
- try {
- // 转换文件为数据流
- InputStream stream = filedata.getInputStream();
- // 建立输出流
- OutputStream bos = new FileOutputStream(filePath + "/" +
- filedata.getFileName());
- // 将文件写入网站根目录下
- int bytesRead = 0;
- byte[] buffer = new byte[8192];
- while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
- bos.write(buffer, 0, bytesRead);
- }
- bos.close();
- stream.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // 返回到提交页面
- return mapping.getInputForward();
- }
- }
<o:p></o:p>
将SetCharacterEncodingFilter.java复制到项目中,在 web.xml 文件中加入以下配置内容,过滤器的编码设置为 UTF-8<o:p></o:p>
<o:p></o:p>
- <filter>
- <filter-name>Set Character Encodingfilter-name>
- <filter-class>com.test.SetCharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>Set Character Encodingfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- <filter-mapping>
- <filter-name>Set Character Encodingfilter-name>
- <servlet-name>actionservlet-name>
- filter-mapping>
配置 Tomcat 的 server.xml 文件,文件在 Tomcat_Home/conf 中<o:p></o:p>
<o:p> </o:p>
在端口配置的前面加入 URIEncoding="UTF-8" 如果使用了和IIS集成的话需要在 8009 的端口前也加入此配置内容。<o:p> </o:p>
现在启动服务器,测试。<o:p></o:p>
</o:p></o:p>