-1
//Convert binary image file to byte array to base64 encoded string
FileInputStream mFileInputStream = new FileInputStream("C:\\basicsworkspace\\base64upload\\src\\main\\resources\\basic.png");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead = 0;
while ((bytesRead = mFileInputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] ba = bos.toByteArray();
byte[] encoded = Base64.getEncoder().encode(ba);
connection = DriverManager.getConnection(connectionString);
String insertSql = "INSERT INTO test (image) VALUES "
+ "("+encoded+")";
System.out.println(insertSql);
prepsInsertProduct = connection.prepareStatement(
insertSql);
System.out.println(prepsInsertProduct.execute());
イメージをSQL Serverに挿入しようとしましたが、イメージをbase64形式にする必要があります。私は例外以下になっています。どのようなタイプのデータ型をSQL Serverにbase64として挿入するのか教えてください。Base64イメージをSQL Serverデータベースに挿入しようとしています
出力:
INSERT INTO test (image) VALUES ([[email protected])
java.sql.SQLException: Invalid SQL statement or JDBC escape, terminating ']' not found.
at net.sourceforge.jtds.jdbc.SQLParser.parse(SQLParser.java:1270)
at net.sourceforge.jtds.jdbc.SQLParser.parse(SQLParser.java:165)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.<init>(JtdsPreparedStatement.java:111)
at net.sourceforge.jtds.jdbc.JtdsConnection.prepareStatement(JtdsConnection.java:2492)
at net.sourceforge.jtds.jdbc.JtdsConnection.prepareStatement(JtdsConnection.java:2450)
at base64upload.base64upload.App.main(App.java:70)
']'見つからないが、そのような値の終わりに、あなたのクエリに ']'を追加([Bする@ 7229724f'] ') –
@ZaidYasyafのおかげで、あなたはしましたこのアドバイスで私の一日を作った – Andremoniy
なぜバイトを最初にBase64エンコードするのですか?あなたは不必要にスペースを無駄にするでしょう。 – Kayaman