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

// see https://s-ul.euprivate static final String APIKEY = ""; public static void main(String[] args)throws IOException {// fileFile 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 paramsMap params =new HashMap<>(); params.put("wizard", "true"); params.put("key", APIKEY); params.put("client", "sharex-native"); // request urlString 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*/// connectHttpURLConnection 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); // readInputStream 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); }

    推荐阅读