mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-25 15:00:09 +08:00
Fix build
This commit is contained in:
parent
64e36ea7b1
commit
00a44a1df8
2
.gitignore
vendored
2
.gitignore
vendored
@ -42,3 +42,5 @@ local.properties
|
||||
# Maven publishing credits
|
||||
keys.properties
|
||||
/plugins/
|
||||
|
||||
token.txt
|
@ -1,8 +0,0 @@
|
||||
|
||||
|
||||
fun getGitToken():String{
|
||||
with(System.getProperty("user.dir") + "/token.txt"){
|
||||
println("reading token file in " + this)
|
||||
return File(this).readText()
|
||||
}
|
||||
}
|
@ -1,73 +0,0 @@
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Base64;
|
||||
|
||||
public class GitUploader {
|
||||
public static boolean create(String url, File file) {
|
||||
long begin = System.currentTimeMillis();
|
||||
System.out.println("上传开始...");
|
||||
//StringBuffer result = new StringBuffer();
|
||||
BufferedReader in = null;
|
||||
HttpURLConnection conn = null;
|
||||
try {
|
||||
URL realUrl = new URL(url);
|
||||
conn = (HttpURLConnection) realUrl.openConnection();
|
||||
conn.setConnectTimeout(120000);
|
||||
conn.setReadTimeout(120000);
|
||||
// 设置
|
||||
conn.setDoOutput(true); // 需要输出
|
||||
conn.setDoInput(true); // 需要输入
|
||||
conn.setUseCaches(false); // 不允许缓存
|
||||
conn.setRequestMethod("PUT"); // 设置PUT方式连接
|
||||
|
||||
conn.setRequestProperty("Content-Type", "application/json");
|
||||
conn.setRequestProperty("Authorization", "token " + getGitToken());
|
||||
conn.setRequestProperty("User-Agent", "Github File Uploader App");
|
||||
conn.connect();
|
||||
// 传输数据
|
||||
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
|
||||
// 传输json头部
|
||||
dos.writeBytes("{\"message\":\".\",\"content\":\"");
|
||||
// 传输文件内容
|
||||
byte[] buffer = new byte[1024 * 1002]; // 3的倍数
|
||||
RandomAccessFile raf = new RandomAccessFile(file, "r");
|
||||
long size = raf.read(buffer);
|
||||
while (size > -1) {
|
||||
if (size == buffer.length) {
|
||||
dos.write(Base64.getEncoder().encode(buffer));
|
||||
} else {
|
||||
byte tmp[] = new byte[(int) size];
|
||||
System.arraycopy(buffer, 0, tmp, 0, (int) size);
|
||||
dos.write(Base64.getEncoder().encode(tmp));
|
||||
}
|
||||
size = raf.read(buffer);
|
||||
}
|
||||
raf.close();
|
||||
// 传输json尾部
|
||||
dos.writeBytes("\"}");
|
||||
dos.flush();
|
||||
dos.close();
|
||||
|
||||
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
//result.append(line).append("\n");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("发送PUT请求出现异常!");
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
} catch (Exception e2) {
|
||||
}
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.printf("上传结束,耗时 %ds\n", (end - begin) / 1000);
|
||||
//result.toString()
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
69
buildSrc/src/main/kotlin/upload/GitToken.kt
Normal file
69
buildSrc/src/main/kotlin/upload/GitToken.kt
Normal file
@ -0,0 +1,69 @@
|
||||
package upload
|
||||
|
||||
import java.io.*
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.util.*
|
||||
|
||||
object GitToken {
|
||||
|
||||
private fun getGitToken(): String {
|
||||
with(System.getProperty("user.dir") + "/token.txt") {
|
||||
println("reading token file in $this")
|
||||
return File(this).readText()
|
||||
}
|
||||
}
|
||||
|
||||
fun upload(file: File, url: String) {
|
||||
val begin = System.currentTimeMillis()
|
||||
println("上传开始...")
|
||||
//StringBuffer result = new StringBuffer();
|
||||
//StringBuffer result = new StringBuffer();
|
||||
var `in`: BufferedReader? = null
|
||||
var conn: HttpURLConnection? = null
|
||||
conn = URL(url).openConnection() as HttpURLConnection
|
||||
conn.connectTimeout = 120000
|
||||
conn.readTimeout = 120000
|
||||
// 设置
|
||||
conn.doOutput = true // 需要输出
|
||||
conn.doInput = true // 需要输入
|
||||
conn.useCaches = false // 不允许缓存
|
||||
conn.requestMethod = "PUT" // 设置PUT方式连接
|
||||
conn.setRequestProperty("Content-Type", "application/json")
|
||||
conn.setRequestProperty("Authorization", "token " + getGitToken())
|
||||
conn.setRequestProperty("User-Agent", "Github File Uploader App")
|
||||
conn.connect()
|
||||
// 传输数据
|
||||
val dos = DataOutputStream(conn.outputStream)
|
||||
// 传输json头部
|
||||
dos.writeBytes("{\"message\":\".\",\"content\":\"")
|
||||
// 传输文件内容
|
||||
val buffer = ByteArray(1024 * 1002) // 3的倍数
|
||||
val raf = RandomAccessFile(file, "r")
|
||||
var size: Long = raf.read(buffer).toLong()
|
||||
while (size > -1) {
|
||||
if (size == buffer.size.toLong()) {
|
||||
dos.write(Base64.getEncoder().encode(buffer))
|
||||
} else {
|
||||
val tmp = ByteArray(size.toInt())
|
||||
System.arraycopy(buffer, 0, tmp, 0, size.toInt())
|
||||
dos.write(Base64.getEncoder().encode(tmp))
|
||||
}
|
||||
size = raf.read(buffer).toLong()
|
||||
}
|
||||
raf.close()
|
||||
// 传输json尾部
|
||||
dos.writeBytes("\"}")
|
||||
dos.flush()
|
||||
dos.close()
|
||||
`in` = BufferedReader(InputStreamReader(conn.inputStream))
|
||||
var line: String?
|
||||
while (`in`.readLine().also { line = it } != null) {
|
||||
//result.append(line).append("\n");
|
||||
}
|
||||
val end = System.currentTimeMillis()
|
||||
System.out.printf("Upload finished within %d seconds\n", (end - begin) / 1000)
|
||||
//result.toString()
|
||||
//result.toString()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user