从Python到Java的请求后重制 我需要将下一段代码从Python重做为Java:

从Python到Java的请求后重制 我需要将下一段代码从Python重做为Java:,java,python,apache,python-requests,httpclient,Java,Python,Apache,Python Requests,Httpclient,我曾尝试将ApacheHttpClient与MultipartEntity一起使用,但没有任何帮助 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("https://trace.moe/api/search"); File file = getImgFile(); MultipartEntity mpEntity = new MultipartEntity();

我曾尝试将ApacheHttpClient与MultipartEntity一起使用,但没有任何帮助

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("https://trace.moe/api/search");
File file = getImgFile();

        MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("image", cbFile);


httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();

System.out.println(response.getStatusLine());
if (resEntity != null) {
    System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
    resEntity.consumeContent();
}

httpclient.getConnectionManager().shutdown();
任何我的尝试都会给出“未收到数据”或空的“文档”。
我能做什么?

好的,经过几分钟的键盘敲击之后,我已经编写了与之配合使用的代码,但它不处理请求代码,也不处理实际的JSON,您需要实现它,它现在会打印出响应。这是你想做的基本开始。这是密码

public static void main(String[] args){
    File file = new File("C:\\Users\\Rab\\Desktop\\lol\\anime.jpg");
    try {
        byte[] fileContent = Files.readAllBytes(file.toPath());
        String imageData = Base64.getEncoder().encodeToString(fileContent);
        search("", imageData);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public static void search(String token, String image){
    try {
        String urlParameters = String.format("token=%s&image=%s", token, image);
        byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
        int postDataLength = postData.length;
        URL url = new URL("https://trace.moe/api/search");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.addRequestProperty("charset", "utf-8");
        conn.addRequestProperty("User-Agent", "Mozilla/4.76");
        conn.addRequestProperty("Content-Length", Integer.toString(postDataLength));
        conn.setUseCaches(false);
        try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
            wr.write(postData);
        }
        InputStream inputStream = conn.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String wtf = br.readLine();
        System.out.println(wtf);

        //Handle your data `wtf` is the response of the server


    } catch (Exception e){
        e.printStackTrace();
    }
}
还有进口

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Base64;

旁注:anime.jpg是

你的标题说你要从java重拍到python,你的帖子说你想从python重拍到java,请下定决心编辑帖子:)
https://whatanime.ga/api/search
不是有效的链接。我的错误是。updatedAlright,我会继续,看看我能做些什么。你能把我可以生成api令牌的网站链接到我这里吗?
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Base64;