1.1获取本地文件
File file = new File("E:\\test\\test.pdf");
FileInputStream in = new FileInputStream(file);
1.2保存Blob到数据库
jdbcTemplate.execute(sql, new PreparedStatementCallback() {
@Override
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
// TODO Auto-generated method stub
ps.setString(1, objects[0]);
ps.setString(2, objects[1]);
ps.setBlob(3, in);
ps.execute();
return null;
}
});
2.1 文件转转字符串
File file = new File("E:\\test\\test.pdf");
FileInputStream in = new FileInputStream(file);
int count = 0;
while(count ==0){
count = in.available()
}
byte[] data=new byte[count];
in.read(data);
//jdk1.8后使用此方法
final Base64.Encoder encoder = Base64.getEncoder();
final String encodedText = encoder.encodeToString(data);
System.out.println(encodedText);
2.2保存到数据库
final InputStream in;
out = new ByteArrayInputStream(encodedText);
jdbcTemplate.execute(sql, new PreparedStatementCallback() {
@Override
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
// TODO Auto-generated method stub
ps.setString(1, "id");
ps.setString(2, "xx文件名");
ps.setBlob(3, in);
ps.execute();
return null;
}
});
3.从数据库读取Blob并转成字符串
jdbcTemplate.query(sql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, dtlSerialNo);
}
},new ResultSetExtractor(){
@Override
public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
while(rs.next()){
map.put("fileName", rs.getString("FILENAME"));
Blob blob= rs.getBlob("BILLFILE");
InputStream in = null;
byte[] data = null ;//data数组为文件解析后得到的数组
try {
in = blob.getBinaryStream();
data=new byte[in.available()];
in.read(data);
String content = new String(blob.getBytes((long)1, (int)blob.length()));
map.put("fileStream", content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return null;
}
});
4.下载文件
String toPath = "E://worktools//Desktop//"+map.get("fileName");
InputStream in = new ByteArrayInputStream(map.get("fileStream").getBytes());
File file = new File(toPath);
BASE64Decoder decoder = new BASE64Decoder();
final Base64.Decoder decoder = Base64.getDecoder();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
byte[] buffer = decoder.decode(in);
fos.write(buffer, 0, buffer.length);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
注意:此次代码中未写流关闭,不要忘记哈。
用字节流FileInputStream和FileOutputStream来Copy文件
public class File_Copy {
public static void main(String[] args) throws IOException {
File file=new File("dome.txt");
if(!file.isFile()){ //判断文件是否存在
System.out.println("该文件不存在,无法Copy!");
return;
}
FileInputStream fis=new FileInputStream(file); //"src\\"+file.getName()
FileOutputStream fos=new FileOutputStream("src/"+file.getName());//把文件拷(输出)到src下
byte bytes[]=new byte[1024];
int temp=0; //边读边写
while((temp=fis.read(bytes))!=-1){ //读
fos.write(bytes,0,temp); //写
}
fis.close();
fos.close();
System.out.println("文件拷贝成功!");
}
}