Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- If you want to create a Java program that reads a file and uploads its content to Pastebin using their API, you can use the Pastebin API and a library like unirest. Make sure to obtain your API key from Pastebin by creating an account on their website.
- First, add the unirest library to your project. You can do this using Maven:
- xml
- Copy code
- <dependencies>
- <dependency>
- <groupId>com.konghq</groupId>
- <artifactId>unirest-java</artifactId>
- <version>3.11.12</version>
- </dependency>
- </dependencies>
- Then, you can use the following Java code to read a file and upload its content to Pastebin:
- java
- Copy code
- import com.konghq.unirest.HttpResponse;
- import com.konghq.unirest.JsonNode;
- import com.konghq.unirest.Unirest;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- public class PastebinUploader {
- private static final String API_KEY = "YOUR_PASTEBIN_API_KEY";
- private static final String PASTE_ENDPOINT = "https://pastebin.com/api/api_post.php";
- public static void main(String[] args) {
- try {
- // Read file content
- String filePath = "path/to/your/file.txt";
- String fileContent = readFileContent(filePath);
- // Upload content to Pastebin
- String pasteUrl = uploadToPastebin(fileContent);
- // Print the Pastebin URL
- System.out.println("Pastebin URL: " + pasteUrl);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static String readFileContent(String filePath) throws IOException {
- Path path = Paths.get(filePath);
- byte[] fileBytes = Files.readAllBytes(path);
- return new String(fileBytes);
- }
- private static String uploadToPastebin(String content) {
- HttpResponse<JsonNode> response = Unirest.post(PASTE_ENDPOINT)
- .field("api_dev_key", API_KEY)
- .field("api_paste_code", content)
- .field("api_paste_private", "0") // 0 = public, 1 = unlisted, 2 = private
- .field("api_paste_expire_date", "1D") // Paste expiration, e.g., 1D (1 day)
- .asString();
- if (response.getStatus() == 200) {
- return response.getBody().getObject().getString("paste_url");
- } else {
- System.err.println("Failed to upload to Pastebin. Response: " + response.getStatusText());
- return null;
- }
- }
- }
- Make sure to replace "YOUR_PASTEBIN_API_KEY" with your actual Pastebin API key. Also, specify the correct file path in the filePath variable.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement