mirror of
https://github.com/ahabhyde/miguelbridge
synced 2025-01-10 06:24:20 +01:00
Aggiunta richiesta post di upload file funzionante
This commit is contained in:
parent
c4bce1ac74
commit
0f305fb327
@ -77,11 +77,18 @@ public class MatrixBot {
|
||||
public String login() throws IOException, ParseException, URISyntaxException {
|
||||
String requestUrl = homeUrl + "client/r0/login";
|
||||
|
||||
/*
|
||||
String[][] reqParams = new String[][] {
|
||||
{"type", "m.login.password"},
|
||||
{"user", readBotUserName()},
|
||||
{"password", readPswd()}
|
||||
};
|
||||
*/
|
||||
|
||||
JSONObject reqParams = new JSONObject();
|
||||
reqParams.put("type", "m.login.password");
|
||||
reqParams.put("user", readBotUserName());
|
||||
reqParams.put("password", readPswd());
|
||||
|
||||
String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams);
|
||||
|
||||
@ -94,7 +101,7 @@ public class MatrixBot {
|
||||
String requestUrl = homeUrl + String.format("client/r0/rooms/%s/join?access_token=%s",
|
||||
roomAddress, accessToken);
|
||||
|
||||
String[][] reqParams = null;
|
||||
JSONObject reqParams = new JSONObject();
|
||||
String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams);
|
||||
|
||||
return risposta[0];
|
||||
@ -104,20 +111,54 @@ public class MatrixBot {
|
||||
String requestUrl = homeUrl + String.format("client/r0/rooms/%s/send/m.room.message?access_token=%s",
|
||||
roomAddress, accessToken);
|
||||
|
||||
/*
|
||||
String[][] reqParams = new String[][] {
|
||||
{"msgtype", "m.text"},
|
||||
{"body", message}
|
||||
};
|
||||
*/
|
||||
JSONObject reqParams = new JSONObject();
|
||||
reqParams.put("msgtype", "m.text");
|
||||
reqParams.put("body", message);
|
||||
|
||||
String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams);
|
||||
|
||||
return risposta[0] + " - " + risposta[1];
|
||||
}
|
||||
|
||||
public synchronized String sendFile(String roomAddress, File file) throws IOException {
|
||||
public synchronized String sendFile(String roomAddress, File file) throws IOException, URISyntaxException, ParseException {
|
||||
String requestUrl = homeUrl + String.format("media/r0/upload?filename=%s&access_token=%s",
|
||||
file.getName(), accessToken);
|
||||
String[] risposta = RequestHandler.postRequestFile(requestUrl, file);
|
||||
|
||||
JSONObject uriFileObj = (JSONObject) new JSONParser().parse(risposta[1]);
|
||||
String uriFile = (String) uriFileObj.get("content_uri");
|
||||
|
||||
System.out.println("Il file è " + uriFile);
|
||||
requestUrl = homeUrl + String.format("client/r0/rooms/%s/send/m.room.message?access_token=%s",
|
||||
roomAddress, accessToken);
|
||||
|
||||
/*
|
||||
String[][] reqParams = new String[][] {
|
||||
{"msgtype", "m.image"},
|
||||
{"body", "image.png"},
|
||||
{"url", uriFile}
|
||||
};
|
||||
*/
|
||||
|
||||
JSONObject reqParams = new JSONObject();
|
||||
JSONObject objInfo = new JSONObject();
|
||||
|
||||
objInfo.put("mimetype", "image/png");
|
||||
objInfo.put("size", file.length());
|
||||
|
||||
reqParams.put("info", objInfo);
|
||||
reqParams.put("msgtype", "m.file");
|
||||
reqParams.put("body", "image.png");
|
||||
reqParams.put("url", uriFile);
|
||||
|
||||
risposta = RequestHandler.postRequestJSON(requestUrl, reqParams);
|
||||
|
||||
return risposta[0] + " - " + risposta[1];
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,6 @@
|
||||
package com.em.miguelbridge.matrixbot;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URLEncoder;
|
||||
@ -13,13 +10,18 @@ import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.methods.RequestBuilder;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.entity.mime.content.FileBody;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
/**
|
||||
@ -48,7 +50,7 @@ public class RequestHandler {
|
||||
return risposta;
|
||||
}
|
||||
|
||||
public static String[] postRequestJSON(String inUrl, String[][] reqParams) throws IOException, URISyntaxException {
|
||||
public static String[] postRequestJSON(String inUrl, JSONObject inObj) throws IOException, URISyntaxException {
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpPost post = new HttpPost(inUrl);
|
||||
|
||||
@ -56,16 +58,8 @@ public class RequestHandler {
|
||||
post.setHeader("User-Agent", "Mozilla/5.0");
|
||||
post.addHeader("Content-Type", "charset=UTF_8");
|
||||
|
||||
JSONObject obj = new JSONObject();
|
||||
if (reqParams != null) {
|
||||
for (String[] param : reqParams)
|
||||
obj.put(param[0], param[1]);
|
||||
}
|
||||
|
||||
String jsonString = obj.toJSONString();
|
||||
|
||||
|
||||
StringEntity requestEntity = new StringEntity(obj.toJSONString(), ContentType.APPLICATION_JSON);
|
||||
String jsonString = inObj.toJSONString();
|
||||
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
||||
post.setEntity(requestEntity);
|
||||
|
||||
HttpResponse response = client.execute(post);
|
||||
@ -84,19 +78,17 @@ public class RequestHandler {
|
||||
}
|
||||
|
||||
public static String[] postRequestFile(String inUrl, File file) throws IOException {
|
||||
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
HttpPost httpPost = new HttpPost(inUrl);
|
||||
httpPost.setHeader("Content-Type", "application/file");
|
||||
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||||
builder.addPart("content", fileBody);
|
||||
HttpEntity entity = builder.build();
|
||||
|
||||
HttpPost request = new HttpPost(inUrl);
|
||||
request.setEntity(entity);
|
||||
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpResponse response = client.execute(request);
|
||||
byte[] b = new byte[(int) file.length()];
|
||||
FileInputStream fileInputStream = new FileInputStream(file);
|
||||
fileInputStream.read(b);
|
||||
|
||||
HttpEntity entity = new ByteArrayEntity(b);
|
||||
httpPost.setEntity(entity);
|
||||
HttpResponse response = httpClient.execute(httpPost);
|
||||
|
||||
BufferedReader rd = new BufferedReader(
|
||||
new InputStreamReader(response.getEntity().getContent()));
|
||||
|
@ -1,27 +1,15 @@
|
||||
package test;
|
||||
|
||||
import com.em.miguelbridge.matrixbot.MatrixBot;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.entity.mime.content.FileBody;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class UploadTest {
|
||||
public static void main(String[] args) throws IOException, ParseException, URISyntaxException {
|
||||
/*
|
||||
MatrixBot bot = new MatrixBot();
|
||||
String token = bot.login();
|
||||
String url = "https://maxwell.ydns.eu/_matrix/media/r0/" +
|
||||
@ -51,5 +39,14 @@ public class UploadTest {
|
||||
result.append(line);
|
||||
|
||||
System.out.println(response.getStatusLine() + " -e- " + result.toString());
|
||||
*/
|
||||
|
||||
MatrixBot bot = new MatrixBot();
|
||||
String token = bot.login();
|
||||
bot.setAccessToken(token);
|
||||
File file = new File("prova.png");
|
||||
//bot.sendMessage("provaa", "!mPkXwqjuGdhEVSopiG:maxwell.ydns.eu");
|
||||
|
||||
System.out.println(bot.sendFile("!mPkXwqjuGdhEVSopiG:maxwell.ydns.eu", file));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user