0

I have a method can convert to a data to byte array and i must post it to a php web site. I am gonna use a java socket now. preparing that if i did i will add it here

it is data to byte[] methods , i found it in PDF to byte array and vice versa

public static byte[] readFully(InputStream stream) throws IOException
    {
byte[] buffer = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int bytesRead;
while ((bytesRead = stream.read(buffer)) != -1)
{
    baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
    }
public static byte[] loadFile(String sourcePath) throws IOException
{
InputStream inputStream = null;
try 
{
    inputStream = new FileInputStream(sourcePath);
    return readFully(inputStream);
} 
finally
{
    if (inputStream != null)
    {
        inputStream.close();
    }
}
}

Here how to post a byte to php web site...

public  static void postMybyte (String out)
{
    try {
            // Construct data
            String data = URLEncoder.encode(out, "UTF-8") ;

            // Send data
            URL url = new URL("");
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the response
           BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
               System.out.println(" --->>>>"+line);
            }
            wr.close();
            rd.close();
    } catch (Exception e) 
    {

    }
}  
Community
  • 1
  • 1
Yasin Caner
  • 131
  • 1
  • 5
  • 16
  • 5
    A BLOB is a *binary large object* that is usually only needed in databases. So maybe you don't actually *need* a BLOB to solve your problem!? BLOB is rather complicated, BLOBs are usually created *on* the database, not locally. – Andreas Dolk Jan 07 '13 at 10:11
  • http://stackoverflow.com/questions/6662813/easiest-way-to-convert-byte-array-into-blob-in-java?rq=1 - looks like a duplicate but actually isn't. This works with a mysql database. – Andreas Dolk Jan 07 '13 at 10:13
  • Your comment should be an answer, @Andreas_D. Me too, I think the OP does not need a Blob Object at all. – Fildor Jan 07 '13 at 10:15
  • Okey , thanks 1st question is okey for me, i missunderstand my subject, soryy 4 that, i must post my binary file to a php web site with socket. well i need search it thanks , i am gonna change my question now – Yasin Caner Jan 07 '13 at 10:18

0 Answers0