如何使用Struts2上传文件?

时间:2014-12-04 21:22:04

标签: java mysql jsp file-upload struts2

我正在尝试通过jsp页面中的标记<s:file>将图像放入MySQL数据库中(我正在使用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>

在我的班级里我做了这个:

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

一切看起来都不错,但是当我选择<s:file>的图像时,它只返回所选文件的名称,所以当我尝试将图像放入数据库时​​,它会返回此错误

  

HTTP状态500 - ImgName.jpg(无法找到所选文件)

这是我的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>
   <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>

这是将所选文件处理为method =“POST”

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

1 个答案:

答案 0 :(得分:0)

不要在同一个动作文件中使用这么多动作......它很快就会成为一个噩梦。

当您使用Struts2上传文件时,它是全自动的:默认拦截器堆栈,您没有更改,使用文件上传拦截器来填充三个变量(一个File元素与文件本身,而不仅仅是假设您有setter,请在您的Actions中使用name和两个字符串(fileName和contentType)。

您在表单中也缺少正确的enctype,二进制上传必须是multipart/form-data

那应该是这样的:

JSP

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

动作

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

详细了解this related question