2015-04-15 18 views
4

我想使用Struts2上传图像(文件)到数据库中,并且发生异常。我已经尝试过使用多种方式(文件,部分),但它仍然显示空指针异常。使用Struts2在数据库中上传图像

的index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<s:form action="imgup" method="post" enctype="multipart/form-data"> 
<s:file name="img" ></s:file> 
<s:submit value="upload"></s:submit> 
</s:form> 
</body> 
</html> 

struts.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> 

<action name="imgup" class="com.stru.imgupload"> 
<interceptor-ref name="fileUpload"> 
      <param name="maximumSize">2097152</param> 

      <param name="allowedTypes"> 
       image/png,image/gif,image/jpeg,image/pjpeg 
          </param> 
     </interceptor-ref> 
    <interceptor-ref name="defaultStack"></interceptor-ref> 
     <result name="success">index.jsp</result> 

</action> 
</package> 
</struts> 

Action类

package com.stru; 

    import java.io.*; 
    import java.sql.*; 
import java.util.Map; 

    import javax.servlet.http.HttpServletRequest; 

import org.apache.commons.io.FileUtils; 
import org.apache.struts2.interceptor.ServletRequestAware; 

import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 

public class imgupload extends ActionSupport implements ServletRequestAware{  



/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
private HttpServletRequest request; 
private File img; 
private byte[] ip; 
public String getFilename() { 
    return filename; 
} 




public void setFilename(String filename) { 
    this.filename = filename; 
} 




public byte[] getIp() { 
    return ip; 
} 




public void setIp(byte[] ip) { 
    this.ip = ip; 
} 
private String imagecontenttype; 
private String filename; 




public File getImg() { 
    return img; 
} 




public void setImg(File img) { 
    this.img = img; 
} 




public String getImagecontenttype() { 
    return imagecontenttype; 
} 




public void setImagecontenttype(String imagecontenttype) { 
    this.imagecontenttype = imagecontenttype; 
} 









@Override 
public void setServletRequest(HttpServletRequest request) { 
    this.request=request; 

} 
public String execute() 
{ 
    try 
    { 
    String filepath=request.getSession().getServletContext().getRealPath("/"); 
    System.out.print("path"+filepath); 
    File filetocreate = new File(filepath, filename); 
    System.out.print("tocreate"+filetocreate.getName()); 
    FileUtils.copyFile(img, filetocreate); 
    ip = getBytes(filetocreate); 

    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/mdb","root","tiger"); 
    PreparedStatement stmt = con.prepareStatement("insert into strpic(pics) values(?)"); 
    stmt.setBytes(1, ip); 
    int i = stmt.executeUpdate(); 

    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return SUCCESS; 
} 

}

的web.xml

<display-name>strutsimage</display-name> 
<filter> 
<filter-name>struts2</filter-name> 
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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> 
</web-app> 

控制台: 路径C:\用户\ RAVI \ mca_1.metadata.plugins \ org.eclipse.wst.server.core \ TMP0 \ wtpwebapps \ strutsimagejava.lang .NullPointerException 在java.io.File中。(File.java:317) 在com.stru.imgupload.execute(imgupload.java:100)

请帮助。

谢谢。

+0

你能张贴您收到,所以我们可以看到,受影响的线路异常文本?谢谢 – TejjD

+0

你只用一个拦截器为你采取行动。使用默认堆栈并在其中配置'fileUpload'拦截器。 –

+0

感谢所有我的问题解决了... 通过简单地创建一个对象 FileInputStream fis = FileInputStream(img);jdbc:insert ps.setBinaryStream(1,fis,(int)img.length()); –

回答

1

Action类的一些改变解决了我的问题。 下面的代码 Action类的execute方法

public String execute() 
{ 
    try 
    { 
     FileInputStream fis = new FileInputStream(img); 
     //ip = new byte[fis.available()]; 
     //System.out.print("printfis"+fis.available()); 
     //fis.read(ip); 
     //fis.close(); 
    //String filepath=request.getSession().getServletContext().getRealPath("/"); 
    //System.out.print("path"+filepath); 
    //File filetocreate = new File(filepath, filename); 
    //System.out.print("tocreate"+filetocreate.getName()); 
    //FileUtils.copyFile(img, filetocreate); 
    //ip = getBytes(filetocreate); 

    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/mdb","root","tiger"); 
    PreparedStatement stmt = con.prepareStatement("insert into strpic(pics) values(?)"); 
    stmt.setBinaryStream(1, fis, (int)img.length()); 
    int i = stmt.executeUpdate(); 
    if(i>0) 
    { 
     return SUCCESS; 
    } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return SUCCESS; 
}