Выгрузка аудио

This commit is contained in:
Victor 2016-03-14 12:01:21 +02:00
parent c4350c0b6e
commit 7b5154ece8
2 changed files with 54 additions and 13 deletions

View File

@ -5,15 +5,16 @@ import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
/** /**
* *
@ -52,7 +53,11 @@ public final class IOUtil {
writer.write(params.getBytes()); writer.write(params.getBytes());
writer.write( ("\r\n--" + boundary + "\r\n").getBytes() ); writer.write( ("\r\n--" + boundary + "\r\n").getBytes() );
writer.write( ("Content-Disposition: form-data; name=\"file\"; filename=\"file." + ext + "\"\r\n").getBytes() ); writer.write( ("Content-Disposition: form-data; name=\"file\"; filename=\"file." + ext + "\"\r\n").getBytes() );
writer.write( ("Content-Type: image/" + 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("Content-Transfer-Encoding: binary\r\n\r\n".getBytes());
writer.write(data); writer.write(data);
writer.write( ("\r\n--" + boundary + "--\r\n").getBytes() ); writer.write( ("\r\n--" + boundary + "--\r\n").getBytes() );
@ -64,19 +69,28 @@ public final class IOUtil {
} }
} }
public static byte[] download(String link) { public static byte[] getFile(String file) {
ByteArrayOutputStream uploadParams = new ByteArrayOutputStream(); ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try { try (InputStream is = new FileInputStream(file)) {
final URL url = new URL(link); copy(is, bytes);
try (InputStream is = url.openStream()) {
byte[] byteChunk = new byte[4096];
int n;
while ((n = is.read(byteChunk)) > 0) {
uploadParams.write(byteChunk, 0, n);
}
}
} catch (IOException ioe) { } } catch (IOException ioe) { }
return uploadParams.toByteArray(); return bytes.toByteArray();
}
public static byte[] download(String link) {
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) {
os.write(byteChunk, 0, n);
}
} }
public static BufferedImage downloadImage(String url) throws IOException { public static BufferedImage downloadImage(String url) throws IOException {

View File

@ -1,6 +1,7 @@
package holdfast.samobot; package holdfast.samobot;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; 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"); 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) { public static String getUnread(int pts) {
String result = VK.query("messages.getLongPollHistory", "v="+API_VERSION+"&pts=" + pts); String result = VK.query("messages.getLongPollHistory", "v="+API_VERSION+"&pts=" + pts);
Log.queryln(" > get unread " + pts, Log.GREEN); Log.queryln(" > get unread " + pts, Log.GREEN);
@ -155,6 +160,28 @@ public class VK {
return "photo" + photo.get("owner_id") + "_" + photo.get("pid"); 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) { private static String getExtension(String link) {
if (link.endsWith(".png")) return "png"; if (link.endsWith(".png")) return "png";
return "jpg"; return "jpg";