将文件保存Blob字段,再读取最后下载文件

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("文件拷贝成功!");
    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值