日期:2014-05-16  浏览次数:20406 次

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();
?

?

?

?

?

?

?

?

?

?

?

?

?