读书人

struts2上传图片到服务器并存路径到数

发布时间: 2012-04-12 15:46:35 作者: rapoo

struts2上传图片到服务器并存路径到数据库出错

HTML code
<form name="addform" action="candiate/candiateadd.action" method="post" enctype ="multipart/form-data"> <input id="zhuceImage" name="upfile"type="file"/> <input id="zhuceBimage" name="upfile" type="file"/> <input id="zhuceName" name="candiate.candiateName" type="text"/></form

Action:
Java code
public class CandiateAction {    private Candiate candiate;         private List<File> upfile;    private List<String> fileFileName;    private List<String> fileContentType;          get  set方法public String candiateadd() throws IOException{        //得到工程保存图片的路径        String address = ServletActionContext.getServletContext().getRealPath("/images/upload");        String relateAddress = "images/upload";//保存到数据库的相对路径        System.out.println(address);        System.out.println(upfile.size());                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");        String dateTime = sdf.format(new Date());        //循环上传的文件        for(int i = 0 ; i < upfile.size() ; i ++){            InputStream is = new FileInputStream(upfile.get(i));            //得到图片保存的位置(根据address来得到图片保存的路径在tomcat下的该工程里)            [color=#FF0000]File destFile = new File(address,this.getFileFileName().get(i)+dateTime);[/color]            [color=#FF0000]this.getFileFileName().get(i)取不到上传文件的名字[/color]            //把图片写入到上面设置的路径里            OutputStream os = new FileOutputStream(destFile);            byte[] buffer = new byte[400];            int length  = 0 ;            while((length = is.read(buffer))>0){                os.write(buffer, 0, length);            }            is.close();            os.close();        }        //将上传的文件路径存入数据库        candiate.setCandiateImage(relateAddress+"/"+this.getFileFileName().get(0)+dateTime);        candiate.setCandiateBimage(relateAddress+"/"+this.getFileFileName().get(1)+dateTime);        return "index";    }}

struts.xml:
XML code
<package name="candiateadd" namespace="/candiate" extends="struts-default">         <action name="candiateadd" class="com.tyut.actions.CandiateAction" method="candiateadd">             <interceptor-ref name="candiateadd">                  <param name="allowedTypes">                       image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png                  </param>             </interceptor-ref>             <interceptor-ref name="defaultStack"/>             <result name="success">/index.jsp</result>         </action>    </package>

web.xml:
XML code
<filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>     <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>

以上是我根据网上搜到的自己写的。运行出现错误,不知道为什么,希望大家能出来解释下,我是新手,希望能够解释详细点,
分立马送上。
错误的提示是这样的:


错误1:
严重: Exception starting filter struts2
Unable to load configuration. - interceptor-ref - file:/D:/Program%20Files/NetBeans%206.7.1/Apache%20Software%20Foundation/Apache%20Tomcat%206.0.18/webapps/zxtp/WEB-INF/classes/struts.xml:91:50
Caused by: Unable to load configuration. - interceptor-ref - file:/D:/Program%20Files/NetBeans%206.7.1/Apache%20Software%20Foundation/Apache%20Tomcat%206.0.18/webapps/zxtp/WEB-INF/classes/struts.xml:91:50
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
... 29 more
Caused by: Unable to find interceptor class referenced by ref-name candiateadd - interceptor-ref - file:/D:/Program%20Files/NetBeans%206.7.1/Apache%20Software%20Foundation/Apache%20Tomcat%206.0.18/webapps/zxtp/WEB-INF/classes/struts.xml:91:50
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:52)
错误2:是我把struts2.xml里的<interceptor-ref name="candiateadd">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
去掉后运行项目提示
java.lang.NullPointerException
com.tyut.actions.CandiateAction.candiateadd(CandiateAction.java:100)
more...
该怎么解决,我是新手,希望能解释的详细点,解决后分马上送上

[解决办法]
参考: http://blog.csdn.net/voyage_mh1987/article/details/5884966
错误的原因:
1: 没有转码
2: 拦截器配置错误
3: 名称没有按照规范命名
4: 没哟设置流格式
5: struct版本不一直导致无法加载配置文件
6: action 配置错误或者没有对应上

HTML code
<?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>    <!-- developer mode convenient for Debug -->    <constant name="struts.i18n.encoding" value="GBK" />    <constant name="struts.ui.theme" value="simple" />    <constant name="struts.ui.templateDir" value="template" />    <constant name="struts.ui.templateSuffix" value="ftl" />    <package name="uploadFile" extends="struts-default">        <action name="myUpload" class="com.UploadAction">            <interceptor-ref name="fileUpload">                            <param name="savePath">/file</param>                <param name="maximumSize">1024000</param>            </interceptor-ref>            <interceptor-ref name="defaultStack" />            <result name="input">/index.jsp</result>            <result name="success">/showUpload.jsp</result>        </action>        <action name="download" class="com.DownLoadAction">            <!-- 设置文件名参数,由页面上传入 -->            <param name="fileName"></param>            <result name="success" type="stream">                <!-- 下载文件类型定义 -->                <param name="contentType">                    application/octet-stream                </param>                <!-- 下载文件处理方法 -->                <param name="contentDisposition">                    attachment;filename="${downloadChineseFileName}"                </param>                <!-- 下载文件输出流定义 -->                <param name="inputName">downloadFile</param>                <param name="bufferSize">4096</param>            </result>        </action>    </package></struts> 


[解决办法]
楼主,你好
现在,专心下来看代码的人少了,而且对于新手,还有很多都不知道其中的原理,我也是最近开始学struts2的。
<input id="zhuceImage" name="upfile" type="file"/>
<input id="zhuceBimage" name="upfile" type="file"/>
注意这里的file的name值,你设的为upfile
那么Action里的绑定的upfile---》对应文件
upfileFileName---》对应名字
把这几个字段该了就行了
特别注意struts2是怎么绑定。
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String dateTime = sdf.format(new Date());
String candiateImageName = dateTime+upfileFilename[0];
楼主记得给分哈
[解决办法]
关于报NULL错误
你jsp页面写的是这样的
<input id="zhuceImage" name="upfile" type="file"/>
<input id="zhuceBimage" name="upfile" type="file"/>
Action里
private List<File> upfile; ---》估计楼主恰好设对了
private List<String> fileFileName; ---》错,应该为upfileFileName
private List<String> fileContentType; ---》错,应该为upfileContentType
this.getFileFileName().get(i)当然报NULL错误了应该为this.getUpfileFileName.get(i)





[解决办法]

探讨

关于报NULL错误
你jsp页面写的是这样的
<input id="zhuceImage" name="upfile" type="file"/>
<input id="zhuceBimage" name="upfile" type="file"/>
Action里
private List<File> upfile; ---》估计楼主恰好设对了
private List<Stri……

读书人网 >J2EE开发

热点推荐