gpt4 book ai didi

java - 如何使用 Struts2 上传文件?

转载 作者:行者123 更新时间:2023-11-29 01:03:04 25 4
gpt4 key购买 nike

我正在尝试通过标记 <s:file> 将图像放入 MySQL 数据库中在 jsp 页面中(我使用的是 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 Status 500 - ImgName.jpg (Unable to found selected file)

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

最佳答案

不要使用同一个 Action 文件,里面有那么多 Action ……它很快就会成为维护的噩梦。

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

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

那么应该是这样的:

JSP

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

Action

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 的信息.

关于java - 如何使用 Struts2 上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27304332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com