我用来读取sqlserver数据库中二进制数据读到文件中是这样的
我的代码部分是这样的:StringstrSQL="";bytedata[]=newbyte[]{};Filefile=null;FileOutputStreamfos=n...
我的代码部分是这样的:
String strSQL = "";
byte data[] = new byte[] {};
File file = null;
FileOutputStream fos = null;
InputStream in = null;
PDMDataBase db = new PDMDataBase();
ResultSet rs = null;
strSQL = "select * from UploadFiles where id ='10'";
rs = db.ExecuteQuerySQL(strSQL);
try {
while (rs.next()) {
file = new File("D:/" + rs.getString("filename"));
data = rs.getBytes("files");
if (!file.exists()) {
file.createNewFile();// 如果文件不存在,创建
}
fos = new FileOutputStream(file);
int size = 0;
if (data.length > 0) {
fos.write(data, 0, data.length);
System.out.println("********************");//输出正常
} else {
while ((size = in.read(data)) != -1) {
fos.write(data, 0, size);
}
in.close();
}
fos.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
请有经验或者知道怎么回事儿的人帮帮忙,解答一下,感激...... 展开
String strSQL = "";
byte data[] = new byte[] {};
File file = null;
FileOutputStream fos = null;
InputStream in = null;
PDMDataBase db = new PDMDataBase();
ResultSet rs = null;
strSQL = "select * from UploadFiles where id ='10'";
rs = db.ExecuteQuerySQL(strSQL);
try {
while (rs.next()) {
file = new File("D:/" + rs.getString("filename"));
data = rs.getBytes("files");
if (!file.exists()) {
file.createNewFile();// 如果文件不存在,创建
}
fos = new FileOutputStream(file);
int size = 0;
if (data.length > 0) {
fos.write(data, 0, data.length);
System.out.println("********************");//输出正常
} else {
while ((size = in.read(data)) != -1) {
fos.write(data, 0, size);
}
in.close();
}
fos.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
请有经验或者知道怎么回事儿的人帮帮忙,解答一下,感激...... 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询