上传的文件可以成功的显示存在指定的路径的且里面的内容也是完好的,但是该路径下多的一个文件该文件图标为一个易字像易语言的,该文件名为:upload__593bfe47_132b551c6b1__8000_00000001.tmp。struts2.xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
      <constant name="struts.i18n.encoding" value="gb2312" />
      <constant name="struts.multipart.saveDir" value="D:/upload" />
       <package name="default" extends="struts-default">
          <action name="upload" class="action.show">
          <interceptor-ref name="fileUpload">
<param name="allowedTypes">text/plain</param>
<param name="maximumSize">524288</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
          
            <result name="success">/ShowRequired.jsp</result>
               <result name="input">/Required.jsp</result>
          </action>
       </package>
</struts>   
action包中的类为show.java如下:
package action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class show extends ActionSupport {
   private File doc;
   private String docFileName;
public File getDoc() {
return doc;
}
public void setDoc(File doc) {
this.doc = doc;
}
public String getDocFileName() {
return docFileName;
}
public void setDocFileName(String docFileName) {
this.docFileName = docFileName;
}
@Override
public String execute() throws Exception {
InputStream in=new FileInputStream(getDoc());
OutputStream on=new FileOutputStream("D:\\upload\\"+getDocFileName());
byte by[]=new byte[1024*1024];
int length=0;
while((length=in.read(by))!=0){
on.write(by,0,length);
}
on.flush();
in.close();
on.close();
return "success";
}
   
}
输入界面Required.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0021)http://www.itzcn.com/ -->
<HTML>
<HEAD><TITLE>免费看视频教程,轻松学习计算机技术!-窗内网 - 视频教程</TITLE>
<LINK href="pay.css" type=text/css rel=stylesheet>
</HEAD>
<body style="text-align: center;">
<center><jsp:include page="index.html"></jsp:include>
<div style="margin-left:50px;text-align: left;">
<s:form action="upload"  method="post"  enctype="multipart/form-data">
   <s:file label="文件文件" name="doc"></s:file>
   <s:submit value="提交"></s:submit>
</s:form> </div>
</center></BODY>
</HTML>
成功界面ShowRequired.jsp:
<%@ page language="java"  pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %><jsp:include page="index.html"></jsp:include>
 <html>
  <body>
    <br>
  <h2>上传成功</h2>
  </body>
 </html> 提交文件后跳到一个错误页面,页面内容如下:    
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.IndexOutOfBoundsException
java.io.FileOutputStream.writeBytes(Native Method)
java.io.FileOutputStream.write(FileOutputStream.java:260)
action.show.execute(show.java:35)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
  

解决方案 »

  1.   

    这叫上传成功?
    java.lang.IndexOutOfBoundsException
    java.io.FileOutputStream.writeBytes(Native Method)
    java.io.FileOutputStream.write(FileOutputStream.java:260)
    action.show.execute(show.java:35)
    报错了!
      

  2.   

    (length=in.read(by))!=0
    去看看api是不等于0吗?
      

  3.   

    /**
     * 文件上传到服务器
     * 
     * @author snowday88
     */
    public class FileUploadAction extends ActionSupport
    {
        /**
         * 序列化
         */
        private static final long serialVersionUID = -5345481758460679463L;
        
        // 文件对象
        private File[] mf;
        
        // 文件名
        private String[] mfFileName;
        
        // 文件类型
        private String[] mfContentType;
        
        
        /**
         * 执行文件上传
         * 
         * @return String
         */
        public String execute()
        {
            // 获得文件夹upload绝对路径
            StringBuffer path = new StringBuffer();
            path.append(ServletActionContext.getServletContext()
                .getRealPath("upload"));
            try
            {
                if (null != mf && mf.length > 0)
                {
                    for (int i = 0; i < mf.length; i++)
                    {
                        String fname = mfFileName[i];
                        path.append("/"
                            + ServletActionContext.getRequest().getRemoteAddr()
                            + fname.substring(fname.lastIndexOf("."),
                                fname.length()));
                        //文件复制工具类,可以自己写个。或者用commons-io的api
                        FileUtil.copy(mf[i], path.toString());
                    }
                   
                }
            }
            catch (Exception e)
            {
                MyLogger.CUSTOMERLOGGER.error("The fileupload has a exception", e);
                return "error";
            }
            return "success";
        }
        
        
        // **************************getter setter方法***********************
        public File[] getMf()
        {
            return this.mf;
        }
        
        public void setMf(File[] mf)
        {
            this.mf = mf;
        }
        
        public String[] getMfFileName()
        {
            return this.mfFileName;
        }
        
        public void setMfFileName(String[] mfFileName)
        {
            this.mfFileName = mfFileName;
        }
        
        public String[] getMfContentType()
        {
            return this.mfContentType;
        }
        
        public void setMfContentType(String[] mfContentType)
        {
            this.mfContentType = mfContentType;
        }
            
    }只要file控件 name=“mf”就行了