Upload files to cloud server using node.js with Java

Asked 2 years ago, Updated 2 years ago, 44 views

I think I wrote it all down in the title, but I'm currently making an application using javafx as a personal project.

I use Google Cloud Platform to study personally.

Here, we program with node.js.

How do you understand how to upload a file (e.g. a.exe) created by the javafx app mentioned above to the node.js server above??

I don't know because I don't have a lot of experience yet, but I think we need a code to send the file in java and receive the file in node.js. I have no idea.

I can get it by URL, but it is difficult to send it.

java javascript node.js server

2022-09-22 11:43

1 Answers

On the server, you can use the file upload package to receive the upload. https://www.npmjs.com/package/fileupload

Here's an example of the code that you upload from Java.

String url = "http://example.com/upload";
String charset = "UTF-8";
String param = "value";
File textFile = new File("/path/to/file.txt");
File binaryFile = new File("/path/to/file.bin");
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.

URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

try (
    OutputStream output = connection.getOutputStream();
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
    // // Send normal param.
    writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
    writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
    writer.append(CRLF).append(param).append(CRLF).flush();

    // // Send text file.
    writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);
    writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
    writer.append(CRLF).flush();
    Files.copy(textFile.toPath(), output);
    output.flush(); // Important before continuing with writer!
    writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

    // // Send binary file.
    writer.append("--" + boundary).append(CRLF);
    writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
    writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
    writer.append("Content-Transfer-Encoding: binary").append(CRLF);
    writer.append(CRLF).flush();
    Files.copy(binaryFile.toPath(), output);
    output.flush(); // Important before continuing with writer!
    writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

    // // End of multipart/form-data.
    writer.append("--" + boundary + "--").append(CRLF).flush();
}

Source


2022-09-22 11:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.