Advertisement
quhluh

Pastebin

Dec 8th, 2023
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. 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.
  2.  
  3. First, add the unirest library to your project. You can do this using Maven:
  4.  
  5. xml
  6. Copy code
  7. <dependencies>
  8.     <dependency>
  9.         <groupId>com.konghq</groupId>
  10.         <artifactId>unirest-java</artifactId>
  11.         <version>3.11.12</version>
  12.     </dependency>
  13. </dependencies>
  14. Then, you can use the following Java code to read a file and upload its content to Pastebin:
  15.  
  16. java
  17. Copy code
  18. import com.konghq.unirest.HttpResponse;
  19. import com.konghq.unirest.JsonNode;
  20. import com.konghq.unirest.Unirest;
  21.  
  22. import java.io.IOException;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.Paths;
  26.  
  27. public class PastebinUploader {
  28.  
  29.     private static final String API_KEY = "YOUR_PASTEBIN_API_KEY";
  30.     private static final String PASTE_ENDPOINT = "https://pastebin.com/api/api_post.php";
  31.  
  32.     public static void main(String[] args) {
  33.         try {
  34.             // Read file content
  35.             String filePath = "path/to/your/file.txt";
  36.             String fileContent = readFileContent(filePath);
  37.  
  38.             // Upload content to Pastebin
  39.             String pasteUrl = uploadToPastebin(fileContent);
  40.  
  41.             // Print the Pastebin URL
  42.             System.out.println("Pastebin URL: " + pasteUrl);
  43.         } catch (IOException e) {
  44.             e.printStackTrace();
  45.         }
  46.     }
  47.  
  48.     private static String readFileContent(String filePath) throws IOException {
  49.         Path path = Paths.get(filePath);
  50.         byte[] fileBytes = Files.readAllBytes(path);
  51.         return new String(fileBytes);
  52.     }
  53.  
  54.     private static String uploadToPastebin(String content) {
  55.         HttpResponse<JsonNode> response = Unirest.post(PASTE_ENDPOINT)
  56.                 .field("api_dev_key", API_KEY)
  57.                 .field("api_paste_code", content)
  58.                 .field("api_paste_private", "0") // 0 = public, 1 = unlisted, 2 = private
  59.                 .field("api_paste_expire_date", "1D") // Paste expiration, e.g., 1D (1 day)
  60.                 .asString();
  61.  
  62.         if (response.getStatus() == 200) {
  63.             return response.getBody().getObject().getString("paste_url");
  64.         } else {
  65.             System.err.println("Failed to upload to Pastebin. Response: " + response.getStatusText());
  66.             return null;
  67.         }
  68.     }
  69. }
  70. 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