2017-05-08 11 views
0

私は、cloudconvertというドキュメントコンバーターAPIを使用しています。彼らは公式のJavaライブラリを持っていませんが、サードパーティのJavaオプションを持っています。ちょっとカスタマイズが必要なので、githubプロジェクトをクローンしてプロジェクトに追加しました。私は、.epubファイルをcloudconvertに送信し、その代わりに.pdfファイルを取得しています。私がデフォルト設定を使用すると問題なく動作し、自分の.epubを.pdfに正しく変換します。それを実現させるコードは次のとおりです。ここでjerseyを使用してJavaで入れ子になったjson POSTリクエストを送信するにはどうすればよいですか?

は、変換をトリガするものである:

// Create service object 
    CloudConvertService service = new CloudConvertService("api-key"); 

    // Create conversion process 
    ConvertProcess process = service.startProcess(convertFrom, convertTo); 

    // Perform conversion 
    //convertFromFile is a File object with a .epub extension 
    process.startConversion(convertFromFile); 

    // Wait for result 
    ProcessStatus status; 
    waitLoop: 
    while (true) { 
     status = process.getStatus(); 
     switch (status.step) { 
      case FINISHED: 
       break waitLoop; 
      case ERROR: 
       throw new RuntimeException(status.message); 
     } 
     // Be gentle 
     Thread.sleep(200); 
    } 
    //Download result 
    service.download(status.output.url, convertToFile); 

    //lean up 
    process.delete(); 

startConversion()呼び出し:

public void startConversion(File file) throws ParseException, FileNotFoundException, IOException { 
    if (!file.exists()) { 
     throw new FileNotFoundException("File not found: " + file); 
    }  
    startConversion(new FileDataBodyPart("file", file));   
} 

実際にジャージを使用してPOSTリクエストを送信するために、これを呼び出します。これまで

private void startConversion(BodyPart bodyPart) { 
    if (args == null) { 
     throw new IllegalStateException("No conversion arguments set."); 
    } 
    MultiPart multipart = new FormDataMultiPart() 
       .field("input", "upload") 
       .field("outputformat", args.outputformat) 
       .bodyPart(bodyPart); 
    //root is a class level WebTarget object 
    root.request(MediaType.APPLICATION_JSON).post(Entity.entity(multipart, multipart.getMediaType())); 
} 

すべてが機能していることを指摘する。私の問題は、変換が起こったときに.pdfが返すものが非常に小さいことです。 cloudconvertは、これらのマージンを変更する方法を提供します。オプションのjsonパラメータconverteroptionsを送信し、マージンを手動で設定することができます。私は郵便配達員を使ってこれをテストしましたが、それは問題なく動作し、適切にフォーマットされた余白文書を得ることができました。それが可能であることを知っている。ここで私が使用POSTMAN情報は次のとおりです。ここで

@POST : https://host123d1qo.cloudconvert.com/process/WDK9Yq0z1xso6ETgvpVQ 
Headers: 'Content-Type' : 'application/json' 
Body: 
    { 
    "input": "base64", 
    "file": "0AwAAIhMAAAAA", //base64 file string that is much longer than this 
    "outputformat": "pdf", 
    "converteroptions": { 
     "margin_bottom": 75, 
     "margin_top": 75, 
     "margin_right": 50, 
     "margin_left": 50 
    } 
} 

が正しくフォーマットされたPOSTリクエストを取得で私の試みをしている、私は非常にジャージを経験していないよ、私はstackoverflowの上で見つけた答えのカップルは動作しませんでした私のために。

試行1、私はMultipart.fieldとしてjson文字列を追加しようとしました。それは私に何の誤りも与えず、変換された.pdfファイルを返しましたが、余白は変更されていないので、私はそれを正しく送り返すべきではありません。私はそれがPOSTMANで働いていたとき、それは「BASE64」として「入力」タイプを使用していた

private void startConversion(BodyPart bodyPart) { 
    String jsonString = "{\"margin_bottom\":75,\"margin_top\":75,\"margin_right\":50,\"margin_left\":50}"; 
    MultiPart multipart = new FormDataMultiPart() 
        .field("input", "upload") 
        .field("outputformat", args.outputformat) 
        .field("converteroptions", jsonString) 
        .bodyPart(bodyPart); 
    root.request(MediaType.APPLICATION_JSON).post(Entity.entity(multipart, multipart.getMediaType())); 
} 

試み2は、ので、私はそれにそれを変更しようとしたが、それはこの時点では無い、全く何も返しませんリクエストエラー、5分のタイムアウトエラー。

