小编典典

无法通过Struts2中的jdbc从数据库存储和检索图像数据

jsp

我正在使用struts2。我试图将图像的路径存储到数据库中并将图像存储在服务器中,然后想要在jsp页面中检索这些图像数据。我需要指导。但我得到一个错误

java.lang.NullPointerException

我创建了3个班级。注册(bean类)registrationService(jdbc代码)registrationaction

注册.java

public class Registration{

  private  String first_name;
  private String last_name;
  private String gender;
  private String email_id;
  private String password;
  private  String dob;
  private String father_name;
  private  String mother_name;
  private    String contact;
   private   String mobile;
   private  String address;
   private  String city;
   private  String country;
   private  String graduation;
   private  String graduate_school_mark;
   private  String graduation_year;
   private    String inter_school;
  private   String inter_school_mark;
  private   String inter_year;
  private   String high_school;
  private   String high_school_mark;
  private   String high_school_year;
  private   String role;
  private   String salary;
  private   File fphoto;
  private File fresume;
  private   String pre_company;

// getter和setter

RegistrationService.java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.struts2.ServletActionContext;

public class RegistrationService {
public boolean filledRegistration(Registration registration) throws SQLException, ClassNotFoundException, FileNotFoundException, IOException{

  Class.forName("com.mysql.jdbc.Driver");
  Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/erp","root","ali$");
  String sql = "insert into registration(first_name,last_name,password,gender,email_id,dob,"
                 + "father_name,mother_name,contact,mobile,address,city,country,graduation,"
                 + "graduation_marks,graduation_year,inter,inter_marks,inter_year,high_school,"
                 + "high_marks,high_year,role,salary,resume,photo,pre_comp) value(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    PreparedStatement p= con.prepareStatement(sql);
                         p.setString(1,registration.getFirst_name());
                        p.setString(2,registration.getLast_name());
                         p.setString(3,registration.getPassword());
                        p.setString(4,registration.getGender());
                        p.setString(5,registration.getEmail_id()); 
                        p.setString(6,registration.getDob());   
                       p.setString(7,registration.getFather_name());
                        p.setString(8,registration.getMother_name());
                        p.setString(9,registration.getContact());
                        p.setString(10,registration.getMobile());
                        p.setString(11,registration.getAddress());
                        p.setString(12,registration.getCity());
                        p.setString(13,registration.getCountry());
                        p.setString(14,registration.getGraduation());
                        p.setString(15,registration.getGraduate_school_mark());
                        p.setString(16,registration.getGraduation_year());
                        p.setString(17,registration.getInter_school());
                        p.setString(18,registration.getInter_school_mark());
                        p.setString(19,registration.getInter_year());
                        p.setString(20,registration.getHigh_school());
                        p.setString(21,registration.getHigh_school_mark());
                        p.setString(22,registration.getHigh_school_year());
                        p.setString(23,registration.getRole());
                        p.setString(24,registration.getSalary());

                        FileInputStream fis1 = new FileInputStream(registration.getFphoto());
                        p.setBinaryStream(25,fis1);

                        FileInputStream fis=new FileInputStream(registration.getFphoto());
                        p.setBinaryStream(26, fis);

                        p.setString(27,registration.getPre_company());

                       String s= ServletActionContext.getRequest().getRealPath("");
                        System.out.println(s);
                        int temp= s.lastIndexOf("\\");
                        String s1= s.substring(0, temp);

                     //   System.out.println("---------->"+s1);

                        int temp1 = s1.lastIndexOf("\\");
                        String s2= s1.substring(0, temp1);

                         FileOutputStream fos=new FileOutputStream(s2+"\\web\\Images\\image"+registration.getEmail_id()+".jpg");
                          byte buffer[]=new byte[1024];
                            int read=0;
                                   while((read=fis.read(buffer, 0, 1024))!=-1){
                                      fos.write(buffer);
                                      }
                                   fis.close();
                        fos.close();

                         int i= p.executeUpdate();
                          if(i>0){
                                return true;
                                 }
                return false;       
        }
        }

RegistrationAction.java

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.io.IOException;
import java.sql.SQLException;
public class RegistrationAction extends ActionSupport implements ModelDriven{
    Registration registration=new Registration();

    public Registration getRegistration() {
        return registration;
    }

    public void setRegistration(Registration registration) {
        this.registration = registration;
    }
    @Override
    public String execute() throws ClassNotFoundException, SQLException, IOException{
    RegistrationService rs= new RegistrationService();
    if( rs.filledRegistration(registration)){
        return "registered";
    } else {
    }
    return "failed";
}

    @Override
    public Object getModel() {
       return registration;
    }
}

welcome.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <s:property value="user.email"/>
        <s:property value="user."/>
        <h1>Welcome</h1>
        <s:property value="user.user_name"/>
        <s:form action="logoutAction">
        <s:submit value="logout"/>
        <%
    String s=(String)request.getContextPath();
    String path=s+"/Images/"+user.email+".jpg";


%>

<img src="<%=path%>" width="200" height="200" />

        </s:form>
    </body>
</html>

玻璃鱼服务器

Severe:   Exception occurred during processing request: null
java.lang.NullPointerException
    at java.io.FileInputStream.<init>(FileInputStream.java:130)
    at package_registration.RegistrationService.filledRegistration(RegistrationService.java:53)
    at package_registration.RegistrationAction.execute(RegistrationAction.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)

阅读 242

收藏
2020-06-10

共1个答案

小编典典

满足您要求的正确设计是:-

1)将图像路径存储到数据库。
2)将图像存储在服务器中,并使用路径访问这些图像。

现在,您的Registration类的fphoto应该为String类型。因为您要存储图像路径,而不是将实际图像存储在db中。

获得文件路径后,创建一个File对象,并将其传递给FileOutputStream,该文件可用于在jsp上显示图像。

2020-06-10