Salve galera do guj,
pessoal, me ajuda ai, eu preciso pegar um arquivo e transformá-lo em binary para salvar no sql server. Por exemplo, um file, como que faço pra ele ficar num formato assim:
0xD0CF11E0A1B11AE1000000000000000000000000000000003E000300FEFF0900060000000
Acho que isso é binário mesmo. Existe algum meio de fazer isso? Pegar um arquivo físico, uma música por exemplo, e transformá-lo assim. Me ajuda aí galera porque não consigo achar a solução em lugar nenhum. Eu tentei com esse código da net, mas não faz o que preciso
File file = new File("H:\\Boleto.rpt");
FileInputStream fis = new FileInputStream(file);
//System.out.println(file.exists() + "!!");
//InputStream in = resource.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try
{
for (int readNum; (readNum = fis.read(buf)) != -1;)
{
bos.write(buf, 0, readNum); //no doubt here is 0
//Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
// System.out.println("read " + readNum + " bytes,");
}
}
catch (IOException ex)
{
Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] bytes = bos.toByteArray();
Blob blob = null;
Conexao conexao = new Conexao();
try
{
conexao.abreConexao();
JOptionPane.showMessageDialog(null,conexao.executarUID("insert into teste(cadastro,imagem) values(1," + bytes + ")") > -1);
}
catch (SQLException ex)
{
Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
}
Dá uma mão aí gente.