//I pass in a File object rather than the bodypart object. 
private void startConversion(File file) { 
    byte[] encoded1 = Base64.getEncoder().encode(FileUtils.readFileToByteArray(file)); 
    String encoded64 = new String(encoded1, StandardCharsets.US_ASCII); 
    String jsonString = "{\"margin_bottom\":75,\"margin_top\":75,\"margin_right\":50,\"margin_left\":50}"; 

    MultiPart multipart = new FormDataMultiPart() 
       .field("input", "base64") 
       .field("outputformat", args.outputformat) 
       .field("file", encoded64) 
       .field("converteroptions", jsonString); 
    root.request(MediaType.APPLICATION_JSON).post(Entity.entity(multipart, multipart.getMediaType())); 
} 

試み3は、適切にジャージJSON POSTリクエストを送信する方法についてグーグルでいくつかの後、私は、フォーマットを変更しました。今回は400の不正なリクエストエラーが返されました。

private void startConversionPDF(File file) throws IOException { 
    byte[] encoded1 = Base64.getEncoder().encode(FileUtils.readFileToByteArray(file)); 
    String encoded64 = new String(encoded1, StandardCharsets.US_ASCII); 

    String jsonString = "{\"input\":\"base64\",\"file\":\"" + encoded64 + "\",\"outputformat\":\"pdf\",\"converteroptions\":{\"margin_bottom\":75,\"margin_top\":75,\"margin_right\":50,\"margin_left\":50}}"; 
    root.request(MediaType.APPLICATION_JSON).post(Entity.json(jsonString)); 
} 

試行4、誰かがあなたが手作業でjsonStringを使用する必要はないと言っていますが、シリアライズ可能なJava Beanを使用する必要があります。そこで、対応するクラスを作成し、以下のようなリクエストを行いました。同じ400の不正なリクエストエラー。

@XmlRootElement 
public class PDFConvert implements Serializable { 
    private String input; 
    private String file; 
    private String outputformat; 
    private ConverterOptions converteroptions; 
    //with the a default constructor and getters/setters for all 
} 
@XmlRootElement 
public class ConverterOptions implements Serializable { 
    private int margin_bottom; 
    private int margin_top; 
    private int margin_left; 
    private int margin_right; 
    //with the a default constructor and getters/setters for all 
} 

private void startConversionPDF(File file) throws IOException { 
    byte[] encoded1 = Base64.getEncoder().encode(FileUtils.readFileToByteArray(file)); 
    String encoded64 = new String(encoded1, StandardCharsets.US_ASCII); 
    PDFConvert data = new PDFConvert(); 
    data.setInput("base64"); 
    data.setFile(encoded64); 
    data.setOutputformat("pdf"); 
    ConverterOptions converteroptions = new ConverterOptions(); 
    converteroptions.setMargin_top(75); 
    converteroptions.setMargin_bottom(75); 
    converteroptions.setMargin_left(50); 
    converteroptions.setMargin_right(50); 
    data.setConverteroptions(converteroptions); 

    root.request(MediaType.APPLICATION_JSON).post(Entity.json(data)); 
} 

私は、これは、テキストの非常に壁である知っているが、私は誰の時間を無駄にしないように、私が試したすべての異なるものを見せたかったです。この仕事をしなければならないかもしれない助けやアイデアをありがとう。私は本当にジャージーと仕事をしたいと思っています。なぜなら、私は完璧に働く他のコンバージョンをいくつか持っているからです。コンバータオプションは必要ありません。また、POSTMANを使用して手動でプロセスを実行しているときに機能するため、その可能性もわかっています。

Cloudconvert api documentation for starting a conversion

Github repo with the recommended 3rd party java library I am using/modifying

答えて

0

私はついにそれを考え出しました。試行錯誤の時間。これを行ったコードは次のとおりです。

private void startConversionPDF(File file) throws IOException { 
    if (args == null) { 
     throw new IllegalStateException("No conversion arguments set."); 
    } 

    PDFConvert data = new PDFConvert(); 
    data.setInput("upload"); 
    data.setOutputformat("pdf"); 
    ConverterOptions converteroptions = new ConverterOptions(); 
    converteroptions.setMargin_top(60); 
    converteroptions.setMargin_bottom(60); 
    converteroptions.setMargin_left(30); 
    converteroptions.setMargin_right(30); 
    data.setConverteroptions(converteroptions); 

    MultiPart multipart = new FormDataMultiPart() 
       .bodyPart(new FormDataBodyPart("json", data, MediaType.APPLICATION_JSON_TYPE)) 
       .bodyPart(new FileDataBodyPart("file", file)); 
    root.request(MediaType.APPLICATION_JSON).post(Entity.entity(multipart, multipart.getMediaType())); 
} 
関連する問題