当前位置: 代码迷 >> J2EE >> struts2上传图片到服务器并存路径到数据库出错解决办法
  详细解决方案

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

热度:55   发布时间:2016-04-22 01:47:48.0
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>

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