Выгрузка аудио
This commit is contained in:
parent
c4350c0b6e
commit
7b5154ece8
@ -5,15 +5,16 @@ import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -52,7 +53,11 @@ public final class IOUtil {
|
||||
writer.write(params.getBytes());
|
||||
writer.write( ("\r\n--" + boundary + "\r\n").getBytes() );
|
||||
writer.write( ("Content-Disposition: form-data; name=\"file\"; filename=\"file." + ext + "\"\r\n").getBytes() );
|
||||
if (ext.contains("mp3")) {
|
||||
writer.write( ("Content-Type: audio/mpeg\r\n").getBytes());
|
||||
} else {
|
||||
writer.write( ("Content-Type: image/" + ext + "\r\n").getBytes());
|
||||
}
|
||||
writer.write("Content-Transfer-Encoding: binary\r\n\r\n".getBytes());
|
||||
writer.write(data);
|
||||
writer.write( ("\r\n--" + boundary + "--\r\n").getBytes() );
|
||||
@ -64,20 +69,29 @@ public final class IOUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] getFile(String file) {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
try (InputStream is = new FileInputStream(file)) {
|
||||
copy(is, bytes);
|
||||
} catch (IOException ioe) { }
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
|
||||
public static byte[] download(String link) {
|
||||
ByteArrayOutputStream uploadParams = new ByteArrayOutputStream();
|
||||
try {
|
||||
final URL url = new URL(link);
|
||||
try (InputStream is = url.openStream()) {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
try (InputStream is = new URL(link).openStream()) {
|
||||
copy(is, bytes);
|
||||
} catch (IOException ioe) { }
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
|
||||
public static void copy(InputStream is, OutputStream os) throws IOException {
|
||||
byte[] byteChunk = new byte[4096];
|
||||
int n;
|
||||
while ((n = is.read(byteChunk)) > 0) {
|
||||
uploadParams.write(byteChunk, 0, n);
|
||||
os.write(byteChunk, 0, n);
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) { }
|
||||
return uploadParams.toByteArray();
|
||||
}
|
||||
|
||||
public static BufferedImage downloadImage(String url) throws IOException {
|
||||
final byte[] imageBytes = IOUtil.download(url);
|
||||
|
@ -1,6 +1,7 @@
|
||||
package holdfast.samobot;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
@ -55,6 +56,10 @@ public class VK {
|
||||
return (new JSONObject(query("photos.getMessagesUploadServer", "v="+API_VERSION)).getJSONObject("response")).getString("upload_url");
|
||||
}
|
||||
|
||||
public static String getAudioUploadServer() {
|
||||
return (new JSONObject(query("audio.getUploadServer", "v="+API_VERSION)).getJSONObject("response")).getString("upload_url");
|
||||
}
|
||||
|
||||
public static String getUnread(int pts) {
|
||||
String result = VK.query("messages.getLongPollHistory", "v="+API_VERSION+"&pts=" + pts);
|
||||
Log.queryln(" > get unread " + pts, Log.GREEN);
|
||||
@ -155,6 +160,28 @@ public class VK {
|
||||
return "photo" + photo.get("owner_id") + "_" + photo.get("pid");
|
||||
}
|
||||
|
||||
public static String uploadAudio(byte[] audioBytes) throws UnsupportedEncodingException {
|
||||
String uploadServer = getAudioUploadServer();
|
||||
Log.queryln(" > upload audio to " + uploadServer);
|
||||
|
||||
String uploadResultRaw = IOUtil.upload(uploadServer, "file=", audioBytes, "mp3");
|
||||
Log.query(" < upload audio result: ");
|
||||
Log.queryln(uploadResultRaw, Log.SMALL);
|
||||
JSONObject uploadResult = new JSONObject(uploadResultRaw);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("server=").append(URLEncoder.encode(uploadResult.get("server").toString(), "UTF-8"));
|
||||
sb.append("&audio=").append(URLEncoder.encode(uploadResult.get("audio").toString(), "UTF-8"));
|
||||
sb.append("&hash=").append(URLEncoder.encode(uploadResult.get("hash").toString(), "UTF-8"));
|
||||
sb.append("&artist=").append(URLEncoder.encode("Леночка", "UTF-8"));
|
||||
|
||||
String audioRaw = VK.query("audio.save", sb.toString());
|
||||
Log.query(" < saveAudio result: ");
|
||||
Log.queryln(audioRaw, Log.SMALL);
|
||||
JSONObject audio = new JSONObject(audioRaw).getJSONObject("response");
|
||||
return "audio" + audio.get("owner_id") + "_" + audio.get("aid");
|
||||
}
|
||||
|
||||
private static String getExtension(String link) {
|
||||
if (link.endsWith(".png")) return "png";
|
||||
return "jpg";
|
||||
|
Loading…
Reference in New Issue
Block a user