! , . , , , , , .
HTTP- jpg, . - OutputStream Java Socket.
PrintWriter , . , , , .
Printwriter Printwriter , . PrintWriter , ( , ). , .
In the end, you can simply close PrintWriterto close the underlying thread.
If you use the class below, you should:
HttpStreamWriterImplby providing OutputStreamfor the base Socket.- If necessary
writeLine(). - Call
writeBinary()if / if necessary. - By
close()click " close().
Example:
public class HttpStreamWriterImpl implements Closeable
{
private @NotNull OutputStream stream;
private @NotNull PrintWriter printWriter;
public HttpStreamWriterImpl(@NotNull OutputStream stream)
{
this.stream = stream;
this.printWriter = new PrintWriter(stream, true, UTF_8);
}
public void writeLine(@NotNull String line)
{
printWriter.print(line);
printWriter.print(HTTP_LINE_ENDING);
}
public void writeBinary(@NotNull byte[] binaryContent) throws IOException
{
printWriter.flush();
stream.write(binaryContent);
stream.flush();
}
@Override
public void close()
{
printWriter.close();
}
}
source
share