// see https://s-ul.eu
private static final String APIKEY = "";
public static void main(String[] args)throws IOException {
// file
File file =new File("D:\\temp\\1.png");
InputStream stream =new FileInputStream(file);
byte[] bytesFile =new byte[stream.available()];
stream.read(bytesFile);
String fileName = file.getName();
// request params
Map params =new HashMap<>();
params.put("wizard", "true");
params.put("key", APIKEY);
params.put("client", "sharex-native");
// request url
String url ="https://s-ul.eu/api/v1/upload";
/* request headers, body*/
long ticks =(System.currentTimeMillis()*10000)+621355968000000000L;
String boundary = Long.toHexString(ticks);
String contentType ="multipart/form-data";
contentType +="; boundary=" + boundary;
Set keySet = params.keySet();
Iterator iterator = keySet.iterator();
String content ="";
while(iterator.hasNext()){
String key = iterator.next();
content +="--" + boundary +"\r\nContent-Disposition: form-data; name=\"" + key +"\"\r\n\r\n" + params.get(key) +"\r\n";
}
byte[] bytesArguments = content.getBytes("utf8");
String mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(fileName);
String dataOpen ="--" + boundary +"\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + fileName +"\"\r\nContent-Type: " + mimeType +"\r\n\r\n";
byte[] bytesDataOpen = dataOpen.getBytes("utf8");
String dataClose ="\r\n--" + boundary +"--\r\n";
byte[] bytesDataClose = dataClose.getBytes("utf8");
long contentLength = bytesArguments.length + bytesDataOpen.length + stream.available() + bytesDataClose.length;
String userAgent ="ShareX/12.4.1";
/* request headers, body end*/
// connect
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("content-type", contentType);
connection.setRequestProperty("user-agent", userAgent);
connection.setRequestProperty("content-length", contentLength+"");
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(bytesArguments, 0, bytesArguments.length);
outputStream.write(bytesDataOpen, 0, bytesDataOpen.length);
outputStream.write(bytesFile, 0, bytesFile.length);
outputStream.write(bytesDataClose, 0, bytesDataClose.length);
// read
InputStream inputStream = connection.getInputStream();
byte[] bytesResult =new byte[inputStream.available()];
inputStream.read(bytesResult);
String result =new String(bytesResult);
//{"domain":"xxxxxxxxxxx.s-ul.eu","filename":"aaaaaaaa","protocol":"https://","url":"https://xxxxxxxxxxx.s-ul.eu/aaaaaaaa"}
System.out.println(result);
}
java重写ShareX上传文件到s-ul
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...