您的位置:程序门 -> java -> 框架、开源



非常郁闷action接收form出错 各位请来看看


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


非常郁闷action接收form出错 各位请来看看
发表于:2008-01-16 11:54:32 楼主
2008-1-16   11:42:20   org.apache.struts.action.requestprocessor   processexception
警告:   unhandled   exception   thrown:   class   java.lang.nullpointerexception
2008-1-16   11:42:20   org.apache.catalina.core.standardwrappervalve   invoke
严重:   servlet.service()   for   servlet   action   threw   exception
java.lang.nullpointerexception
at   com.struts.action.uploadfileaction.EXECute(uploadfileaction.java:73)
at   org.apache.struts.action.requestprocessor.processactionperform(requestprocessor.java:419)
at   org.apache.struts.action.requestprocessor.process(requestprocessor.java:224)
at   org.apache.struts.action.actionservlet.process(actionservlet.java:1194)
at   org.apache.struts.action.actionservlet.dopost(actionservlet.java:432)
at   javax.servlet.http.httpservlet.service(httpservlet.java:710)
at   javax.servlet.http.httpservlet.service(httpservlet.java:803)
at   org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:269)
at   org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:188)
at   org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:213)
at   org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:174)
at   org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:127)
at   org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:117)
at   org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:108)
at   org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:151)
at   org.apache.coyote.http11.http11aprprocessor.process(http11aprprocessor.java:834)
at   org.apache.coyote.http11.http11aprprotocol$http11connectionhandler.process(http11aprprotocol.java:640)
at   org.apache.tomcat.util.net.aprendpoint$worker.run(aprendpoint.java:1286)
at   java.lang.thread.run(unknown   source)
发表于:2008-01-16 11:55:161楼 得分:0
jsp页面

<%@   page   language="java"   contenttype="text/html;   charset=gb2312"%>
<%@   taglib   uri="http://struts.apache.org/tags-bean"   prefix="bean"%>
<%@   taglib   uri="http://struts.apache.org/tags-html"   prefix="html"%>
<%@   taglib   uri="http://struts.apache.org/tags-logic"   prefix="logic"%>
<%@   taglib   uri="http://struts.apache.org/tags-tiles"   prefix="tiles"%>


<!doctype   html   public   "-//w3c//dtd   html   4.01   transitional//en">
<html>
<head>
<title> uploadfiles.jsp </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>
<form   action="uploadfile.do?action=addfile"enctype="multipart/form-data"   method="post">
<table   align="center"   width="200px">
<tr>
<td   nowrap="nowrap">
文件名:
</td>
<td>
<input   type="file"   name="filename"   />
</td>
</tr>
<tr>
<td   nowrap="nowrap">
备注
</td>
<td>
<input   type="textarea"   name="memo"   />
</td>
</tr>
<tr>
<td   colspan="2"   align="center">
<input   type="submit"   name="submit"   value="增加资料"/>
</td>
</tr>
</table>
</form>
</body>
</html>
发表于:2008-01-16 11:55:332楼 得分:0
form

/*
  *   generated   by   myeclipse   struts
  *   template   path:   templates/java/javaclass.vtl
  */
package   com.struts.form;

import   javax.servlet.http.httpservletrequest;

import   org.apache.struts.action.actionerrors;
import   org.apache.struts.action.actionform;
import   org.apache.struts.action.actionmapping;
import   org.apache.struts.upload.formfile;
import   org.apache.struts.upload.multipartrequesthandler;

/**
  *   myeclipse   struts   creation   date:   01-16-2008
  *  
  *   xdoclet   definition:
  *  
  *   @struts.form   name="uploadfileform"
  */
public   class   uploadfileform   extends   actionform   {

private   string   memo;

private   formfile   filename   =   null;

public   formfile   getfilename()   {
return   filename;
}

public   void   setfilename(formfile   filename)   {
this.filename   =   filename;
}

public   string   getmemo()   {
return   memo;
}

public   void   setmemo(string   memo)   {
this.memo   =   memo;
}

public   actionerrors   validate(actionmapping   mapping,
httpservletrequest   request)   {
actionerrors   errors   =   new   actionerrors();
if(filename==null){
system.out.println("filename为null");
}
return   errors;
}

public   void   reset(actionmapping   mapping,   httpservletrequest   request)   {
//   todo   auto-generated   method   stub
}
}
发表于:2008-01-16 11:55:533楼 得分:0
/*
  *   generated   by   myeclipse   struts
  *   template   path:   templates/java/javaclass.vtl
  */
package   com.struts.action;

import   java.io.file;
import   java.io.fileoutputstream;
import   java.io.ioexception;
import   java.io.inputstream;
import   java.io.outputstream;
import   java.io.printwriter;
import   java.sql.connection;
import   java.sql.sqlexception;
import   java.text.simpledateformat;
import   java.util.date;

