`
wfzhanga
  • 浏览: 68792 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring文件上传下载(文件存存数据库)

阅读更多
1.将二进制数据写入数据库(hibernate):
 
//FileInputStream str;
		try {
			//str = new FileInputStream(enBO.getFileName());
			InputStream fs=enBO.getFile().getInputStream();
			PreparedStatement pstmt;
			pstmt = this.getSession().connection().prepareStatement("insert into file_content values("+enBO.getAttachmentId()+",?)");
			//pstmt.setBinaryStream(1,str,str.available());
			pstmt.setBinaryStream(1,fs,fs.available());
			pstmt.execute();
			pstmt.close();
			fs.close();
			//str.close();
			}
		catch (IOException e) {
				e.printStackTrace();
		}
		catch (HibernateException e) {
				e.printStackTrace();
		} 
		catch (SQLException e) {
				e.printStackTrace();
		}

 

2.二进制文件数据下载
 
FileContent fileContent = contactService.getFileContent(fileContentId);

		response.reset();
		//
		response.addHeader("Content-Disposition", "attachment;filename="
				+ fileContent.getAttachment().getFileName());
		response.addHeader("Content-Length", ""+fileContent.getFileContent().length);
		response.setContentType("bin");
		OutputStream toClient = new BufferedOutputStream(response
				.getOutputStream());
		response.setContentType("application/octet-stream");
		toClient.write(fileContent.getFileContent());
		toClient.flush();
		toClient.close();
 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics