gpt4 book ai didi

java - 无法将文件上传到数据库

转载 作者:行者123 更新时间:2023-12-01 15:02:24 24 4
gpt4 key购买 nike

我使用以下代码将文件上传到数据库中,该代码对于图像工作正常,而对于其他文件则不起作用,我最后发布了堆栈跟踪。

我的第二个值是一个 blob

   String strFilePath = null;
Hashtable<Object,Object> fileTable = null;
InputStream input = null;
CosUploadFile file = null;

fileTable = multiPartFormData.getFiles();
file = (CosUploadFile)fileTable.get("filepath");
input =file.getInpuStream();

prepare = connection.prepareStatement("insert into all_files values(?,?,?)");
prepare.setString(1, strFileSplit[0]);
prepare.setBinaryStream(2,input);
prepare.setString(3,strFileSplit[1]);
prepare.execute();

错误:

J2CA0206W: A connection error occurred.  To help determine the problem, enable the Diagnose Connection Usage option on the Connection Factory or Data Source.
J2CA0056I: The Connection Manager received a fatal connection error from the Resource Adapter for resource datasource. The exception is: java.sql.SQLRecoverableException: Io exception: Connection reset by peer: socket write error:java.net.SocketException: Connection reset by peer: socket write error
com.ibm.websphere.ce.cm.StaleConnectionException: Io exception: Connection reset by peer: socket write error

这是我尝试上传文档文件时的堆栈跟踪。对此我该怎么办。

编辑:以下是我的连接代码

DBConnect dbConnect = new DBConnect();
Connection connection = dbConnect.connect();

DbConnect 类

  public Connection connect() 
{
Connection con = null;
try
{
InitialContext context = new InitialContext();
DataSource datasource = (DataSource)context.lookup("datasource");
con = datasource.getConnection("TRAIN2012", "xyz123");
return con;
}

最佳答案

尝试以下操作,但最好将图像存储在文件系统中并将文件的位置存储在数据库中。可以找到关于此的冗长讨论 here .

FileInputStream fis = null;

File image = new File("\\yourpath\test.PNG");
fis = new FileInputStream(image);
prepare.setBinaryStream(2,fis,(int)image.length());
prepare.execute();

关于java - 无法将文件上传到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13452061/

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