我有一种方法可以将数据转换为字节数组,我必须将其发布到 php 网站。我现在要使用 java 套接字。准备如果我这样做了,我会在这里添加
它是字节 [] 方法的数据,我在PDF 中找到它到字节数组,反之亦然
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();
}
}
}
在这里如何将字节发布到 php 网站...
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)
{
}
}