在 MySQL 中保存 3gp 文件时出现 ArrayIndexOutOfBoundsException

发布于 2024-11-04 15:54:22 字数 1939 浏览 6 评论 0原文

我的代码是:

psmnt = conn.prepareStatement("INSERT INTO upload_data(user_id,photo,audio) " + "values(?,?)");
psmnt.setLong(1, userId);   
fis = new FileInputStream(photoFile);
psmnt.setBinaryStream(2, (InputStream) fis, (int) (photoFile.length()));
fisAudio =  new FileInputStream(audioFile);
psmnt.setBinaryStream(3, (InputStream) fisAudio, (int) (audioFile.length()));
int s = psmnt.executeUpdate();

MySQL数据库中的音频类型是blob。

例外的是:

java.lang.ArrayIndexOutOfBoundsException: 2
    at com.mysql.jdbc.PreparedStatement.setBinaryStream(PreparedStatement.java:2089)
    at com.test.dao.TestDao.saveTestDetails1(TestDAo.java:154)
    at com.test.servlet.UploadImage.doPost(UploadImage.java:131)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)

如何解决这个问题?

My code is:

psmnt = conn.prepareStatement("INSERT INTO upload_data(user_id,photo,audio) " + "values(?,?)");
psmnt.setLong(1, userId);   
fis = new FileInputStream(photoFile);
psmnt.setBinaryStream(2, (InputStream) fis, (int) (photoFile.length()));
fisAudio =  new FileInputStream(audioFile);
psmnt.setBinaryStream(3, (InputStream) fisAudio, (int) (audioFile.length()));
int s = psmnt.executeUpdate();

The type of audio in the MySQL database is a blob.

Exception is:

java.lang.ArrayIndexOutOfBoundsException: 2
    at com.mysql.jdbc.PreparedStatement.setBinaryStream(PreparedStatement.java:2089)
    at com.test.dao.TestDao.saveTestDetails1(TestDAo.java:154)
    at com.test.servlet.UploadImage.doPost(UploadImage.java:131)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)

How to resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪之魂 2024-11-11 15:54:22

psmnt.setBinaryStream(3

Jennifer,您正在尝试设置第三个参数,但插入中只有两个参数:

psmnt = conn.prepareStatement("INSERT INTO upload_data(user_id,photo,audio) "
                        + "values(?,?)");

psmnt.setBinaryStream(3,

Jennifer, you're trying to set the third parameter but you only have two in your insert:

psmnt = conn.prepareStatement("INSERT INTO upload_data(user_id,photo,audio) "
                        + "values(?,?)");
感性不性感 2024-11-11 15:54:22

你的 SQL 语句中只有两个参数(问号),但你试图将二进制流绑定到第三个参数,而不是现有的参数。像这样更改你的 SQL 语句:

psmnt = conn.prepareStatement("INSERT INTO upload_data(user_id,photo,audio) "
                        + "values(?,?,?)");

fisAudio =  new FileInputStream(audioFile);
// you should bind something to the first and second ? marks too!
psmnt.setLong(1, 12345L);
psmnt.setString(2, "this is my photo");
psmnt.setBinaryStream(3, (InputStream) fisAudio, (int) (audioFile.length()));
int s = psmnt.executeUpdate();

you have only two parameters (question marks) in your SQL statement, but you're trying to bind the binary stream to the 3rd, not existing one. change your SQL statement like this:

psmnt = conn.prepareStatement("INSERT INTO upload_data(user_id,photo,audio) "
                        + "values(?,?,?)");

fisAudio =  new FileInputStream(audioFile);
// you should bind something to the first and second ? marks too!
psmnt.setLong(1, 12345L);
psmnt.setString(2, "this is my photo");
psmnt.setBinaryStream(3, (InputStream) fisAudio, (int) (audioFile.length()));
int s = psmnt.executeUpdate();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文