无法将文件上载到数据库

时间:2012-11-19 10:38:13

标签: java blob

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

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

DbConnect Class

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

1 个答案:

答案 0 :(得分:1)

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

FileInputStream fis = null;

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