Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Struts2在数据库中上载图像_Java_Struts2 - Fatal编程技术网

Java 使用Struts2在数据库中上载图像

Java 使用Struts2在数据库中上载图像,java,struts2,Java,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</t

我正在尝试使用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>
}

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>
strutsimage
支柱2
org.apache.struts2.dispatcher.FilterDispatcher
支柱2
/*
index.jsp
控制台: 路径C:\Users\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)

请帮忙


谢谢。

动作课上的一点变化解决了我的问题。 这是密码 动作类执行方法

 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;
}

您能发布您收到的异常文本,以便我们看到受影响的行吗?谢谢你只使用了一个拦截器。使用默认堆栈并在其中配置
fileUpload
拦截器。。。通过简单地创建FileInputStream fis=FileInputStream(img)的对象;jdbc:插入ps.setBinaryStream(1,fis,(int)img.length());
  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;
}
 <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>
 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;
}