import   javax.servlet.http.httpservletrequest;
import   javax.servlet.http.httpservletresponse;
import   javax.servlet.http.httpsession;
import   javax.sql.datasource;

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.actionmessage;
import   org.apache.struts.action.actionmessages;
import   org.apache.struts.upload.formfile;
import   com.struts.form.uploadfileform;

import   com.struts.dao.filedao;

/**
  *   myeclipse   struts   creation   date:   01-16-2008
  *  
  *   xdoclet   definition:
  *  
  *   @struts.action   path="/uploadfile"   name="uploadfileform"   input="/seeload.jsp"
  *                                 scope="request"   validate="true"
  */
public   class   uploadfileaction   extends   action   {
/*
  *   generated   methods
  */

/**
  *   method   EXECute
  *  
  *   @param   mapping
  *   @param   form
  *   @param   request
  *   @param   response
  *   @return   actionforward
  */
public   actionforward   EXECute(actionmapping   mapping,   actionform   form,
httpservletrequest   request,   httpservletresponse   response)   {
string   encoding   =   request.getcharacterencoding();
if((encoding   !=   null)&&(encoding.equalsignorecase("utf-8"))){
response.setcontenttype("text/html;charset=gb2312");//   如果没有指定编码,编码格式为gb2312
}

uploadfileform   uploadfileform   =   (uploadfileform)   form;
formfile   filename   =   uploadfileform.getfilename();
string   memo   =   uploadfileform.getmemo();

datasource   ds   =   null;
connection   conn   =   null;
httpsession   session   =   null;

try{
ds   =   getdatasource(request,"a");
conn   =   ds.getconnection();

filedao   sqldao   =   new   filedao(conn);

string   action   =   request.getparameter("action");
actionmessages   errors   =   new   actionmessages();

//   增加文件
if(action.equals("addfile")){
//   传递文件
if(filename.getfilename().trim().length()> 0){
//   取得上传时间
simpledateformat   dateformat1   =   new   simpledateformat("yyy-mm-dd");
date   dt   =   new   date();
string   uptime   =   dateformat1.format(dt);

string   tempname="";

try{
//   开始上传文件
string   filepath   =   this.getservlet().getservletcontext().getrealpath("/");//   取得当前路径
inputstream   stream   =   filename.getinputstream();//   把文件读入

//   建立一个上传文件的输入流如果是linux系统请把"\"换成"/"

system.out.println(filename.getfilename());

int   position   =   filename.getfilename().indexof(".");
string   type   =   filename.getfilename().substring(position);

string   fileid   =   sqldao.getmaxfileid();

tempname   =   fileid   +   type;

outputstream   bos   =   new   fileoutputstream(filepath   +   "upfiles"+"'\'"   +   tempname);

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(exception   e){
errors.add(actionmessages.global_message   ,   new   actionmessage("file.add.failure"))   ;
                                                if   (   !errors.isempty()){
                                                saveerrors(request,   errors)   ;
                                                }
                                                return   new   actionforward(mapping.getinput());
}
//写入到数据库记录
if(!sqldao.insernew(tempname,   filename.getfilename(),   memo,   uptime)){
errors.add(actionmessages.global_message,new   actionmessage("file.add.failure"));
if(!errors.isempty()){
saveerrors(request,errors);
}
return   new   actionforward(mapping.getinput());
}
removeformbean(mapping,request);
return   new   actionforward(mapping.getinput());
}
}
}catch(sqlexception   e){
e.printstacktrace();
}finally{
try{
if(conn   !=   null){
conn.close();
}
}catch(sqlexception   e){
e.printstacktrace();
}
}
return   null;

}

protected   void   removeformbean(actionmapping   mapping,
httpservletrequest   request)   {
if   (mapping.getattribute()   !=   null)
if   ("request".equals(mapping.getscope()))   {
request.removeattribute(mapping.getattribute());
}   else   {
httpsession   session   =   request.getsession();
session.removeattribute(mapping.getattribute());
}
}

}
发表于:2008-01-16 11:56:354楼 得分:0
struts-comfig

<?xml   version="1.0"   encoding="utf-8"?>
<!doctype   struts-config   public   "-//apache   software   foundation//dtd   struts   configuration   1.2//en"   "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
    <data-sources   />
    <form-beans   >
        <form-bean   name="uploadfileform"   type="com.struts.form.uploadfileform"   />

    </form-beans>

    <global-exceptions   />
    <global-forwards   />
    <action-mappings   >
        <action
            attribute="uploadfileform"
            input="/uploadfiles.jsp"
            name="uploadfileform"
            path="/uploadfile"
            scope="request"
            type="com.struts.action.uploadfileaction"   >
            <forward   name="listfiles"   path="/seeload.jsp"   />
        </action>


    </action-mappings>

    <message-resources   parameter="com.struts.applicationresources"   />
</struts-config>

发表于:2008-01-17 22:05:495楼 得分:0
<form       action="/uploadfile.do?action=addfile"enctype="multipart/form-data"       method="post">  

      试一试


快速检索

最新资讯
热门点击