java重写ShareX上传文件到s-ul

// 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);

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 当你们还在纠结“渔网”流行的时尚问题时,我们的带货女王大幂幂当天穿一件运动风上衣,口罩遮面现身机场~衣品简单舒适!...
    时尚潮流达人阅读 234评论 0 0
  • 20岁那年,我开始减肥,同时也开始了我的暴食记。 1. 我小的时候因为没吃母乳长大,体质差,虽然出生的时候,以八斤...
    圆儿宝阅读 1,082评论 28 14
  • 今天是第一天开始正式记日记了,以前虽然也想过,但是毕竟最终没有实现,这次在跑步后的走路期间记的日记,算是一时二用吧...
    洛织阅读 182评论 0 0
  • 从早上7点出发到现在在徐家汇站等9号线。眼镜痛到不行。其实就那么一个场景,来来回回拍,几百号群演留到最后。抵触心理...
    夏安和草帽阅读 269评论 0 0