开发者

Simple stream read/write question in java

I'm trying to upload a file via URLConnection, but I need to read/write it as a binary file without any encoding changes.

So i've tried to read byte[] array from a FileInputStream, but now i have an issue. The PrintWriter object I use for outputing to the server does not allow me to do writer.write(content) (where content is of type byte[]). How can i fix this? Or is there another way to quickly copy binary data from a FileInputStream to a Pri开发者_C百科ntWriter?

Thank you


I bet that this is an follow-up on this question: Upload files from Java client to a HTTP server

If you want to upload binary files as well using multipart/form-data, then you need to write them to the OutputStream instead. Here's the changed example of the code as I posted in your previous example, only the try block has been changed to keep a separate handle to the binary output stream, so that you can write any InputStreams to it without any encoding pains:

OutputStream output = null;
PrintWriter writer = null;
try {
    output = connection.getOutputStream();
    writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true); // true = Autoflush, important!

    writer.println("--" + boundary);
    writer.println("Content-Disposition: form-data; name=\"paramToSend\"");
    writer.println("Content-Type: text/plain; charset=UTF-8");
    writer.println();
    writer.println(paramToSend);

    writer.println("--" + boundary);
    writer.println("Content-Disposition: form-data; name=\"fileToUpload\"; filename=\"" + fileToUpload.getName() + "\"");
    writer.println("Content-Type: " + URLConnection.guessContentTypeFromName(fileToUpload.getName());
    writer.println("Content-Transfer-Encoding: binary");
    writer.println();
    InputStream input = null;
    try {
        input = new FileInputStream(fileToUpload);
        byte[] buffer = new byte[1024];
        for (int length = 0; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
        output.flush();
    } finally {
        if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
    }
    writer.println();

    writer.println("--" + boundary + "--");
} finally {
    if (writer != null) writer.close();
}


Writer objects (including PrintWriter) are intended specifically for output of character data. It sounds like you want an OutputStream instead of a Writer here.

Where did your PrintWriter come from? If it was created by wrapping some kind of OutputStream with an OutputStreamWriter and then wrapping that with a PrintWriter, then you should just use the original write(byte[] b) method from the original OutputStream, rather than trying to use a Writer.

If you want to mix character output and byte output, you may need to use String.getBytes(). Check out this example:

OutputStream o = this.conn.getOutputStream(); // Based on your comment
String s = "Hello, world!";
byte[] b = ...;      // These are the raw bytes that you want to write
o.write(s.getBytes("UTF-8"));
o.write(b);

(Of course, this will only work if the system that is reading your output understands that you are writing a mixture of characters and raw bytes and knows how to handle the mixed data that you are sending it.)


You could use 'getOutputStream()' on your URLConnection. Where is the PrintWriter coming from?


You should not use a PrintWriter as that is designed for text representation, and you want binary. A plain OutputStream should do, as Writers all operate on chars, in essence, text.

What do you want to achieve?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