为什么我的下载文件总是被破坏。
我有一个使用blob上传文件的代码。
代码是
InputStream inputStream = new FileInputStream(new File(filePath)); //the file to upload
pst.setBinaryStream(15, inputStream); //to upload the selected file
它成功上传到我的sql数据库但是当我尝试下载它时,它总是被破坏了。 下面是我从sql下载文件的代码。
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url1 = "jdbc:sqlserver://ets88-spare:1433;databaseName=users;user=sa;password=test";
Connection conn1 = DriverManager.getConnection(url1);
String query1 = "SELECT * FROM ets_verification";
Statement state = conn1.createStatement();
ResultSet rs = state.executeQuery(query1);
while (rs.next())
{
String boardname = jboardName.getSelectedItem().toString();
String sn = jserialNumber.getText();
String status = jverificationStatus.getSelectedItem().toString();
String filename = boardname + "_" + "_" + sn + "_"+ status;
byte[] array = rs.getBytes(16);
FileOutputStream fos = new FileOutputStream("c:\\" + filename + ".rar");
fos.write(array);
fos.close();
System.out.println("array:" + array);
}
}
catch (ClassNotFoundException | SQLException e)
{
jnote.setText(e.toString());
System.out.println("error" + e.toString());
} catch (IOException ex) {
Logger.getLogger(ets_verification.class.getName()).log(Level.SEVERE, null, ex);
}
答案 0 :(得分:0)
你可以试试。 在ORACLE DB中测试。
(...)
Blob blob = rs.getBlob(16);
if (blob != null) {
byte[] b = blob.getBytes(1, (int) blob.length());
try (FileOutputStream fos = new FileOutputStream("PATH/TO/FILE/FILENAME.rar")){
fos.write(file);
}catch (IOException ex) {
//Do something
}
}
(...)