I revised this code to use java.net.http — this works better if you need to make a modular application since apache commons has yet to implement app modules so you can’t package without a big fuss.
public static long uploadFile(File f, int counter, int size){
String uri = "https://www.YOURDOMAIN.com/wp-json/wp/v2/media";
String extension = "";
String mime = "";
String encoded_cred = Base64.getEncoder().encodeToString(("ACCOUNT:PASSWORD").getBytes());
long mediaID = -1;
String fileName = f.getName();
int i = fileName.lastIndexOf('.');
if (i > 0) {
extension = fileName.substring(i + 1);
}
if (extension.equalsIgnoreCase("png")) {
mime = "image/png";
}
if (extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg")) {
mime = "image/jpeg";
}
Path fp = f.toPath();
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
.POST(HttpRequest.BodyPublishers.ofFile(fp) )
.headers("AUTHORIZATION", "Basic " + encoded_cred,
"Content-Disposition", "attachment; filename=" + f.getName(),
"Content-Type", mime
)
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body()); // output response body
}catch (Exception e){e.printStackTrace(); AppendLog.appendLog("Upload failed for file: " + f.getName(), consoleLog);}
return mediaID;
}
additional implementation of upload button using Vaadin to get file for the uploadFile step:
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.upload.Upload;
import com.vaadin.flow.component.upload.receivers.MultiFileMemoryBuffer;
import com.vaadin.flow.server.StreamResource;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class SetImages {
public static Component imageUpload(ArrayList<File> setImages, Div setHolder){
Random random = new Random();
var layout = new HorizontalLayout();
var label = new Label("Select set images");
MultiFileMemoryBuffer buffer = new MultiFileMemoryBuffer();
Upload upload = new Upload(buffer);
upload.addSucceededListener(event -> {
String fileName = event.getFileName();
InputStream fileData = buffer.getInputStream(fileName);
int randFile = random.nextInt();
File f = null;
try {
f = File.createTempFile("setimg-" + randFile, ".jpg");
}catch (IOException IE) {IE.printStackTrace();}
Image img = new Image(new StreamResource(f.getName(), () ->{
try{
int rand = random.nextInt();
File imageFile = File.createTempFile("setimg-" + rand, ".jpg");
FileUtils.copyInputStreamToFile(fileData, imageFile);
setImages.add(imageFile);
return new FileInputStream(imageFile);
} catch (IOException fnf){fnf.printStackTrace();}
return null;
}), "alt text");
img.setWidth("250px");
img.setHeight("400px");
setHolder.add(img);
});
layout.setAlignItems(FlexComponent.Alignment.CENTER);
layout.add(label, upload);
return layout;
}
}
2
Answers
You can upload an image file (of certain file types -- common image formats are enabled by default (full WP accepted upload formats list here: https://wordpress.com/support/accepted-filetypes/ )
The code I used to accomplish this is as follows:
My pomfile used these, though versions aren't necessarily the same:
Answer 2:
I revised this code to use java.net.http — this works better if you need to make a modular application since apache commons has yet to implement app modules so you can’t package without a big fuss.
additional implementation of upload button using Vaadin to get file for the uploadFile step: