今天主要分享开源框架Struts2文件上传实例过程,并且笔者将带着大家解决出现的一系列乱码问题,本文章中的重要部分将用特殊颜色标识,斜体表示不确定内容。笔者建议读者先快速阅读一遍本文,下载应具备的工具,再动手操作。或许写一遍比看十遍的功效更为明显。
笔者的Struts2版本号是2.2.3,如果你的是2.0版本以上也没关系。创建的java project项目名字为:uploads ,各个文件、页面编码统一为:UTF-8。
首 先导入Struts所依赖或自身的jar包,我们直接导入struts官方给出的空白实例所用到的jar包,分为:asm-3.1.jar、asm- commons-3.1.jar、asm-tree-3.1.jar、commons-fileupload-1.2.2.jar、commons- io-2.0.1.jar、commons-lang-2.5.jar、freemarker-2.3.16.jar、javassist- 3.11.0.GA.jar、ognl-3.0.1.jar、struts2-core-2.2.3.jar、xwork-core- 2.2.3.jar,这些包位于\struts-2.2.3\apps\struts2-blank\WEB-INF\lib目录下,如果你和笔者所使用 的struts版本不一致,请直接导入该目录下的所有文件。
uploads\WebRoot\WEB-INF\web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>uploads</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>uploads</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
uploads\src\struts.xml (注意:如果你的版本和笔者的不一样可以展开struts2-core-2.2.3 .jar,打开struts-default.xml文件 ,大概在24-26行 DOCTYPE 内容替换以下粗体)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant><!-- 解决乱码!! -->
<package name="uploads" extends="struts-default">
<action name="uploads"
class="com.lingdus.action.FileUploadAction">
<result name="success" type="redirect">/index.jsp</result>
</action>
</package>
</struts>
uploads\src\com\lingdus\action\FileUploadAction.java
package com.lingdus.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private File file; // 这个和以下的内容在struts中有特殊的要求,具体请翻阅相关资料。
private String fileFileName;
public synchronized File getFile() {
return file;
}
public synchronized void setFile(File file) {
this.file = file;
}
public synchronized String getFileFileName() {
return fileFileName;
}
public synchronized void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
@Override
public String execute() throws Exception {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(file);
os = new FileOutputStream("C:\\" + fileFileName);
byte[] buffer = new byte[2875623];
int length = 0;
while (-1 != (length = is.read(buffer))) {
os.write(buffer, 0, length);
}
Map<String, Object> session = ActionContext.getContext()
.getSession();
session.put("result", "文件成功上传至:" + "C:\\" + fileFileName);
} catch (Exception e) {
e.printStackTrace();
return "ERROR";
} finally {
os.close();
is.close();
}
return SUCCESS;
}
}
uploads\WebRoot\index.jsp
<%@ 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>文件上传</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>
<s:form action="uploads" enctype="multipart/form-data" method="post" >
<s:file name="file" label="文件上传"></s:file>
<s:submit value="立即上传"></s:submit>
</s:form>
上传结果
<hr />
${result }
</body>
</html>
演示效果
本文转自北大青鸟 成都锦江校区,原文链接 http://www.scbdqn.com/course/netjava/3269.html