How do I load a file using Struts2?

I am trying to put an image into a MySQL database using a tag <s:file>

in a jsp page (I am using Struts 2):

<s:form action="carica" id="carica" style="display:none">
    <s:file id="carica" name="caricaimg"></s:file>
    <s:submit value="Carica" ></s:submit>
</s:form>

      

and in my class i did the following:

public String carica() throws SQLException, FileNotFoundException{
    Connessione();   // DB connection method
    System.out.print(caricaimg);
    File file = new File(caricaimg);
    InputStream fin = new java.io.FileInputStream(file);
    int fileLength = (int)file.length();
    PreparedStatement pstmt = con.prepareStatement("INSERT INTO Utenti (NomeImg, Immagine) VALUES (?, ?)");
    pstmt.setString(1, file.getName());
    pstmt.setBinaryStream (2, fin, fileLength);
    pstmt.executeUpdate();
    return "success";
}

      

Everything looks fine, but when I select the image with <s:file>

, it only returns the name of the selected file, so when I try to put the image in the DB, it returns this error

HTTP Status 500 - ImgName.jpg (Could not find the selected file)

This is my struts.xml file

    <?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>
   <package name="Model" extends="struts-default">
      <action name="dati" class="Model.Registrazione" method="execute">
            <result name="success">/RegistrazioneRiuscita.jsp</result>
      </action>
      <action name="login">
            <result>/Login.jsp</result>
      </action>
      <action name="acces" class="Model.Registrazione" method="accesso">
            <result name="success">/LoginRiuscito.jsp</result>
            <result name="fail">/LoginFallito.jsp</result>
      </action>
      <action name="modifica" class="Model.Registrazione" method="modifica">
            <result name="success">/ModificaRiuscita.jsp</result>
            <result name="fail">/ModificaFallita.jsp</result>
      </action>
      <action name="elimina" class="Model.Registrazione" method="elimina">
            <result name="success">/EliminatoSuccesso.jsp</result>
      </action>
      <action name="carica" class="Model.Registrazione" method="carica">
            <result name="success">/index.jsp</result>
      </action>
      </package>
</struts>

      

This is the part that treats the selected file as a method = "POST"

<action name="carica" class="Model.Registrazione" method="carica">
    <result name="success">/index.jsp</result>
</action>

      

+3


source to share


1 answer


Don't use the same action file with so many actions inside ... it will soon become a mantain's nightmare.

BTW, when you upload a file using Struts2, everything is automatic: the default interceptor stack, which you haven't changed, uses the file upload interceptor to populate three variables (the File element with the file itself, not just the name and two strings for the filename and contentType ) in your actions if you have setters.

You are also missing the enctype right on your form, which for binary uploads should be multipart/form-data

.

Then it should be like this:

JSP



<s:form action="carica" enctype="multipart/form-data">
    <s:file name="caricaimg" />
    <s:submit value="Carica" />
</s:form>

      

Act

private File caricaimg;
private String caricaimgFileName;
private String caricaimgContentType;

/* getters and setters here */

public String carica() throws SQLException, FileNotFoundException{
    Connessione();   // DB connection method
    System.out.print(caricaimg);

    // not needed -> caricaimg is already a file !!
    // File file = new File(caricaimg);
    // InputStream fin = new java.io.FileInputStream(file);
    // int fileLength = (int)file.length();

    InputStream fin = new java.io.FileInputStream(caricaimg);
    int fileLength = (int)caricaimg.length();
    PreparedStatement pstmt = con.prepareStatement("INSERT INTO Utenti (NomeImg, Immagine) VALUES (?, ?)");

    // not needed -> you already have the fileName
    // pstmt.setString(1, file.getName());

    pstmt.setString(1, caricaimgFileName);
    pstmt.setBinaryStream (2, fin, fileLength);
    pstmt.executeUpdate();

    // DON'T FORGET TO CLOSE THE STREAM !!
    fin.close();
    // ---------

    return "success";
}

      

More on this related question .

0


source







All Articles