URLConnection image recording

I am trying to write an image through HttpURLConnection.

I know how to write text, but I'm having real problems trying to write an image

I was able to record local HD using ImageIO:

But I'm trying to write Image ImageIO at the url and failed

URL url = new URL(uploadURL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "multipart/form-data;
                                            boundary=" + boundary);
output = new DataOutputStream(connection.getOutputStream());
output.writeBytes("--" + boundary + "\r\n");
output.writeBytes("Content-Disposition: form-data; name=\"" + FIELD_NAME + "\";
                                            filename=\"" + fileName + "\"\r\n");
output.writeBytes("Content-Type: " + dataMimeType + "\r\n");
output.writeBytes("Content-Transfer-Encoding: binary\r\n\r\n");
ImageIO.write(image, imageType, output);

uploadURL is the URL of the asp page on the server that will upload the image with the file name specified in the "Content-Plate: part.

now when i submit this asp page find the query and find the file name. but does not find the file to download.

The problem is that when writing ImageIO at the URL, what name of the file on which ImageIO is written,

, , , ImageIO URLConnection , asp,

, ,

+3
2

, io.flush(), io.close() .

. , , . , asp, , , HTTP, , . image/jpeg.

, , , , , :

    URL url = new URL("http://localhost:8080/handler");
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type", "image/jpeg");
    con.setRequestMethod("POST");
    InputStream in = new FileInputStream("c:/temp/poc/img/mytest2.jpg");
    OutputStream out = con.getOutputStream();
    copy(in, con.getOutputStream());
    out.flush();
    out.close();
    BufferedReader r = new BufferedReader(new  InputStreamReader(con.getInputStream()));


            // obviously it is not required to print the response. But you have
            // to call con.getInputStream(). The connection is really established only
            // when getInputStream() is called.
    System.out.println("Output:");
    for (String line = r.readLine(); line != null;  line = r.readLine()) {
        System.out.println(line);
    }

copy(), Jakarta IO utils. :

protected static long copy(InputStream input, OutputStream output)
        throws IOException {
    byte[] buffer = new byte[12288]; // 12K
    long count = 0L;
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

, POST. , .

+4

OP , :

// main method 
URL url = new URL(uploadURL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true); // triggers "POST"
// connection.setDoInput(true); // only if needed
connection.setUseCaches(false); // dunno
final String boundary = Long.toHexString(System.currentTimeMillis());
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="
                                                                + boundary);
output = new DataOutputStream(connection.getOutputStream());
try {
     // image must be a File instance
    flushMultiPartData(image, output, boundary);
} catch (IOException e) {
    System.out.println("IOException in flushMultiPartData : " + e);
    return;
}
// ...
private void flushMultiPartData(File file, OutputStream serverOutputStream,
            String boundary) throws FileNotFoundException, IOException {
    // SEE /questions/582/using-javaneturlconnection-to-fire-and-handle-http-requests/9336#9336
    PrintWriter writer = null;
    try {
        // true = autoFlush, important!
        writer = new PrintWriter(new OutputStreamWriter(serverOutputStream,
                charsetForMultipartHeaders), true);
        appendBinary(file, boundary, writer, serverOutputStream);
        // End of multipart/form-data.
        writer.append("--" + boundary + "--").append(CRLF);
    } finally {
        if (writer != null) writer.close();
    }
}

private void appendBinary(File file, String boundary, PrintWriter writer,
        OutputStream output) throws FileNotFoundException, IOException {
    // Send binary file.
    writer.append("--" + boundary).append(CRLF);
    writer.append(
        "Content-Disposition: form-data; name=\"binaryFile\"; filename=\""
            + file.getName() + "\"").append(CRLF);
    writer.append("Content-Type: "
            +  URLConnection.guessContentTypeFromName(file.getName()))
            .append(CRLF);
    writer.append("Content-Transfer-Encoding: binary").append(CRLF);
    writer.append(CRLF).flush();
    InputStream input = null;
    try {
        input = new FileInputStream(file);
        byte[] buffer = new byte[1024];
        for (int length = 0; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
        output.flush(); // Important! Output cannot be closed. Close of
        // writer will close output as well.
    } finally {
        if (input != null) try {
            input.close();
        } catch (IOException logOrIgnore) {}
    }
    writer.append(CRLF).flush(); // CRLF is important! It indicates end of
    // binary boundary.
}

, Gzip - . , GZIPOutputStream Gzip . ImageIO - ImageIO . @BalusC answer

0

Source: https://habr.com/ru/post/1789680/


All Articles