0

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

我的第二个值是 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

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

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

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

数据库连接类

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

1 回答 1

1

尝试以下方法,但是最好将图像存储在文件系统中并将文件的位置存储在数据库中。在这里可以找到关于这个的冗长讨论 。

FileInputStream fis = null;

File image = new File("\\yourpath\test.PNG");
fis = new FileInputStream(image);
prepare.setBinaryStream(2,fis,(int)image.length());
prepare.execute();
于 2012-11-19T10:56:23.580 回答