终于上次的项目告一段落了,可以重新学习Struts了,于是终于吃过饭就开始捣鼓了,因为早就从网上把MAX先生的教程下来了,就看了看教程,着手进行吧
代码都是Max先生的,但是中间却出现了不少的错误,
主要文件有:FileUpload.jsp上传页面
xml 代码
- <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- >
- <html xmlns ="http://www.w3.org/1999/xhtml">
- <head>
- <title> Struts 2 File Upload title>
- head >
- <body >
- <s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data" >
- <s:file name ="myFile" label ="Image File"/>
- <s:textfield name ="caption" label ="Caption"/>
- <s:submit/>
- s:form >
- body >
- html >
action处理页面FileUploadAction.java
java 代码
- package tutorial;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Date;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class FileUploadAction extends ActionSupport {
- private static final long serialVersionUID = 572146812454l;
- private static final int BUFFER_SIZE = 16 * 1024;
- private File myFile;
- private String contentType;
- private String fileName;
- private String imageFileName;
- private String caption;
- public void setMyFileContentType(String contentType) {
- this.contentType = contentType;
- }
- public void setMyFileFileName(String fileName) {
- this.fileName = fileName;
- }
- public void setMyFile(File myFile) {
- this.myFile = myFile;
- }
- public String getImageFileName() {
- return imageFileName;
- }
- public String getCaption() {
- return caption;
- }
- public void setCaption(String caption) {
- this.caption = caption;
- }
- private static void copy(File src, File dst) {
- try {
- InputStream in = null;
- OutputStream out = null;
- try {
- in = new BufferedInputStream(new FileInputStream(src),
- BUFFER_SIZE);
- out = new BufferedOutputStream(new FileOutputStream(dst),
- BUFFER_SIZE);
- byte[] buffer = new byte[BUFFER_SIZE];
- while (in.read(buffer) > 0) {
- out.write(buffer);
- }
- } finally {
- if (null != in) {
- in.close();
- }
- if (null != out) {
- out.close();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static String getExtention(String fileName) {
- int pos = fileName.lastIndexOf(".");
- return fileName.substring(pos);
- }
- @Override
- public String execute() {
- imageFileName = new Date().getTime() + getExtention(fileName);
- File imageFile = new File(ServletActionContext.getServletContext()
- .getRealPath("/UploadImages")
- + "/" + imageFileName);
- copy(myFile, imageFile);
- return SUCCESS;
- }
- }
ShowUpload.jsp上传成功显示页面
xml 代码
- <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
- <html xmlns ="http://www.w3.org/1999/xhtml" >
- <head>
- <title> Struts 2 File Upload </title >
- </head>
- <body>
- <div style="padding: 3px; border: solid 1px #cccccc; text-align: center" >
- <img src='UploadImages/<s:property value ="imageFileName" />'/>
- <br/>
- <s:property value ="caption"/>
- </div>
- </body>
- </html>
struts.xml配置文件
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 >
- <package name ="fileUploadDemo" extends ="struts-default" >
- <action name ="fileUpload" class ="tutorial.FileUploadAction" >
- <interceptor-ref name ="fileUploadStack" />
- <result name ="success" >/ShowUpload.jsp</result >
- </action>
- </package >
- </struts >
web.xml
xml 代码
- <?xml version="1.0" encoding="UTF-8" ?>
- <web-app id ="WebApp_9" version ="2.4"
- xmlns ="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
- <display-name > Struts 2 Fileupload </display-name >
- <filter >
- <filter-name > struts-cleanup </filter-name >
- <filter-class >
- org.apache.struts2.dispatcher.ActionContextCleanUp
- </filter-class >
- </filter >
- <filter >
- <filter-name > struts2 </filter-name >
- <filter-class >
- org.apache.struts2.dispatcher.FilterDispatcher
- </filter-class >
- </filter >
- <filter-mapping >
- <filter-name > struts-cleanup </filter-name >
- <url-pattern > /* </url-pattern >
- </filter-mapping >
- <filter-mapping >
- <filter-name > struts2 </filter-name >
- <url-pattern > /* </url-pattern >
- </filter-mapping >
- <welcome-file-list >
- <welcome-file > index.html </welcome-file >
- </welcome-file-list >
- </web-app >
在进行调试的时候发现我的运气是相当的好 啊,因为别人提出的错误我都遇到了,比如如下:
java 代码
- Servlet.service() for servlet SimpleUploader threw exception
- java.lang.RuntimeException: Unable to load bean org.apache.struts2.dispatcher.multipart.MultiPartRequest (jakarta) - [unknown location]
我在网上搜了下,发现有人这样解决的:
xml 代码
- 只要不让struts2拦截处理上传事件,就应该没问题了。
- 修改web.xml,把原来的
- <filter-mapping>
- <filter-name>struts</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- 改为
- <filter-mapping>
- <filter-name>struts</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- 而上传页面调用时直接用文件名调用(如upload.jsp)。struts2只处理 *.action 的请求。
但是我用了一下出现了更麻烦的错误,于是又找了下是因为我的包所用版本太低的原因于是到Apache上下载了最新的包删除旧的导入新的刷新,打开tomcat服务,输入地址,又出现了一个错误
java 代码
- javax.servlet.ServletException: String index out of range: -1
- org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:518)
- org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)
- org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
因为我的代码是从max先生的文章直接复制粘贴的,开始有很多多余的空格,我就一个个删除的,这时我想是不是空格的问题,于是又检查一遍代码,把空格全部删除,ok..
看来代码是要自己敲的,还是有好处的。】
这里记下,提醒自己,呵呵
高手就不用看了,因为我们是菜鸟,呵呵